Grav v1.14.0

SMTP

import "std/net" (not included in import "std") · namespace std.net

SmtpClient sends email through a mail server; EmailMessage builds the message, with a text and/or HTML body and attachments.

grav
import "std"
import "std/net"

fn main() {
    let mail = new EmailMessage()
        .from("Ada <ada@example.com>")
        .to("bob@example.com")
        .cc("Cy <cy@example.com>")
        .subject("Quarterly numbers")
        .text("See the attachment.")
        .attach("q3.csv", "text/csv", Bytes.fromString("month,total\nJul,12\n"))
    print(mail.allRecipients())
    let mime = mail.toMime()
    print(Str.contains(mime, "Subject: Quarterly numbers"))
    print(Str.contains(mime, "multipart/mixed"))
    print(EmailMessage.encodeHeader("Grüße"))
    // new SmtpClient("smtp.example.com", 587).login(user, password).send(mail)
}
Output
["bob@example.com", "cy@example.com"]
true
true
=?UTF-8?B?R3LDvMOfZQ==?=
  • The message: from, to, cc, bcc (envelope only, never shown), replyTo, subject, text, html (sent as alternatives when both are set), header(name, value) and attach(filename, type, data). toMime() gives the message as sent: UTF-8 bodies in base64, a Date and a Message-ID, and non-ASCII subjects as encoded words.
  • Sending: new SmtpClient(host, port), then login(user, password) if the server needs it, then send(message). Port 587 upgrades to TLS with STARTTLS (and refuses to go on without it), port 465 uses TLS from the start; other ports use STARTTLS when offered. withSecurity("none") is for local test servers, withTls(ctx) changes which certificates are trusted.
  • Logging in tries AUTH PLAIN, then LOGIN. A refusal at any step throws NetError with the server's reply (credentials are never included in it).

API

class EmailMessage

An email: addresses, subject, a text and/or HTML body, and attachments.

Constructors

  • new EmailMessage()

Static members

  • static fn address(text: string) -> string — "addr@host" from "Name <addr@host>".
  • static fn encodeHeader(value: string) -> string — A header value safe for any text: non-ASCII is written as a MIME encoded word (=?UTF-8?B?...?=).
  • static fn base64Lines(data: Bytes) -> string — Base64 in lines of 76 characters (as MIME bodies are sent).

Fields

  • sender: string — The From address.
  • recipients: Array<string> — The To addresses.
  • ccList: Array<string> — The Cc addresses.
  • bccList: Array<string> — The hidden recipients.
  • subjectLine: string — The Subject.
  • textBody: string — The plain-text body.
  • htmlBody: string — The HTML body.
  • replyAddress: string — The Reply-To address ("" for none).
  • headers: Headers — Extra headers.
  • attachments: Array<FormPart> — Attached files.

Methods

  • fn from(address: string) -> EmailMessage — The sender, "Name <addr@host>" or "addr@host"; returns this message.
  • fn to(address: string) -> EmailMessage — Adds a recipient; returns this message.
  • fn cc(address: string) -> EmailMessage — Adds a copy recipient (listed in the Cc header).
  • fn bcc(address: string) -> EmailMessage — Adds a hidden recipient (not listed in the message).
  • fn subject(text: string) -> EmailMessage — The subject; returns this message.
  • fn text(body: string) -> EmailMessage — The plain-text body; returns this message.
  • fn html(body: string) -> EmailMessage — An HTML body (sent next to the text one, if any); returns this message.
  • fn replyTo(address: string) -> EmailMessage — Where replies should go; returns this message.
  • fn header(name: string, value: string) -> EmailMessage — Adds a header; returns this message.
  • fn attach(filename: string, contentType: string, data: Bytes) -> EmailMessage — Attaches a file; returns this message.
  • fn allRecipients() -> Array<string> — Every envelope recipient: to, cc and bcc, as bare addresses.
  • fn toMime() -> string — The message in MIME format (headers and body, CRLF line ends).

class SmtpReply

A reply from an SMTP server: its code and text (lines joined with "\n").

Constructors

  • new SmtpReply(code: int, text: string)

Fields

  • code: int — The three-digit reply code (250, 354, ...).
  • text: string — The reply text.

Methods

  • fn toString() -> string

class SmtpClient

Sends email through an SMTP server.

Constructors

  • new SmtpClient(host: string, port: int)

Static members

  • static fn reply(conn: TcpStream) -> SmtpReply — One reply, joining continuation lines ("250-a" then "250 b").
  • static fn offers(ehlo: SmtpReply, ext: string) -> bool — Whether an EHLO reply lists ext.
  • static fn capability(ehlo: SmtpReply, ext: string) -> string — The parameters of an EHLO extension ("AUTH PLAIN LOGIN" -> "PLAIN LOGIN").
  • static fn dotStuff(text: string) -> string — A message body ready for DATA: lines starting with "." get another one, and every line ends with CRLF.

Fields

  • host: string — The mail server.
  • port: int — Its port: 587 (submission), 465 (TLS) or 25.
  • security: string — "starttls" (upgrade when offered; required on port 587), "tls" (from the start; the default on port 465) or "none" (never encrypt).
  • heloName: string — The name sent in EHLO (this machine's host name by default).
  • timeoutMs: int — Give up on a silent server after this long.
  • tls: TlsContext — TLS settings (null: verify against the system's CAs).

Methods

  • fn login(user: string, password: string) -> SmtpClient — Logs in with these credentials (AUTH PLAIN or LOGIN); returns this client.
  • fn withSecurity(mode: string) -> SmtpClient — Sets the security mode ("starttls", "tls" or "none"); returns this client.
  • fn withTls(ctx: TlsContext) -> SmtpClient — Uses these TLS settings; returns this client.
  • fn send(message: EmailMessage) — Sends message to all its recipients in one session.

Edit this page on GitHub