duover-blog-banner.jpg

Setting Up Postfix to Work with MAMP

Posted by: Matt Messmer on Apr 15, 2015 9:31:00 AM in Development


When developing Drupal websites locally, you may want to be able to send emails for testing purposes and have them all get rerouted to you for debugging purposes. At Duo, I’m often working on database dumps from production sites that have many user accounts and I want to make sure that when sending these test emails, none of the actual users receive the test emails.

Gmail Inbox

We use MAMP and I wanted to set it up so that the emails from Drupal would be sent through our Gmail email address but I had some trouble getting Postfix to work in MAMP. I tried many different configurations, but I found that the Postfix settings tab in MAMP did not result in a working configuration. Whenever I run into a challenge like this I start by asking my team if they already have a solution. In this case, I ended up coming up with a solution. The instructions below detail the steps I took to set up Postfix to work with MAMP and includes settings that re-route all emails sent to a single email address for testing.

How to Set Up Postfix to Work With MAMP

These instructions are for a OS X Mavricks enviroment running MAMP 3.0.7.3.

Start by opening Terminal and enter the following command:
open /var/log/mail.log

This opens up mail log in the console to monitor for errors. Open the file /etc/postfix/main.cf in VIM or any text editor.

Add the following lines to end of file:

myorigin = gmail.com
myhostname = smtp.gmail.com
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
inet_protocols = ipv4
smtp_use_tls = yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom
sender_canonical_maps = regexp:/etc/postfix/sender_canonical
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical

Edit file /private/etc/postfix/master.cf and uncomment the line beginning with "tlsmgr" (remove sharp).

Create a sasl_passwd File to Authenticate with Google's Servers

Next we need to create a sasl_passwd file that will be used to authenticate with google’s servers.

From the command line create and edit the file:
sudo nano /etc/postfix/sasl_passwd

Add the following line to the file contents replacing with your gmail username:
[smtp.gmail.com]:587 username@gmail.com:password

Depending on your Google security settings, you probably will require an app specific password to use instead of your normal Gmail password. After editing these files, we need to secure the password file and tell Postfix to update all the configurations.

Run these commands from the command line:

sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo launchctl stop org.postfix.master
sudo launchctl start org.postfix.master
sudo postfix upgrade-configuration

Next, reload Postfix.
sudo postfix reload
sudo postfix flush

To check this is working, try sending a test email.
date | mail -s test username@gmail.com

Setting Up Postfix So All Emails Get Sent to One Email Address

At this point you can send emails to any gmail account, but we can take it one step further and set up postfix so that all emails it sends get sent to one email address. This is useful for testing on the local environment so that test emails are not sent out to real users.

Create file /etc/postfix/sender_canonical
contents:
/.+/ youremail@domain.com

Create file /etc/postfix/recipient_canonical
contents: 
/.+/ youremail@domain.com

Run some more commands.

sudo postmap /etc/postfix/recipient_canonical
sudo postmap /etc/postfix/sender_canonical
sudo postfix upgrade-configuration

Reload postfix again.

sudo postfix reload
sudo postfix flush

Now all emails sent by postfix will be sent to the address you entered in /etc/postfix/recipient_canonical and will be sent from the address entered in /etc/postfix/sender_canonical

Try another test email.
date | mail -s test user@domain.com

No matter what email address we give, the email should get sent to the address in /etc/postfix/sender_canonical

Caution: MAMP will overwrite the changes to main.cf and master.cf if you click the "use this data" button on the Postfix tab.

By following these directions you will be able to have all test emails rerouted to you when working in MAMP. 

This post was updated with new information on June 21, 2016.