Working with mails in dev environment

If your application sends emails, you probably don’t want emails to be sent when you are developing on your machine.

If you use nice libraries like SwiftMailer, it is easy to use a mock instead of sending real emails. But if you don’t, you can act on PHP’s configuration: instead of installing and using a real SMTP server on your machine, you can fake one using a simple script.

The fake server will be a shell script: create it as /usr/local/bin/sendmail-fake:

1
2
3
4
5
6
#!/bin/bash
{
date
echo $@
cat
} >> /var/log/sendmail-fake.log

Set up file permissions and check that it works:

1
2
3
$ sudo chmod +x /usr/local/bin/sendmail-fake
$ sudo chmod 777 /var/log/sendmail-fake.log
$ /usr/local/bin/sendmail-fake

Now configure PHP to use it in the php.ini:

1
sendmail_path = /usr/local/bin/sendmail-fake

(and restart Apache)

That’s it!

You can also see the emails content in /var/log/sendmail-fake.log.

You could even go further and edit the script to open the content of the mail into a text editor, or a browser. But that would be OS dependent, so I kept with the log file here.

Comments