Mock mail setup for Travis CI

Travis CI offers an excellent and free! continuous integration service. It runs your unit tests after you pushed your code to GitHub and deploys your changes to your staging/production servers if your tests pass.

One of the issues I run into while setting up my application with Travis was that my tests require email to work. By default tests sending emails will fail because the underlying mail system is disabled. In my case testing a ZF2 application, the error message was along those lines:

Configuration read from /home/travis/build/[...]/phpunit.xml 1: -t: not found
Time: 5 seconds, Memory: 73.00Mb
There was 1 error:
1) UserTest\Controller\UserControllerTest::testRegisterActionValidUser
Zend\Mail\Exception\RuntimeException: Unable to send mail: Unknown error

To work around this you need to set up a mock mail service. This requires you to set up your .travis.yml according to this example:

language: php

  - 5.3
  - 5.4
  - 5.5

  - sudo apt-get update -qq
  - sudo apt-get install -y -qq postfix

  - sudo service postfix stop
  - smtp-sink -d "%d.%H.%M.%S" localhost:2500 1000 &
  - echo -e '#!/usr/bin/env bash\nexit 0' | sudo tee /usr/sbin/sendmail
  - echo 'sendmail_path = "/usr/sbin/sendmail -t -i "' | sudo tee "/home/travis/.phpenv/versions/`php -i | grep "PHP Version" | head -n 1 | grep -o -P '\d+\.\d+\.\d+.*'`/etc/conf.d/sendmail.ini"

  email: false

This solution is based on the PHPMailer Travis configuration. Thanks!

Mock mail setup for Travis CI