NOTE : This post was sitting in my drafts for way too long (over 7 months now), so I'll just post it in the hopes someone likes it. There could be rough edges here and there, but hey, that's what the contact page is for... ;)

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.

Install MockSMTP (via the AppStore or download it from their website)

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,1025 ip from any to any 25 in

Edit the Postfix file from the postfix configuration folder:

sudo /etc/postfix/nano

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

(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

Start the MockSMTP application and make sure it is listening on port 1025 (see the status in the bottom of the screen). MockSMTP Status

Now try to send a testmail:

echo "This is the body of a text test-mail" | mail -s "Email test"

And all went well, we now have a testmail in our MockSMTP app. MockSMTP mailmessage

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...

Previous Article Next Article