Test transactional email content
A receipt or welcome email can be sent successfully and still be wrong: the subject says undefined, the name is missing, a template placeholder like {{order.total}} shows up literally, the plain-text part is empty, or the PDF invoice is not attached. These are content bugs, and unit tests on the template rarely see them because they appear only after the real rendering and sending path. This guide shows a node:test file that triggers a transactional email, reads it through AssertInbox and checks the facts a customer would notice.
What to check
The message JSON from GET /v1/wait carries what you need: from, reply_to, subject, text, html, links and attachments (each with filename, content_type and size in bytes). The tests below cover:
- The sender address and the
reply_toaddress, so replies reach a mailbox someone reads. - The subject: it names the thing (order number, product), and contains no placeholder residue.
- A non-empty plain-text part. Some clients and spam filters look at it, and an empty one often means the text template was forgotten.
- No unrendered placeholders anywhere:
{{,}},%7B%7B,undefined,NaN,[object Object]. - The recipient's name appears, which proves the data reached the template.
- Link hosts belong to you.
- An expected attachment, such as the invoice, by name, type and a plausible size range.
Get an API key
Get one in a click at /start. One request creates a key and your private namespace, with no email address or card. The key is shown once, so keep it in your secret store and export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page, and the client files can replace the helper functions below.
The test file
Save this as receipt-email.test.js and run it with node --test on Node 18 or newer. The route that creates a test order (/api/test/orders) and the expected values are placeholders for your own staging app; use it only with an app you own. Set EXPECTED_SENDER, EXPECTED_REPLY_TO and EXPECTED_HOST for your setup.
const { test, after } = require("node:test");
const assert = require("node:assert");
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
const APP = process.env.APP_URL || "http://localhost:3000";
const EXPECTED_SENDER = process.env.EXPECTED_SENDER || "orders@example.com";
const EXPECTED_REPLY_TO = process.env.EXPECTED_REPLY_TO || "support@example.com";
const EXPECTED_HOST = process.env.EXPECTED_HOST || new URL(APP).host;
const tags = [];
function newInbox() {
const tag = ("receipt-" + Date.now() + "-" + Math.random().toString(36).slice(2, 8)).toLowerCase();
tags.push(tag);
return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
}
async function waitForEmail(tag, filters = {}) {
const params = new URLSearchParams({ tag, timeout: "25", ...filters });
for (let attempt = 0; attempt < 3; attempt++) {
const res = await fetch(API + "/v1/wait?" + params, {
headers: { Authorization: "Bearer " + KEY },
});
if (res.status === 200) return res.json();
if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status);
}
throw new Error("No email arrived for tag " + tag);
}
const PLACEHOLDER_RESIDUE = [/{{/, /}}/, /%7B%7B/i, /\bundefined\b/, /\bNaN\b/, /\[object Object\]/];
function residue(label, value) {
return PLACEHOLDER_RESIDUE.filter((re) => re.test(value)).map((re) => label + " contains " + re);
}
after(async () => {
for (const tag of tags) {
await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), {
method: "DELETE",
headers: { Authorization: "Bearer " + KEY },
});
}
});
test("the receipt email is complete", { timeout: 120000 }, async () => {
const inbox = newInbox();
const customer = "Ada Lovelace";
const res = await fetch(APP + "/api/test/orders", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email: inbox.email, name: customer, items: [{ sku: "demo", quantity: 2 }] }),
});
assert.ok(res.ok, "order returned " + res.status);
const mail = await waitForEmail(inbox.tag, { subject: "receipt" });
assert.ok(mail.from.toLowerCase().includes(EXPECTED_SENDER), "sender was " + mail.from);
assert.strictEqual((mail.reply_to || "").toLowerCase(), EXPECTED_REPLY_TO);
assert.match(mail.subject, /receipt|order/i);
assert.ok(mail.text.trim().length > 50, "the plain-text part is empty or very short");
assert.deepStrictEqual(
[...residue("subject", mail.subject), ...residue("text", mail.text), ...residue("html", mail.html)],
[],
);
assert.ok(mail.text.includes("Ada") || mail.html.includes("Ada"), "the customer's name is missing");
for (const raw of mail.links) {
assert.strictEqual(new URL(raw).host, EXPECTED_HOST, "link to another host: " + raw);
}
const invoice = mail.attachments.find((a) => /^invoice-.*\.pdf$/i.test(a.filename || ""));
assert.ok(invoice, "no invoice attachment in " + JSON.stringify(mail.attachments));
assert.strictEqual(invoice.content_type, "application/pdf");
assert.ok(invoice.size > 1000 && invoice.size < 2000000, "invoice size was " + invoice.size);
});
Run it with the key in the environment:
export ASSERTINBOX_KEY=ai_your_key_here
export ASSERTINBOX_NAMESPACE=your_namespace
APP_URL=https://staging.example.com EXPECTED_SENDER=orders@your-domain.example node --test receipt-email.test.js
Notes on each check
Placeholders. The pattern list is deliberately blunt. A real receipt may legitimately contain the word "undefined" in a product name, which is rare enough that a false alarm is worth a look. If your product names can include double braces, narrow the patterns. The residue helper reports which part of the email matched, so the failure message points at the template.
The recipient's name. Use a distinctive name in the test data, not "Test", so that a hard-coded fallback in the template does not pass by accident. Names with accents or apostrophes are a cheap way to find encoding bugs; assert on the exact string once you know your templates handle it.
Attachments. AssertInbox keeps only the name, type and size of each attachment, never the content. A size range catches an empty or failed PDF render without pinning the file to an exact byte count, which changes whenever the layout does. If you need to check what is inside the PDF, test the generator directly.
Plain text. The test requires more than a few characters. Some template systems send HTML only; if that is intentional for your product, drop the assertion and say so in a comment.
What not to assert
- The complete text or HTML. Copy edits would break the test on every change, and a test that breaks on every change gets deleted.
- Exact prices unless the test controls the data. Compute the expected total from the order you created.
- Timing. Delivery speed belongs in monitoring, not in a functional test; see asserting that an email was not sent for waiting strategies.
For links, checking every link in an email goes further than the host check here.
Troubleshooting
- The wait times out. The
subjectfilter is a case-insensitive substring of the real subject. Print the subject from the app's own logs. attachmentsis empty. The attachment may be generated by a background job that sends a second email. Wait for it with theafterfilter.reply_tois null. The sender did not set a Reply-To header, so replies go to the From address.- HTTP 429. The key used its 2,000 requests for the day; see the limits page.
Next steps
For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.