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
sudo mkdir /etc/postfix/sasl
sudo vim /etc/postfix/sasl/sasl_passwd
and enter in the following:
[smtp.gmail.com]:587 [email protected]:password
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]
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
echo 'test' | mail -s "contents" [email protected]
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!
Don't need the postmap if relaying through Gmail! Google will use your AUTH to automagically rewrite the sender.
And if the email comes from root, they are nice enough to change the username for you.
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!