Postfix, Gmail and Mountain Lion Server

What a headache it’s been to get my mail server up and running — consistently — on Mac OS X 10.8! I guess the powers-that-be at Apple felt that it’d be a good idea to shift from /etc/postfix/ to /Library/Server/Mail/Config/postfix/ because… I don’t know, it has a proprietary ring to it perhaps? Who knows? Whatever the case it looks as though the transition was only partially complete, in that not all the paths were written and not all the directories were made.

If you’re like me and you’ve tried to strong arm control back over to /etc/postix then you’ve likely been just as frustrated that every time Apple updates the Server.app or serveradmin CLI (now burred deep within the Server.app, worth finding and adding to your $PATH) then all your s#!t breaks and you have to go back and do it again. Yeah for Apple Draconianism.

Since I can’t beat them, I might as well join them. So here are the steps to get Gmail piping out messages a la mail/sendmail/postfix from your command line with Mountain Lion 10.8, migrating from configurations in /etc/postfix/ to /Library/Server/Mail/Config/postfix/.

Assumptions

I assume a few things here:

  1. You have Server.app installed on your Mac.
  2. You’re comfortable in the command line.
  3. serveradmin have been buried recently. I suggest you add /Applications/Server.app/Contents/ServerRoot/usr/sbin to your $PATH.
  4. When I say “edit such-an-such file”, I assume that you have sudoer privileges and and an editor (VIM, Nano, TextMate, Sublime Text 2, etc…) that can edit these system files. In all of these examples I use subl for SublimeText, so substitute your favorite editor there.
  5. Backup, backup, backup. Don’t edit any of these files without making a backup copy first.
  6. Since you’re here, let’s assume that you already know why you want to do this and how to use command-line mail.

How to

  1. Stop the mail and postfix services:

    $ sudo serveradmin stop mail
    $ sudo postfix stop
  2. Let’s make sure you have the spool directory (one of those folders not originally created with the original release of the OS X 10.8 Server.app):

    $ sudo mkdir -p /Library/Server/Mail/Data/spool
  3. Open the postfix launch daemon:

    $ sudo subl /System/Library/LaunchDaemons/org.postfix.master.plist
  4. Replace the plist contents with:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
        <key>AbandonProcessGroup</key>
        <true/>
        <key>Label</key>
        <string>org.postfix.master</string>
        <key>OnDemand</key>
        <false/>
        <key>Program</key>
        <string>/usr/libexec/postfix/master</string>
        <key>ProgramArguments</key>
        <array>
                <string>master</string>
        </array>
        <key>QueueDirectories</key>
        <array>
                <string>/Library/Server/Mail/Data/spool/maildrop</string>
        </array>
        </dict>
    </plist>
  5. Unload/reload the plist:

    $ sudo launchctl unload /System/Library/LaunchDaemons/org.postfix.master.plist
    $ sudo launchctl load /System/Library/LaunchDaemons/org.postfix.master.plist
  6. Open the main.cf:

    $ sudo subl /Library/Server/Mail/Config/postfix/main.cf
  7. Find and replace all instances of /etc/postfix/ with /Library/Server/Mail/Config/postfix/

  8. Add the following lines to the bottom:

    # my main.cf edits
    relayhost = [smtp.gmail.com]:587
    smtp_generic_maps = hash:/Library/Server/Mail/Config/postfix/generic
    smtp_tls_loglevel=1
    smtp_tls_security_level=encrypt
    smtp_sasl_auth_enable=yes
    smtp_sasl_password_maps=hash:/Library/Server/Mail/Config/postfix/sasl/passwd
    smtp_sasl_security_options = noanonymous
    smtp_use_tls = yes
  9. Find out who you are:

    $ whoami
  10. Open the aliases file:

    $ sudo subl /Library/Server/Mail/Config/postfix/aliases
  11. Find the line #root: you, uncomment it and replace “you” with the results of whoami:

    # Person who should get root's mail. Don't receive mail as root!
    root: adam 
  12. Initialize the alias database:

    $ sudo newaliases
  13. Find out your machine name:

    $ hostname
  14. Open the generic file:

    $ sudo subl /Library/Server/Mail/Config/postfix/generic
  15. Add these lines to the end (with your credentials in place of whoami and hostname):

    # my generic edits
    whoami@hostname youremail@gmail.com
    @hostname youremail@gmail.com
  16. Create a password file:

    $ sudo mkdir -p /Library/Server/Mail/Config/postfix/sasl
    $ sudo subl /Library/Server/Mail/Config/postfix/sasl/passwd
  17. Add the following (with your credentials):

    [smtp.gmail.com]:587 youremail@gmail.com:yourpassword
  18. Set postfix permissions:

    $ sudo postfix set-permissions
    $ sudo postmap /Library/Server/Mail/Config/postfix/sasl/passwd
  19. Start the mail services:

    $ sudo postfix start
    $ sudo serveradmin start mail
 

Adam Merrifield

 

Leave a Reply