Verify CI Emails Without Delivering Them to Users
In this article
This guide shows you how to verify that your CI/CD pipeline’s email notifications (build failures, deploy alerts, test reports) are correctly formatted, addressed, and triggered — without spamming real users. You’ll learn to redirect all outbound email to a test inbox, use a local SMTP catcher, and inspect actual message content before any real delivery happens. This is essential for teams that send frequent automated emails and want to avoid embarrassing typos or misdirected alerts.
What You’ll Need
- Access to your CI/CD configuration files (e.g.,
.gitlab-ci.yml,Jenkinsfile,.github/workflows/, or similar). - An SMTP server or email service credentials (or a local SMTP catcher like MailHog or Mailpit).
- A test email address you control (e.g.,
ci-test@yourdomain.com). - Basic familiarity with YAML or your CI platform’s configuration syntax.
Step-by-Step
- Identify your email configuration. Find where your CI pipeline defines SMTP settings or email notification recipients. This is often in a global config, an environment variable, or a dedicated notification step.
- Set up a test inbox. Create a dedicated email address that only you and your team use for CI testing. Alternatively, use a service like Mailinator or a disposable email provider if you don’t need persistent access.
- Override the recipient list. In your CI configuration, replace all production recipient addresses with your test address. Do this in a way that is easy to revert — for example, using an environment variable like
CI_TEST_EMAIL. - Redirect SMTP to a local catcher (optional but recommended). If you want to inspect emails without them leaving your machine, install MailHog or Mailpit. Run it locally (e.g.,
mailhog), then set your CI’s SMTP host tolocalhostand port to1025. - Add a conditional flag. Introduce a variable like
EMAIL_ENABLEDthat defaults totruein production but is set tofalsein test runs. Wrap your email-sending step in a condition that checks this flag. This prevents accidental sends even if you forget to change the recipient. - Trigger a test pipeline. Push a dummy commit or manually run your pipeline with the test variables set. Ensure the pipeline reaches the email step without failing.
- Inspect the email. If using a local catcher, open its web UI (usually
http://localhost:8025) and click on the received message. Check the subject line, body content, and any attachments. If using a test inbox, log in and do the same. - Verify the recipient and headers. Confirm the email was sent to the test address only, and that the
FromandReply-Toheaders are correct. Look for any dynamic content (like build numbers) that might be broken. - Test failure scenarios. Intentionally break a build step to trigger a failure email. Verify that the failure alert is correctly formatted and includes the right error details.
- Document your test procedure. Write down exactly how to run the test (commands, variables, and where to look) so any team member can repeat it. Store this in your repository’s README or a CONTRIBUTING file.
- Re-enable production emails. Once you’re satisfied, revert the recipient list and remove the
EMAIL_ENABLED=falseflag. Do this in a separate commit that can be easily reverted if something goes wrong.
Common Mistakes
- Forgetting to change the SMTP port. If you point to
localhostbut leave the port as 587 (the standard submission port), your local catcher won’t receive anything. Use port 1025 for MailHog/Mailpit. - Hardcoding recipients in multiple places. If the email address appears in several steps, you might miss one. Use a single variable for the recipient.
- Testing only on success. Failure emails often have different templates. Always test at least one failed build.
- Leaving test credentials in production. After testing, remove any test SMTP credentials or localhost settings from your main configuration file.
- Not checking the spam folder. Some test inboxes (like Mailinator) may filter messages. Always check spam if you don’t see the email in the inbox.
Tips
- Use a separate Git branch for testing. Create a
ci-email-testbranch and make all your changes there. Merge it only when you’re done, and you’ll have a clean revert point. - Automate the verification. Write a small script that checks the email content for expected keywords (e.g., “BUILD FAILED”) and exits with a non-zero code if they’re missing. Integrate this into your pipeline as a final check.
- Use a dedicated SMTP service for testing. Services like Mailtrap or SendGrid’s test mode allow you to capture emails without a local server, which is useful for remote CI runners.
- Include a timestamp in the subject. When testing, add a unique identifier like the build number to the subject line. This makes it easy to match the email to the specific pipeline run.
- Test with a real email client. If your emails contain HTML, open them in an actual email client (like Gmail or Outlook) to ensure rendering works, not just in the catcher’s preview.
Troubleshooting
Email not received at all. First, check the CI logs for the email step. Look for errors like “connection refused” or “authentication failed.” If using a local catcher, make sure it’s running and the port is correct. If using a test inbox, check the spam folder. Also verify that the pipeline actually ran the email step — sometimes conditional logic skips it unintentionally.
Email received but with wrong recipient. Search your configuration for any hardcoded email addresses that might override the variable. Also check if your CI platform has a global notification setting that sends to a default list (e.g., the last committer).
Email content is broken (missing variables, empty fields). This usually means the template is referencing a variable that isn’t available in the test run. Add debug output to the email step to print all variables. Ensure that the test pipeline has the same environment variables as production, or mock them.
Local catcher not showing emails. If you’re running CI on a remote server (like GitHub Actions), a local catcher on your machine won’t work — the CI runner can’t reach it. Use a cloud-based SMTP testing service instead. If you’re on a local runner, double-check that the SMTP host is set to localhost and not 127.0.0.1 (they should be the same, but some tools treat them differently).
Pipeline fails because of the email step. If the email step itself is failing (not just the email not sending), check for syntax errors in the configuration. Also ensure that the SMTP credentials (if any) are correct for the test server. If you’re using a local catcher, it doesn’t require authentication, so remove any username/password fields.
Frequently Asked Questions
Can I test email delivery without any external service?
Yes, use a local SMTP catcher like MailHog or Mailpit. They run on your machine and intercept all outbound email, displaying them in a web interface. This is ideal for local development but not for cloud CI runners.
How do I prevent accidental emails to real users during testing?
Use a combination of a test recipient variable and a conditional flag like EMAIL_ENABLED. Set the flag to false in your test pipeline and wrap the email step in a condition that checks it. This way, even if you forget to change the recipient, no email is sent.
What if my CI platform sends its own notifications (e.g., build status) separately?
Those are separate from your custom email steps. To test them, you’ll need to change the notification settings in your CI platform’s UI or configuration to point to a test email address. This is usually done in the project settings.
Is it safe to use a public disposable email service for testing?
It’s generally safe for non-sensitive content, but be aware that anyone can access those inboxes if they know the address. Avoid sending real credentials or proprietary code in test emails. Use a private test address for anything confidential.
How can I verify that attachments are correctly generated?
Open the email in your catcher or test inbox and download the attachment. Check its file size and content. You can also add a step in your pipeline that checks the attachment exists and is non-empty before sending.
Should I test on every commit or only on release?
Test on every commit that changes the email configuration or templates. For other changes, a periodic test (e.g., weekly) is sufficient. Automating the test as part of your CI ensures it’s always up to date.