Skip to content
ES

Concepts

Email

How a context sends emails through SMTP, and how your tests read them.

A context sends its emails with base.email. It exists so your modules can write to someone without caring whether the email goes through a server, to the console or nowhere at all.

SMTP needs nodemailer, which newt does not install for you:

Terminal window
bun add nodemailer

Give the context smtpEmail(), next to its database and its files:

src/contexts/tasks/tasks.context.ts
import { smtpEmail } from '@newt-js/core/email/smtp'
const { database, storage, email } = await openData('tasks', {
database: { adapter: sqlite(), migrations: MIGRATIONS_FOLDER },
files: localFiles(),
email: smtpEmail(),
router,
place,
})
const base = new ContextBase({
name: 'tasks',
platform,
database,
storage,
email,
})

And say in .env which server to use:

.env
SMTP_URL=smtp://user:password@smtp.example.com:587
EMAIL_FROM=Newthub <hello@example.com>
Variable Meaning
SMTP_URL The server: smtp:// (port 587, usually) or smtps:// for TLS from the start (port 465).
EMAIL_FROM Who the emails are from.
<CONTEXT>_EMAIL_FROM Who the emails of one context are from, instead.
When What happens
bun run dev, with no SMTP_URL They are written to the console, links and all.
With SMTP_URL They are sent through the server.
In production, with no SMTP_URL The backend does not start, and says what is missing.
In tests (data: 'memory') They are kept in memory. None goes out.
await this.base.email.send({
to: 'ana@example.com',
subject: 'You have a new task',
text: 'Write the report, by Friday.',
})
Field Required Meaning
to yes An address, or several. Each alone, with no name: ana@example.com.
from no Who it is from, { name, address }. What you leave out is EMAIL_FROM’s.
subject yes The subject.
text yes The email as plain text.
html no The same as HTML.
replyTo no Where answers go.

send rejects when the server does not take the email, or does not answer in 10 seconds.

To sign an email with another name, from the same address:

await this.base.email.send({
to: 'ana@example.com',
from: { name: 'Newthub Support' },
subject: 'We got your question',
text: 'We will answer today.',
})

A server may be down for a moment. Send from a job, and newt tries again by itself:

src/contexts/tasks/modules/reminder/reminder.module.ts
@UseCase()
async tellAssignee(taskId: string): Promise<void> {
const task = await this.deps.task.get(taskId)
const reminder = Reminder.sent({ task })
await this.base.email.send({
to: task.assignee.email,
subject: 'You have a new task',
text: task.title,
})
await this.repository.persist(reminder)
}

A job may run twice, so the same email may go out twice. Make that harmless.

A test context keeps its emails:

await context.settle()
expect(context.email.sent).toMatchObject([
{ to: 'ana@example.com', subject: 'You have a new task' },
])

To try a server that fails:

spyOn(context.email, 'send')
.mockRejectedValueOnce(new Error('down'))

With the whole backend, ask for those of one context:

import { sentEmails } from '@newt-js/core/application/testing'
const backend = await startBackend(0, { data: 'memory' })
const emails = sentEmails(backend.context('tasks').base)