Testing Emails in Automated Test Scripts with a Fake SMTP server: MailCatcher
This article is included in my “How to in Selenium WebDriver” series.
On a recent software project, I heard of an embarrassing mistake: emails have been sent to real customers during internal system testing.
CUSTOMERS SHALL NEVER RECEIVE TESTING EMAILS!
Unfortunately, this could happen to software testing after certain production data was loaded for verifying production issues when lacking good prevention procedures.
To avoid this, I have seen many solutions have been used, such as:
- Disable emails sending
- Replace all account emails with a fixed test email address
- An improved version of the above by including the recipient
However, all of the above were complex and inadequate. My favorite (and the simplest) solution is to use a fake SMTP server, such as MailCatcher. I have been testing emails this way for over 10 years, never had any problems. There are some alternative commercial email testing servers/services on the market, such as MailTrap (recommend plan costs $49/month). Even taking the cost out of the equation, I still think the free MailCatcher is far better.
Why fake SMTP Server makes sense for Testing emails?
The purpose of email testing is to ensure that
- the emails are received (to the correct address)
- the email content is correct
The actual sending/receiving via external Internet in the context of software testing does not matter. That has been taken care of by the SMTP protocol, and we don’t need to test every time.
Furthermore, there are three extra benefits of using a fake SMTP Server:
- email delivery is not guaranteed
Email is only ‘best effort’ delivery. Even though it is highly reliable by the design, the delivery is not guaranteed. There are no such concerns with fake SMTP servers: the “sent” emails are guaranteed to arrive.