Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

The ecosystem · email

Email that goes out over SMTP, or waits in a mailbox while you develop

A welcome, a sign-in link, a receipt: Email.send! hands a message to your SMTP server, as text or as text and HTML. While you develop, with no SMTP server set, the message is kept in the server's memory and shown in the dev mailbox. Nothing reaches a real inbox by accident, and nothing is lost: a deployed server with no SMTP server doesn't start.

The package runs on the server only. Its plugin adds the dev mailbox's pages and checks your SMTP server at start; you send with the Email module.

Installing it#

Name the package in your app's header (email: "../packages/email/main.roc"), import email.Email, and list its plugin in your program's plugins:

server/test/app/main.roc, in part
# Email (packages/email): SMTP_URL and MAIL_FROM from the environment, the dev mailbox under --dev.
Email.plugin({}),

Both options have defaults:

OptionDefaultWhat it is
from : StrMAIL_FROM from the environment, or else rocx <rocx@localhost>who email is from: "Acme <hello@example.com>". MAIL_FROM, if set, wins, so a deploy names the sender.
smtp_url! : {} => Strreads SMTP_URL from the environmentwhere email goes, read once at start. Give your own function to read it from elsewhere, such as a secrets vault. "" means no SMTP server, which only --dev (and --debug, for tests) allows.

Sending an email#

Email.send!({ to, subject, text, html }) sends one message and waits for the SMTP server's answer. to is an address, with or without a name ("Ada Lovelace <ada@example.com>"), or several (below). Leave out html, or give "", for text only. It answers Ok({}), or EmailErr with the reason: an address it can't read, a header it refuses, a server that refuses the message or can't be reached, or no SMTP server outside --dev.

Send from a job, not from the request: the request doesn't wait for the SMTP server, and a failed send is tried again later. A job that sends, from the server platform's test app:

server/test/app/main.roc, in part
## Sends an email.
mail_job : Job.Kind(_, { to : Str, run : Str })
mail_job = Job.define(
	"mail",
	|_context, { to, run }|
		match Email.send!({ to, subject: "From a job ${run}", text: "Sent by a job.", html: "" }) {
			Ok({}) => Ok({})
			Err(EmailErr(why)) => Err(why)
		},
)

The job returns the send's error as its own, so the jobs plugin tries it again, a little later each time.

Several recipients, Cc, Bcc, Reply-To#

A message is a record with defaults (Email.Message): only to, subject and text are required.

Minutes, to the team
Email.send!({
	to: "Ada <ada@example.com>, \"Lovelace, Grace\" <grace@example.com>",
	cc: ["lin@example.com"],
	bcc: ["archive@example.com"],
	reply_to: "team@example.com",
	headers: [("List-Unsubscribe", "<https://example.com/unsubscribe?t=${token}>")],
	subject: "Minutes",
	text: minutes,
})
FieldDefaultWhat it is
to : Str(must be given)one or several addresses, separated by commas as in an email's To header; a comma inside a quoted name is the name's ("Lovelace, Ada" <ada@example.com>). At least one.
html : Str""the same as HTML; "" for text alone
cc : List(Str)[]more who see it, in its Cc header; each item one address or several
bcc : List(Str)[]more who get it, named in no header: only the SMTP server is told, so no recipient sees them
reply_to : Str""where a reply goes; "": to the sender
headers : List((Str, Str))[]your own headers, such as List-Unsubscribe

to stays a Str, so a message for one recipient is unchanged. For a list, join it: Str.join_with(addresses, ", "). The SMTP server is told each address in to, cc and bcc once. If it refuses one, nobody gets the message, and Email.send! names the refused address.

Your headers can't break the message. One with a line break in its name or value (which would start a new header, say a Bcc that quietly adds a recipient) is refused, as are the headers the package writes (From, To, Cc, Bcc, Reply-To, Subject, Date, Message-ID, MIME-Version, Content-*): use the field instead. A value that isn't plain ASCII is encoded, as the subject is.

Connecting to an SMTP server#

SMTP_URL (or what your smtp_url! returns) says where to connect and how:

URLConnectionDefault port
smtps://user:password@hostTLS from the start465
smtp://user:password@host?tls=requiredSTARTTLS; fails if the server doesn't offer it587
smtp://user:password@hostSTARTTLS, required as above: a password never goes in clear25
smtp://host?tls=opportunisticSTARTTLS if the server offers it, plain otherwise (with a user: refused if it isn't offered)25
smtp://hostplain, no TLS (a local relay, or a test server)25
smtp://user:password@host?tls=neverplain, the password too (a relay on this machine)25

A port in the URL (host:2525) overrides the default. With a user in the URL, the client signs in with AUTH PLAIN; write special characters in the user or password as % escapes. The password goes in clear only with ?tls=never.

Once the server has started, the plugin connects to the SMTP server (TLS and all), says goodbye, and logs whether it answered, under email.smtp, so a wrong URL shows in the log, not at the first email. An SMTP server that doesn't answer doesn't stop the app.

The dev mailbox#

A server started with --dev (as rocx dev starts it) answers /__rocx/mail before your app. It lists the email kept instead of sent, newest first (when, from, to, subject), with a Clear button, updating as mail arrives. Each message has a page at /__rocx/mail/<id>: its headers (Cc, Bcc, Reply-To and your own), its text, its HTML in a sandboxed frame (no script runs; links open in a new tab), and the whole message as SMTP would have carried it, where no header names the Bcc. The dev server lists the mailbox's address as it starts.

The last 100 messages are kept in the server's memory (Kv), with or without a database, so a restart forgets them, on purpose. /__rocx/mail.json has them as JSON, for tests: the testing package reads it to find the link in a sign-in email. Under --debug without --dev, only the JSON is served. Without either flag, these paths are your app's.

How it works#

The package is Roc on what the server platform gives every plugin. Email.roc writes the message: it reads every address first, encodes a subject or name that isn't plain ASCII (RFC 2047), sends such a body as Base64, and makes text with HTML multipart/alternative. Smtp.roc is a small SMTP client over the platform's Tcp, which has TLS: a RCPT TO for each recipient, then the message, on a new connection for each send. Mailbox.roc is the dev mailbox: paths a plugin answers before the app, only under --dev or --debug (plugins), drawn in the admin's look.

Without the plugin, Email.send! still works, reading SMTP_URL and MAIL_FROM itself on each send. You lose the mailbox, the SMTP check at start, and the refusal to start without a server: a send fails instead.

Not yet#

The package's README.md (packages/email/) has the full reference.