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.

@wouterds
Copy link

Hmm, doesn't seem to work for me :-(.
(not using gmail, using my own email hosting with SSL on port 465)

@larrybotha
Copy link
Author

@wouterds ye, I've tried using simple POP email addresses for this configuration and come up empty handed. I've fought with email configurations like a noob for so long I decided to simply settle with using my gmail account :/

Not ideal, but I don't have nightmares about mail configuration anymore!

@wouterds
Copy link

@larrybotha fixed it with my custom postfix/dovecot mailserver with another tut :)

@wealthychef1
Copy link

I've got a maddening error of
Aug 15 14:15:25 RichCookHomeMac postfix/smtp[3191]: warning: SASL authentication failure: No worthy mechs found
Aug 15 14:15:25 RichCookHomeMac postfix/smtp[3191]: AED65298168E: SASL authentication failed; cannot authenticate to server smtp.comcast.net[76.96.40.155]: no mechanism available

I have enabled SASL in the main.cf and cannot figure out this puppy. Anyone able/willing to help? :-D

Copy link

ghost commented Aug 19, 2014

Good question - I'm seeing the same error.

Appropriate entries in main.cf:
mydomain_fallback = localhost
relayhost=smtp.gmail.com:587
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

@Jaace
Copy link

Jaace commented Sep 4, 2014

This isn't working for me either and I was getting an error at first, which went away when I created the /Library/Server/Mail/Data/spool

I ran this to see if it was correct, and it looks correct...but I'm not really sure:

ps ax | grep postfix
 1818   ??  Ss     0:00.02 /usr/libexec/postfix/master

Now it doesn't complain but nothing is sent to my email account. Mail is so frustrating.

@larrybotha
Copy link
Author

I really don't know how to resolve any of your errors :/

@lifereimagined try putting brackets around the gmail subdomain:

relayhost=[smtp.gmail.com]:587

Additionally, I don't have smtp_tls_security_level=encrypt in my main.cf, so that may be something to attempt to remove.

@Jaace, @wealthychef1If you guys come across solutions please post them here - seems like mail on Mac is an issue for many.

@pjotrsavitski
Copy link

Please note that Yosemite (10.10) requires one more configuration for relay host: smtp_sasl_mechanism_filter

Values of plain or login seem to both work.

Please see stack overflow for more information.

@larrybotha
Copy link
Author

@pjotrsavitski thanks for the update - will check it out once I move to Yosemite

@null9beta
Copy link

@pjotrsavitski - wanted to get my postfix up and running again after haven`t used it for some time. I knew that the configuration was working before, your hint made my day

@senorgeno
Copy link

Hi I needed to enable access from "Less secure apps" with google. Did anyone else have to do this? Otherwise can any help, would rather not take this measure. (I am on Yosemite)

Copy link

ghost commented Mar 4, 2015

Not working for me either. I don't care so much about Gmail though, as I am just trying to setup a mock mail server for local tests. Anyone got this working?

@twodayslate
Copy link

Not working for me either. I don't have the option to enable "less secure apps" as I am using google apps for your domain

@marr
Copy link

marr commented Jun 4, 2015

send-mail: fatal: myhostname and relayhost parameter settings must not be identical: smtp.gmail.com

@n0mer
Copy link

n0mer commented Jul 27, 2015

this advice came handy: http://stackoverflow.com/questions/26447316/mac-os-x-10-10-yosemite-postfix-sasl-authentication-failed

It turned out I was missing the following line in /etc/postfix/main.cf:

smtp_sasl_mechanism_filter = plain

@pelly
Copy link

pelly commented Sep 15, 2015

Wanted to echo that the

smtp_sasl_mechanism_filter = login

worked for me on Yosemite (just upgraded)

@lacostenycoder
Copy link

@pelly did that get around the "Set Less Secure" issue in google accounts? I'm still on Mavericks and can only get this to work if I set less secure access, which kinda blows. My work around was to use a different gmail account that I don't care that much about.

@roubles
Copy link

roubles commented Jan 28, 2016

It was starting to get painful to do this on all my macs, so I wrote a small script that takes care of all these settings for you.

@cybercussion
Copy link

cybercussion commented Jul 11, 2016

So quick question - if you run OS X Server with the Mail option, does it also use Postfix? Or do you use one or the other?
I had it working on Mavericks in 2010, but the latest build I just did in El Capitan was giving me some headaches. 'no route to host' or 'relay access denied'.

@HolyGraal
Copy link

this is not working. Close. I made some progress. But I am getting mailer daemons using the totally incorrect details here.
It is trying to send mail from [user]@smtp.gmail.com (where user is my mac username without the brackets)
I definitely setup my sasl_passwd, and I am using the email in the form of [account]@gmail.com:[password]
So, why it trying to send mail using my mac user @ smtp.gmail.com ? I think that may be the issue?? How do I fix this?

@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