Mail: KScript.Mail
Introduction
Most apps send email: a welcome message, a receipt, a link to reset a password. KScript.Mail sends it with one command, @send_mail. Where the mail goes is a setting, not code: while you build, every message is written to the log; in production, it goes through your SMTP server; in tests, a fake keeps it so you can check it.
import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Welcome to the shop', text: 'Thanks for signing up.')
With no settings, that message is written to the log, so you can see what would have been sent.
Configuration
Mail settings live under mail in the config section of kscript.json:
{
"config": {
"mail": {
"driver": "smtp",
"host": "smtp.example.com",
"port": 587,
"username": "shop",
"from": { "address": "hello@example.com", "name": "The Shop" }
}
}
}
| Setting | What it does |
|---|---|
driver |
log (the default) writes each message to the log. smtp sends it to a mail server. fake sends nothing and keeps it, as @fake_mail() does. |
host, port |
The SMTP server. The defaults are localhost and 587. |
username, password |
The SMTP sign-in. Leave the password out of kscript.json: it is read from the MAIL_PASSWORD environment variable (and the username from MAIL_USERNAME) when they aren’t set here. |
encryption |
tls (the default) upgrades the connection with STARTTLS. none sends in the clear, for a test server on your own computer. |
from |
Who mail comes from unless @send_mail says: "hello@example.com", "The Shop <hello@example.com>", or { "address": ..., "name": ... }. |
queue |
The queue queued mail goes on. Defaults to default. |
As with every config setting, kscript.<env>.json can change them for one environment. A kscript.test.json with "driver": "fake" keeps tests from sending anything, and config.kscript can read the server from environment variables:
fn configure(environment: string) {
return {
mail: {
driver: 'smtp'
host: @env('MAIL_HOST', default: 'localhost')
port: @env('MAIL_PORT', default: '587')
}
}
}
Sending mail
@send_mail takes who the mail is for, a subject and a body, and returns the message it sent as a map:
import KScript.Mail
$sent = @send_mail(to: 'joan@example.com', subject: 'Your receipt', text: 'Thanks for your order.')
echo $sent.subject
to: takes an address, a user (any map or record with an email), or a list of them. cc: and bcc: take the same, and reply_to: and from: take one address:
import KScript.Mail
@send_mail(
to: ['joan@example.com', 'ada@example.com'],
cc: 'orders@example.com',
reply_to: 'help@example.com',
subject: 'Your order has shipped',
text: 'It is on its way.',
)
The body
A message has one body: text:, html: or view:. Giving two is an error. HTML mail gets a plain-text copy made from it, for mail readers that don’t show HTML.
import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Welcome', html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>')
Views
Longer messages belong in a view, like a page. view: 'mail/welcome' renders views/mail/welcome.html (or .md, for Markdown mail), and the keys of data: are its variables:
<!-- views/mail/welcome.html -->
<h1>Welcome, { $name }</h1>
<p>Your account is ready. <a href="{ $link }">Sign in</a> to get started.</p>
import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Welcome', view: 'mail/welcome', data: { name: 'Joan', link: 'https://example.com/login' })
kscript make mail welcome --vars name,link writes a view to start from, and says how to send it.
Attachments
attachments: takes a list of files, as paths relative to the script or file values:
import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Your invoice', text: 'It is attached.', attachments: ['invoices/1042.pdf'])
Queued mail
Talking to a mail server takes a moment, and a page shouldn’t wait for it. Add queue, and the message goes on a queue for kscript worker to send:
import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Welcome', view: 'mail/welcome', data: { name: 'Joan' }, queue)
The view is rendered when you call @send_mail, so the worker sends exactly what you made. The message goes on config.mail.queue of the project’s default queue connection, and is tried three times before it counts as failed (kscript queue failed lists it). On the sync connection, which tests use, it is sent at once.
Testing mail
@fake_mail() keeps every message the test sends from then on, queued or not, instead of sending it. Its methods say what was sent:
import KScript.Test
import KScript.Mail
test(name: 'sign-up sends a welcome email') {
$mail = @fake_mail()
@send_mail(to: 'joan@example.com', subject: 'Welcome', text: 'Hi Joan', queue)
expect($mail->has_sent('joan@example.com'), equals: true)
expect($mail->sent('joan@example.com')[0].subject, equals: 'Welcome')
expect($mail->queued()->count(), equals: 1)
}
| Method | What it gives |
|---|---|
->sent(to?) |
The messages, or only those to one address or user: maps of to, cc, bcc, from, reply_to, subject, html, text, view, attachments and is_queued. |
->queued(to?) |
The messages sent with queue. |
->has_sent(to) |
True when mail went to the address, as to, cc or bcc. |
->count() |
How many messages were sent. |
->count_of(to) |
How many went to one address. |
->clear() |
Forgets them, and returns how many there were. |
@fake_mail() works in routes a test calls too, so a test can post a sign-up form and then check the email it sent.