Because I tend to do some PHP development on my MacBookPro runnin OSX 10.7(Lion) and want to be able to test the email-functionality, without the system actually sending email to people, I was on the lookout for an easy way to accomplish this in such a way, that I would be able to collect all mail and display both text and HTML versions of it. I found MockSMTP, and it works like a charm. In this post we’ll set it up and configure the system to route all mail to MockSMTP, even when it’s not running.
Open the terminal and enter the following command to reroute all traffic on port 25 to port 1025 on the local system.
sudo ipfw add 1025 forward 127.0.0.1,1025 ip from any to any 25 in
Edit the Postfix main.cf file from the postfix configuration folder:
sudo /etc/postfix/nano main.cf
Add the following at the end of the file:
relayhost = localhost:1025
Make sure the line reading
"mydomain_fallback = localhost" is witten as:
"mydomain_fallback=localhost" so without the spaces, to prevent any runtime warnings.
In OSX Lion a new version of postfix is installed, so we need an extra file to deliver mail:
sudo touch /etc/postfix/submit.cred sudo nano /etc/postfix/submit.cred
Add the following lines:
submitcred version 1 localhost|username|password
(Note: the username and password can be used literally here, they don’t need to match your account credentials!)
Check the maillog in a new terminal tab/window. This monitors new lines written to the logfile. This way you can troubleshoot and see if messages are sent correctly. (Ctrl+C to stop)
tail -f /var/log/mail.log
Start Postfix and make sure it will restart on system boot: enter in the terminal:
sudo launchctl load -w /System/Library/LaunchDaemons/org.postfix.master.plist
Afterwards to stop or start the service use:
sudo launchctl unload /System/Library/LaunchDaemons/org.postfix.master.plist sudo launchctl unload /System/Library/LaunchDaemons/org.postfix.master.plist
Now try to send a testmail:
echo "This is the body of a text test-mail" | mail -s "Email test" firstname.lastname@example.org
Because we used the IP Port Forward, mail sent by any application that tries to use the local SMTP server (localhost:25) or sendmail/postfix will be rerouted to MockSMTP. If the MockSMTP app is not running, mail will be spooled and will be delivered when you run the app again.
Have fun developing…