Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joech4n/d59e7bc9f99273fbda31 to your computer and use it in GitHub Desktop.
Save joech4n/d59e7bc9f99273fbda31 to your computer and use it in GitHub Desktop.
Configure Postfix for Gmail SMTP in Mac OSX

Configure Postfix for Gmail SMTP in Mac OSX

This should work on at least:

  • 10.9 Mavericks
  • 10.10 Yosemite

Taken from Using MacOSX Lion command line mail with Gmail as SMTP

Edit file /etc/postfix/main.cf and add this to the bottom:

# Configure Postfix for Gmail SMTP in Mac OSX Yosemite
# Added per https://gist.github.com/joech4n/72108461bfac1bf2e99f
# Set the relayhost to the Gmail Server.  Replace with your SMTP server as needed
relayhost = [smtp.gmail.com]:587
# Postfix 2.2 uses the generic(5) address mapping to replace local fantasy email
# addresses by valid Internet addresses. This mapping happens ONLY when mail
# leaves the machine; not when you send mail between users on the same machine.
smtp_generic_maps = hash:/etc/postfix/generic

# These settings (along with the relayhost setting above) will make
# postfix relay all outbound non-local email via Gmail using an
# authenticated TLS/SASL session.
smtp_tls_loglevel=1
smtp_tls_security_level=encrypt
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl/sasl_passwd
smtp_sasl_security_options = noanonymous

# To fix these errors per http://askubuntu.com/q/73865:
# Dec 15 17:14:12 localhost.local postfix/smtp[3691]: Untrusted TLS connection established to smtp.gmail.com[74.125.28.108]:587: TLSv1 with cipher RC4-SHA (128/128 bits)
smtp_tls_CApath = /usr/local/etc/openssl/certs
smtp_tls_CAfile = /usr/local/etc/openssl/cert.pem

# To fix these errors per http://stackoverflow.com/q/26447316:
# Dec 15 17:46:51 heimerdinger.local postfix/smtp[4758]: C9682156786: to=<[email protected]>, relay=smtp.gmail.com[74.125.28.108]:587, delay=1.3, delays=0.77/0.11/0.42/0, dsn=4.7.0, status=deferred (SASL authentication failed; cannot authenticate to server smtp.gmail.com[74.125.28.108]: generic failure)
smtp_sasl_mechanism_filter = plain

Create a sasl_passwd if one doesn't exist

sudo mkdir /etc/postfix/sasl
sudo vim /etc/postfix/sasl/sasl_passwd

and enter in the following:

[smtp.gmail.com]:587 [email protected]:password

Set up address mapping

Use the generic(5) address mapping to replace local fantasy email ([email protected]) addresses by valid Internet addresses ([email protected]). This mapping happens ONLY when mail leaves the machine; not when you send mail between users on the same machine. Set this up by editing /etc/postfix/generic.

sudo vi /etc/postfix/generic

and add the following (only think you need to replace is GMAIL_USERNAME:

[email protected] [email protected]
@host.domain     [email protected]

Protect credentials, create Postfix files, and restart Postfix

sudo chmod -R 600 /etc/postfix/sasl
sudo postmap /etc/postfix/sasl/sasl_passwd
sudo postmap /etc/postfix/generic
sudo launchctl stop org.postfix.master
sudo launchctl start org.postfix.master

Testing

echo 'test' | mail -s "contents" [email protected]

Errors?

If you receive the following error:

send-mail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory

you can do the following:

sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions
sudo /usr/sbin/postfix start

as per this question.

NB: If things aint sending / receiving, and you're getting notices, check that the mail servers you're using are actually working!

@mingliuboy
Copy link

It's a great tutorial, and I've learned a lot from it

@stardiviner
Copy link

I got connection time out problem for sending email when I execute command mailq.

Here is the command output:

-Queue ID-  --Size-- ----Arrival Time---- -Sender/Recipient-------
8CF038D0FBDB     2915 Sat May 13 17:39:13  [email protected]
         (connect to smtp.gmail.com[108.177.125.109]:587: Operation timed out)
                                         [email protected]
                                         [email protected]

A912E8D0FC9C     3025 Sat May 13 17:41:41  [email protected]
         (connect to smtp.gmail.com[108.177.125.109]:587: Operation timed out)
                                         [email protected]
                                         [email protected]
                                         [email protected]

-- 5 Kbytes in 2 Requests.

I'm in China, maybe network can't access Gmail SMTP server. I'm not sure this is the reason.
Or is there a way to configure proxy for postfix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment