Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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=<username@gmail.com>, 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 username@gmail.com:password

Set up address mapping

Use the generic(5) address mapping to replace local fantasy email (user@host.local) addresses by valid Internet addresses (username@gmail.com). 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:

user@host.domain GMAIL_USERNAME@gmail.com
@host.domain     GMAIL_USERNAME@gmail.com

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" your@yourdomain.com

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!

@mavam
Copy link

mavam commented Jul 10, 2015

and enter in the following:
[smtp.gmail.com]:587 username@gmail.com:password

Do you know whether it's possible to avoid store the password in plaintext? I would prefer to keep it encrypted, e.g., in the OSX keychain and extract it only on-demand via security find-generic-password.

@mrshll
Copy link

mrshll commented Jan 2, 2016

This worked great, thank you!

@dotysan
Copy link

dotysan commented Sep 21, 2017

Don't need the postmap if relaying through Gmail! Google will use your AUTH to automagically rewrite the sender.

From: Curtis Doty <curtis@doty.org>
X-Google-Original-From: curtis@shine.local (Curtis Doty)
Received: by shine.local (Postfix, from userid 501) id 989FA34850C8; Wed, 20 Sep 2017 17:58:09 -0700 (PDT)

And if the email comes from root, they are nice enough to change the username for you.

From: System Administrator <curtis@doty.org>
X-Google-Original-From: root@shine.local (System Administrator)
Received: by shine.local (Postfix, from userid 0) id D4AA534850CE; Wed, 20 Sep 2017 17:58:31 -0700 (PDT)

However... for extra-extra credit, if you do use a sender_canonical or other postmap to rewrite the sender, if it's an alias that Gmail has already verified, they honor that without any X-Google-Original-From header or mangling!

@borch84
Copy link

borch84 commented Dec 17, 2017

Hello, I am not getting my email sent out using the generic template from /etc/postfix/generic.
I am getting the error:
550-Verification failed for user@imac.local 550-Unrouteable
address 550 unable to verify sender address (in reply to RCPT TO command)

I have included these lines inside /etc/postfix/generic:

user@imac.local user@gmail.com
@imac.local user@gmail.com

Then I ran postmap generic and restarted postfix with this commands:

sudo launchctl stop local.org.postfix.master
sudo launchctl start local.org.postfix.master

Also did postfix reload.

But I still get the same 550 message.

Did anyone have such problem?

Thank you.

@alenb
Copy link

alenb commented Apr 1, 2018

Here's what worked for me:

/etc/postfix/main.f

# Gmail SMTP relay
relayhost = [smtp.gmail.com]:587

# Enable SASL authentication in the Postfix SMTP client.
smtpd_sasl_auth_enable = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options =
smtp_sasl_mechanism_filter = AUTH LOGIN

# Enable Transport Layer Security (TLS), i.e. SSL.
smtp_use_tls = yes
smtp_tls_security_level = encrypt
tls_random_source = dev:/dev/urandom

/etc/postfix/sasl_passwd

[smtp.gmail.com]:587 EMAIL@gmail.com:PASSWORD

@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  numbchild@gmail.com
         (connect to smtp.gmail.com[108.177.125.109]:587: Operation timed out)
                                         numbchild@gmail.com
                                         joseph@vidal-rosset.net

A912E8D0FC9C     3025 Sat May 13 17:41:41  numbchild@gmail.com
         (connect to smtp.gmail.com[108.177.125.109]:587: Operation timed out)
                                         numbchild@gmail.com
                                         emacs-orgmode@gnu.org
                                         yantar92@posteo.net

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