Automated Email Testing in CI: GitHub and GitLab
An automated email test sends a real message and asserts on what arrived. Your code sends over plain SMTP, your test reads the result over the API, and the build fails when the email is wrong or never shows up. send.smtp.dev delivers only to accounts in your sandbox, so test emails can't reach anyone real.
One-Time Setup
Create a sender and a receiver account
On the Accounts page or via the API, for example ci@example.com and user@example.com.
Create an API token
On the API Keys page. The token is shown once.
Store the secrets in your repository
SMTP_DEV_API_KEY (the token) and SMTP_DEV_PASSWORD (the sender account's password). On GitHub, under Settings → Secrets and variables → Actions. On GitLab, under Settings → CI/CD → Variables.
The Test
Point your app's SMTP transport at the sandbox. With nodemailer:
import nodemailer from 'nodemailer'
const transport = nodemailer.createTransport({
host: 'send.smtp.dev',
port: 587,
requireTLS: true,
auth: { user: 'ci@example.com', pass: process.env.SMTP_DEV_PASSWORD },
})
Then assert on what arrived:
import assert from 'node:assert'
import { test } from 'node:test'
const API = 'https://api.smtp.dev'
const headers = { 'X-API-KEY': process.env.SMTP_DEV_API_KEY }
async function api(path) {
const res = await fetch(API + path, { headers })
assert.ok(res.ok, `${path} returned ${res.status}`)
return res.json()
}
async function inboxOf(address) {
const { member } = await api(`/accounts?address=${encodeURIComponent(address)}`)
const account = member.find(a => a.address === address)
const { id, mailboxes } = await api(`/accounts/${account.id}`)
return { accountId: id, mailboxId: mailboxes.find(m => m.path === 'INBOX').id }
}
async function waitForMessage({ accountId, mailboxId }, match, timeout = 30_000) {
const deadline = Date.now() + timeout
while (Date.now() < deadline) {
const { member } = await api(`/accounts/${accountId}/mailboxes/${mailboxId}/messages`)
const found = member.find(match)
if (found)
return api(`/accounts/${accountId}/mailboxes/${mailboxId}/messages/${found.id}`)
await new Promise(resolve => setTimeout(resolve, 2000))
}
throw new Error(`no matching message after ${timeout}ms`)
}
test('welcome email', async () => {
const to = `user+${crypto.randomUUID()}@example.com`
await transport.sendMail({
from: 'ci@example.com',
to,
subject: 'Welcome',
text: 'Confirm your address: https://example.com/confirm/abc123',
})
const inbox = await inboxOf('user@example.com')
const message = await waitForMessage(inbox, m => m.to.some(t => t.address === to))
assert.equal(message.subject, 'Welcome')
assert.equal(message.from.address, 'ci@example.com')
assert.match(message.text, /\/confirm\//)
})
Each test sends to its own plus alias of user@example.com and matches on it. Parallel jobs and matrix builds can share the one account without reading each other's mail, and nothing has to be created or deleted per run. Plus aliases work on every plan.
waitForMessage throws when no message matches within 30 seconds. The test fails, npm test exits non-zero, and the job goes red. List Messages returns 30 messages per page, newest first, so a message that just arrived is on the first page. The API quickstart explains the endpoints the helpers call.
What to Assert
The welcome test checks three fields. More of a message can break between releases:
| Assert on | Catches |
|---|---|
to, from, replyTo | A fixture address or the wrong sender left in a template |
subject | Template variables that didn't render, like {{name}} |
The origin of links in text | Links to localhost or production from a staging build |
html | A missing HTML part, or one that drifted from the text |
attachments | A file that's missing, empty, or has the wrong content type |
The Nodemailer guide has each of these as code.
The Workflow
name: email-tests
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v4
with:
node-version: 24
- run: npm ci
- run: npm test
env:
SMTP_DEV_API_KEY: ${{ secrets.SMTP_DEV_API_KEY }}
SMTP_DEV_PASSWORD: ${{ secrets.SMTP_DEV_PASSWORD }}
email-tests:
image: node:24
script:
- npm ci
- npm test
GitHub passes a secret only to the step that names it under env. GitLab passes its CI/CD variables to every job, so its file has nothing to map. A variable marked Protected only reaches protected branches, so tests on a feature branch run without the secrets and fail at the SMTP login.
Any other CI works the same way: the two secrets in the environment, then the test command. The sandbox is reached over the public internet on ports 587 and 443, so there's no service container to start.
Next
- Playwright and Cypress run the same wait inside a browser test and click the link from the email.
- OTP codes and magic links covers login emails: resend, reuse, and single-use links.
- Nodemailer goes further on the sending side, with error codes and refused recipients.
API Quickstart
Create a test inbox over REST, send it a message, and read the subject, body, and SPF, DKIM, and DMARC results as JSON. No SDK.
Catch-All
One catch-all account receives mail for every address on your test domain. A fresh, real email address per test run, with no account to create for each one.