Skip to content

Instantly share code, notes, and snippets.

@larrybotha
Last active October 27, 2021 04:28
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save larrybotha/6009971 to your computer and use it in GitHub Desktop.
Save larrybotha/6009971 to your computer and use it in GitHub Desktop.
Postfix Using Gmail SMTP in Mac OSX Mavericks

Configure Postfix for Gmail SMTP in Mac OSX Mavericks

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

Edit file /etc/postfix/main.cf

sudo vim /etc/postfix/main.cf

and add in the following below the commented out relayhosts :-

myhostname = smtp.gmail.com

.
.
.

# SASL SUPPORT FOR SERVERS
#
# The following options set parameters needed by Postfix to enable
# Cyrus-SASL support for authentication of mail servers.
#
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes

Create a sasl_passwd if one doesn't exist

sudo vim /etc/postfix/sasl_passwd

and enter in the following:-

[smtp.gmail.com]:587 username@gmail.com:password

Run the following commands

sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo launchctl stop org.postfix.master
sudo launchctl start org.postfix.master

test by sending the contents of a directory to an email address with the following:

tree /var/www/somefolder | mail -s "contents" your@yourdomain.com

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!

NOTE: A crappy way to get around Google's

Someone just tried to sign in to your Google Account [youremail]@gmail.com from an app that doesn't meet modern security standards.

is to enable less secure apps on your account: https://www.google.com/settings/security/lesssecureapps. Do this at your own discretion.

@lvlaxpt
Copy link

lvlaxpt commented Apr 19, 2017

sudo nano /etc/postfix/main.cf

add below configs

mydomain_fallback = localhost
mail_owner = _postfix
setgid_group = _postdrop
relayhost = smtp.gmail.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
#smtp_tls_security_level = encrypt
#tls_random_source = dev:/dev/urandom
smtp_sasl_mechanism_filter = login

in sasl_passwd

sudo nano /etc/postfix/sasl_passwd

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

then send email

date | mail -s test test@yahoo.com

u will receive email from Google with subject "Review blocked sign-in attempt"
from the email you can activate the less secured apps

it works using macos sierra based on above config

hope it helps, as it took me nearly one hour to get it right.

@ranelpadon
Copy link

Thank you @lvlaxpt. Adding this line: smtp_sasl_mechanism_filter = login in /etc/postfix/main.cf and restarting Postfix to commit the changes, fixed my issue on my macOS Sierra. :)

@hauleth
Copy link

hauleth commented Dec 2, 2017

@lvlaxpt even with your config I am still receiving TLS is required, but our TLS engine is unavailable in mailq.

@capseaslug
Copy link

Nearly 4 hours later and 4am...and @n0mer you just solved my issue. Here is what I am running that seems to work:

myhostname = smtp.gmail.com
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/private/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_sasl_mechanism_filter = plain

@scdn
Copy link

scdn commented Oct 7, 2018

i had issue that postfix is running,but when i send mail ,the username is the my mac username@smtp.gmail.com(i had in the sasl_passwd wrote the gmail username) ,then i got the ERROR 5.1.1,who can tell me how to deal with it

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