v0.21

mail

introduction

dframework includes built in mail transport services supporting local log output, zero dependency smtp delivery, aws ses, and custom webhooks.

configuration

mail configuration is defined in config/mail.js.

1import { Env } from 'dframework';
2
3export default {
4 default: Env.value('MAIL_MAILER', 'log'),
5 mailers: {
6 log: {
7 transport: 'log'
8 },
9 smtp: {
10 transport: 'smtp',
11 host: Env.value('MAIL_HOST', '127.0.0.1'),
12 port: Env.value('MAIL_PORT', 587),
13 encryption: Env.value('MAIL_ENCRYPTION', 'tls'),
14 username: Env.value('MAIL_USERNAME'),
15 password: Env.value('MAIL_PASSWORD')
16 },
17 ses: {
18 transport: 'ses',
19 region: Env.value('AWS_DEFAULT_REGION', 'us-east-1'),
20 key: Env.value('AWS_ACCESS_KEY_ID'),
21 secret: Env.value('AWS_SECRET_ACCESS_KEY'),
22 endpoint: Env.value('MAIL_ENDPOINT')
23 },
24 webhook: {
25 transport: 'webhook',
26 url: Env.value('MAIL_WEBHOOK_URL')
27 }
28 },
29 from: {
30 address: Env.value('MAIL_FROM_ADDRESS', 'hello@example.com'),
31 name: Env.value('MAIL_FROM_NAME', 'dframework')
32 }
33};

sending mail

the global Mail facade provides a fluent builder interface via Mail.to() as well as direct delivery via Mail.send():

1// direct send with message object
2await Mail.send({
3 to: 'user@example.com',
4 subject: 'welcome',
5 text: 'welcome to dframework'
6});

compiling view templates

1await Mail.to('user@example.com')
2 .subject('welcome to dframework')
3 .send('emails.welcome', { user });

when rendering email templates, .d views are compiled cleanly without browser asset tags (dstrn.css and dstrn.js).

plain text mail

1await Mail.to('user@example.com')
2 .subject('security alert')
3 .raw('a login from a new device was recorded');

switching mailers

to send using a specific mailer configured in config/mail.js:

1await Mail.mailer('ses')
2 .to('user@example.com')
3 .subject('welcome')
4 .send('emails.welcome', { user });

queueing mail

to send mail asynchronously in the background, use queue():

1await Mail.to('user@example.com')
2 .subject('order confirmation')
3 .queue('emails.order', { order });

this delegates delivery to Job.dispatch on your active queue driver without delaying the http response.

testing mail

use Mail.fake() to intercept mail in test environments:

1Mail.fake();
2
3await Mail.to('user@example.com')
4 .subject('welcome')
5 .send('emails.welcome', { user });
6
7Mail.assertSent('welcome');
8Mail.assertSent(m => m.to.some(r => (typeof r === 'string' ? r : r.address) === 'user@example.com'));
9Mail.assertSentCount(1);

you can also assert queued mail:

1Mail.assertQueued('order confirmation');

to assert that no mail was sent or queued during an operation:

1Mail.assertNothingSent();

when writing integration tests using Tester, helper methods are available directly on the Tester facade:

1Tester.fakeMail();
2
3await Tester.post('/register', { email: 'user@example.com' });
4
5Tester.assertSentMail('welcome');
6Tester.assertSentMailCount(1);
7Tester.assertQueuedMail('verify email');

to assert that no mail was dispatched or queued during an operation:

1Tester.assertNothingMailSent();

predicates passed to assertSent or assertQueued receive an object with { to, cc, bcc, from, subject, view, template, data, replyTo, html, text }. passing a string matches against either the template view name or the subject line. recipients in to, cc, and bcc are normalized address entries which can be email strings or { address, name } objects.