Fakemail for Acceptance Testing

testing tutorial


A big part of automated acceptance testing involves confirming your applications are sending out the right emails. I looked at a few ways of accomplishing this:

Method 1)

Our framework logs a record of all outgoing email to a log file. In my tests, I was reading those log files to confirm the emails I expected were there. This worked okay, but our logs only records to, from, subject and a truncated version of the body. Not enough data, and I didn’t want to add more.

Method 2)

I played around with reading a RSS feed of a test Gmail account. This kind of worked, except you could only really get the subject information, so that wouldn’t cut it.

Method 3)

I had Selenium actually log into Gmail to check for the emails. This made the tests slower as it had to log in to Gmail, and was brittle because if Gmail ever changed their interface my tests would break.

Final Method: Fakemail

Finally, I settled on Fakemail, which is – as the name implies – is a fake mail server that essentially intercepts outgoing mail from your server and puts the messages in text files in the directory of your choosing.

Here’s the set up…

Download Fakemail


Unzip the folder, then in Terminal navigate to the folder and run this command:

sudo python setup.py install

Start Fakemail

Start the fakemail daemon. The path is where your mail will get dumped too. Leave this running, so any mail will get routed through it.

fakemail.py --host=localhost --port=10025 --path=/Users/YourName/Sites/Fakemail

Send some emails

Now, time to send emails. It doesn’t use the plain old PHP mail() function (probably because you need control over what port the email sends through?). Instead, you’ll want to use PHPMailer.

You can find out more about PHPMailer here, but here’s the quick and dirty:

Make sure you have class.phpmailer.php and class.smtp.php in place for the following example (you can find them here).


$mail = new PHPMailer();
$mail->From = 'from@foobar.com';
$mail->Body = 'Hi!';
$mail->Subject = 'Hello';
$mail->Mailer = 'smtp';
$mail->Host = 'localhost';
$mail->Port = isset($_GET['port']) ? $_GET['port'] : 10025;
if ($mail->send()) {
    print 'Mail sent to <em>' . $_GET['email'] . '</em><br />';

Check your results

When you hit that script, if you go back to Terminal where you have the fakemail daemon running, you should see something like this:

Susan-Bucks-MacBook-Air:~ Susan$ fakemail.py --host=localhost --port=10025 --path=/Users/Susan/Sites/jarapps/codagogy.com/fakemail/
Starting fakemail.py
Listening on port 10025
Incoming mail
Capturing mail to codagogy@gmail.com
Mail to codagogy@gmail.com saved
Incoming mail dispatched

Navigate to the path you specified, and you should see a text file with your email.

Susan-Bucks-MacBook-Air:fakemail Susan$ ls
Susan-Bucks-MacBook-Air:fakemail Susan$ cat codagogy@gmail.com.1
Date: Wed, 6 Feb 2013 00:56:46 -0500
Return-Path: info@codagogy.com
To: Codagogy <codagogy@gmail.com>
From: Codagogy <info@codagogy.com>
Reply-To: Codagogy <info@codagogy.com>
Subject: Test
Message-ID: <d5feb20154f7cc96ce10da26589c34eb@codagogy.dev>
X-Priority: 3
X-Mailer: PHPMailer 5.2.1 (http://code.google.com/a/apache-extras.org/p/phpmailer/)
MIME-Version: 1.0
Content-Type: multipart/alternative;

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

To view the message, please use an HTML compatible email viewer.

Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

Test body


Susan-Bucks-MacBook-Air:fakemail Susan$ 


Comments are closed.