Getting Postfix sending email on your Mac

I have recently been setting up my MacBook (running Leopard) to send email using the local email delivery system, i.e. Postfix. This means modifying the default installation to relay email through my ISP/email service – in my case; FastMail. I use FastMail because they have a nice secure email setup with both IMAP and SMTP access encrypted using SSL, but the instructions below will also work with Google Mail. The advantage of having your Apple Mac set up like this is that it enables command line scripts and various unix programs to send email. In my case, it allows me to use Mutt and Emacs as my email client.

The first step is to modify the main.cf file in the /etc/postfix directory. Use your favourite editor to do this, but you will need to sudo in order to modify the file. If you are using vi, type ‘sudo vi /etc/postfix/main.cf’ in a terminal window. Then search for a commented out “relayhost =” line and add the following:

relayhost = [mail.messagingengine.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = 
smtp_sasl_local_domain = yourdomain.com
#smtpd_sasl_application_name = smtpd
broken_sasl_auth_clients = yes
smtpd_pw_server_security_options = noanonymous
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

Now I am relaying my email through the server mail.messagingengine.com and Postfix will attempt to connect to the server on port 587. The square brackets around “mail.messagingengine.com” tell Postfix not to do an MX lookup on the name, but just got to that IP address. You will want to modify these values to suit your ISP/email service. For GMail you can use the line:

relayhost=smtp.gmail.com:587

The next step is to create the file “/etc/postfix/sasl_passwd”. This file contains the information you need to use to authenticate yourself against the relaying mail server (in other words, your GMail username and password). Add the following lines to the /etc/postfix/sasl_passwd file:

[mail.messagingengine.com]:587 *username*:*password*

Or if you use GMail:

smtp.gmail.com:587 *username*@gmail.com:*password*

Remember to replace *username* with your actual username and *password* with your password.

From the Terminal command line, run the following commands:

sudo postmap hash:/etc/postfix/sasl_passwd
sudo chown root:wheel /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

These commands create the “/etc/postfix/sasl_passwd.db” file from the “/etc/postfix/sasl_passwd” file, and then change the permissions on the file so that they can only be read by the root user.

Congratulations! You should now be good-to-go. Try sending an email from the command line by typing “mail -s ‘Test Email’ yourname@yourisp.com” at the command line. Type some words and then type “Ctrl-D” to finish. You can monitor the log file at /var/log/mail.log in order to see what is happening, and use the “mailq” program to see the state of the mail queue.

20 Replies to “Getting Postfix sending email on your Mac”

  1. Hey Brett,

    After a while, I start getting these messages:

    Sep 2 20:42:56 [machinename] postfix/master[10344]: warning: process /usr/libexec/postfix/pickup pid 16520 exit status 1
    Sep 2 20:41:55 [machinename] postfix/master[10344]: warning: /usr/libexec/postfix/pickup: bad command startup — throttling

    Any ideas? I then have to stop and start postfix and reenables deliveries, but I don\’t know what\’s going on.

    I am following your instructions verbatim, but this happens after a few hours.

  2. Hi Mau, I just wrote a small python test that sends myself a large number of emails and I am monitoring the /var/spool/mail.log file. So far I am not getting the error you describe above.

    The problem you are getting seems to be related to Postfix’s pickup process. Have you changed your /etc/postfix/master.cf at all? Also, are you running postfix as a daemon? I am not – postfix starts up for me when I send mail locally and terminates after the last message is sent.

  3. Sorry to take your time, and thanks for looking into this.

    I have not touched the master.cf, the only one I have modified is the main.cf with your suggested updates and by changing the mydomain and myhostname vars within it.

    The only other update I have done was the one described on this link [http://switch.richard5.net/2006/08/19/fatal-open-lock-file-pidmasterpid/] because I was getting thefollowing error: open lock file pid/master.pid unable to set exclusive lock: Resource temporarily unavailable.

    As far as running postfix as a daemon… I don’t think I know how to tell whether I am or not.

    Any ideas?

  4. @mau
    Interestingly, if I run launchctl on my MacBook, I see the org.postfix.master entry. I don’t have it disabled on my machine via the /System/Library/LaunchDaemons/org.postfix.master.plist file.

    If you do a ps aux | grep post, can you see any processes running that belong to the _postfix user?

    The other thing to check would be the permissions in your /var/spool/postfix directory.

    The pickup process is supposed to send messages to the cleanup program for deletion. It is obviously running into problems when trying to do this, but I’m not sure why this is.

  5. Well, what do you know… I just removed the ‘disabled’ key in the org.postfix.master.plist and all seems to be working just fine.

    I restarted my MP and all looks good, I had to recreate the sasl_passwd files to use different smtp credentials and I think I am very close to where I want to be.

    Thanks again, you provided invaluable insight.

    Regards.

  6. I can also see on the log that the postfix service stops itself and starts again when needed.
    I didn’t have to change the permissions on the spool/postfix directory either.

  7. Hi again, I’ve been trying to find any resources pointing out how to change smtp credentials on the fly.

    Right now my server is always sending email as one email account, say, smtpuser@domain1.com.

    If the Mac is hosting several domains, do you know how can I get to do this:

    domain1.com sends email as smtpuser@domain1.com
    domain2.com sends email as smtpuser@domain2.com

    domainX.com sends email as smtpuser@domainX.com

    To take into consideration: all smtpuser@ are part of their own GoogleApps account, so authentication is done via smtp.gmail.com.

    All I need now is that domain1.com uses the related smtp account.

    Any guidance will be greatly appreciated.

  8. @mau Hi Mau, sorry am traveling at the moment and have intermittent internet access. I believe the trick is to use your email client to change who you are sending email from (using the From and Reply-To headers). Will expand more soon – am off to the airport!

  9. Hi Brett, I followed the link you provided and left a question out there, but I don’t think they are listening there anymore… so, based on what they explain on how to do multiple SASL, how do you make a script (PHP for instance) target x or y credential?

    Sorry for bugging you again and for not replying earlier.

  10. @mau Hi Mau, the script sends mail to the postfix server on your local Mac. The Postfix server itself chooses which identity to use to send the email based on the user that’s sending the email. Let me set up an example tomorrow on my own machine, and I’ll post the config I use once I get it all working.

Leave a Reply

Your email address will not be published. Required fields are marked *