Why automate email testing?
Manual email checking doesn't scale. When your app sends dozens of transactional emails — password resets, order confirmations, magic links, team invites — you need automated assertions that verify the right email was sent, to the right person, with the right content.
MailHog's API gives your test suite the same access your dashboard has. Query captured emails by recipient, subject, or time range, then assert on content, headers, and attachments.
Example: verify a password reset email
// In your integration test (Node.js / Jest example)
const res = await fetch(
"https://mailhog.site/api/v1/inbox/MY_INBOX/messages?to=user@example.com",
{ headers: { "X-API-Key": process.env.MAILHOG_API_KEY } }
);
const { data } = await res.json();
const email = data[0];
expect(email.subject).toContain("Reset your password");
expect(email.html).toContain("Reset Password");
expect(email.to[0].address).toBe("user@example.com");API capabilities
- List emails by inbox with pagination and filtering
- Search by recipient, sender, subject, or date range
- Get full email body (HTML and plain text)
- Download attachments programmatically
- Delete emails (useful for test cleanup)
- Wait for email delivery with polling or webhook
CI/CD integration
Most teams wire MailHog into their GitHub Actions, GitLab CI, or Jenkins pipelines. Your CI spins up your app pointed at MailHog's SMTP server, triggers email-sending flows, then uses the API to verify results. This catches email regressions before merge.
# .github/workflows/test.yml
env:
SMTP_HOST: smtp.mailhog.site
SMTP_PORT: 2525
SMTP_USER: ${{ secrets.MAILHOG_SMTP_USER }}
SMTP_PASS: ${{ secrets.MAILHOG_SMTP_PASS }}
MAILHOG_API_KEY: ${{ secrets.MAILHOG_API_KEY }}
steps:
- run: npm test # includes email assertion testsStart automating email tests
API access is available on Hobby tier and above.