Sometimes we may need to send mails from localhost for testing purposes. That can be easily done using an external Mail client (Gmail, Hotmail, Yahoo… ETC) and Postfix, which is the standard email server installed by default in Mac OS X (At least in the latest versions). As I said, this is a quick workaround for testing purposes but if security is a requirement, you must buy yourself a certificate at Thawte or Verisign, or create your own one.
This tutorial is about how to configure Postfix for Mac OS X. If you are using windows, you may want to try this approach.
Step 1. Edit Postfix config file
Open a terminal and edit the file main.cf
sudo vi /etc/postfix/main.cf
First check Postfix is configured correctly, look for the following lines (They are probably separated):
mydomain_fallback = localhost mail_owner = _postfix setgid_group = _postdrop
Now add the following lines at the very end of the file:
#Gmail SMTP relayhost=smtp.gmail.com:587 # Enable SASL authentication in the Postfix SMTP client. smtp_sasl_auth_enable=yes smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd smtp_sasl_security_options=noanonymous smtp_sasl_mechanism_filter=plain # Enable Transport Layer Security (TLS), i.e. SSL. smtp_use_tls=yes smtp_tls_security_level=encrypt tls_random_source=dev:/dev/urandom
This is telling Postfix to use a GMAIL SMTP server with Simple Authentication and Security Layer (SASL). Which will be stored in the path “/etc/postfix/sasl_passwd“. You can use any other SMTP provider (Hotmail, Yahoo, ETC…). You only need to know the SMTP host and port. For example for hotmail you should replace the relayhost for the following:
#Hotmail SMTP relayhost=smtp.live.com:587
Or for Yahoo:
#Yahoo SMTP relayhost=smtp.mail.yahoo.com:465
Step 2. Create the sasl_passwd file
We need to create the sasl_passwd file with the SMTP credentials
sudo vi /etc/postfix/sasl_passwd
Write the following content and save:
smtp.gmail.com:587 [email protected]:your_password
Create the Postfix lookup table from the sasl_passwd file.
sudo postmap /etc/postfix/sasl_passwd
This will create the file sasl_passwd.db
Step 3. Restart Postfix
To apply all new changes we have to restart Postfix:
sudo postfix reload
Step 4. Turn on less secure apps (Only Gmail)
In Gmail we must switch on the option “Access for less secure apps“, otherwise we will get the error:
SASL authentication failed
Step 5. Test it!
Let’s send a mail to our own account to be sure everything is working fine:
date | mail -s testing [email protected]
You can check the mail queue and the posible delivery errors using “mailq“
mailq
Use the postfix logs to ensure everything is working as expected:
tail -f /var/log/mail.log
Other useful commands
To clear the mail queue:
sudo postsuper -d ALL
Comments 69
Remove whitespace online 3 and 8. They break things.
Author
Fixed, Thanks Shaun
It worked! THANK YOU
With havin so much content do you ever run into any problems of
plagorism or copyright infringement? My website has a lot of unique
content I’ve either created myself or outsourced but it appears a lot of it is popping it up all over the web without my permission. Do you know any techniques to help reduce content from being stolen? I’d truly
appreciate it.
Author
To be honest I’m not really worried about that. This blog is mainly a reminder for me and I’m not a pioneer in many of the approaches I’m posting.
If someone is taking advantage and copying literally my content.. What can I say? I just hope they include a link to me website 😉
I went through your instructions & I then sent a test email to myself and I got the following error: send-mail: cannot load trust file ~/Programs/Thawte Root Certificates/thawte Premium Server CA/Thawte Premium Server CA.cer: error:00000000:lib(0):func(0):reason(0)
send-mail: could not send mail (account default from /usr/local/etc/msmtprc). Can you please help me in fixing it so that I won’t encounter this error any longer? Feel free to email me if you wish. Thanks.
This works great, thank you!
Took me a while to find the correct settings to work with Rackspace, sharing here in case anyone else is having the same issue.
#Rackspace SMTP
relayhost = smtp.emailsrvr.com:587
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
smtp_sasl_mechanism_filter = AUTH LOGIN
smtp_sasl_security_options =
smtp_use_tls=yes
smtp_tls_security_level=encrypt
Author
Thanks for the contribution
Great, thank you very much.
This is very interesting, You’re a very skilled blogger. I’ve joined your feed and look forward
to seeking more of your excellent post. Also, I have shared your website in my
social networks!
It’s amazing to pay a quick visit this web page and reading the views of all mates concerning this piece of writing, while I am also eager of getting know-how.
I’m having a few problems with this. First of all my main.cf file did not have the following lines:
mail_owner = _postfix
setgid_group = _postdrop
It only had the “mydomain_fallback = localhost” and then some other code under that. So I went ahead and added the two missing lines directly under “mydomain_fallback = localhost”. Then when I restart postfix I get the following:
postfix: fatal: chdir(/Library/Server/Mail/Data/spool): No such file or directory
I’ve checked that the sasl_passwd and sasl_password.db files have been created and they are there but I’m betting that I can’t just simply add the two lines to main.cf as I did? Can you please help me out? Thanks in advance!
This worked very nice. Thanks so much for the tutorial. I found it surprisingly difficult to locate good information on postfix.
Admiring the dedication you put into your blog and detailed information you offer.
It’s great to come across a blog every once in a while that isn’t
the same out of date rehashed information. Fantastic read!
I’ve saved your site and I’m including your RSS feeds to my Google account.
Finally got postfix working, thanks!
Thnks for post. You taught me cool stuff
Thx, it works like a charm
Thanks for the excellent explanation. How would one get Postfix to start automatically on boot ?
When I try to create the Postfix lookup table it says “postmap: fatal: open database /etc/postfix/sasl_passwd.db: Permission denied”
p.s. The problem is that I used BBEdit to create the sasl_passwd file. Permissions on that file must not have been right. When I learned how to use vi and created the sasl_passwd with vi… it all worked.
I had this problem:
# sudo postfix reload
postfix/postfix-script: fatal: the Postfix mail system is not running
# sudo launchctl
launchd% start org.postfix.master
launchd% quit
# sudo postfix reload
postfix/postfix-script: refreshing the Postfix mail system
# sudo mailq
Mail queue is empty
HTH
This came in very handy! The line that worked for me in El Capitan was:
sudo launchctl start org.postfix.master
Then it reloaded successfully!
Following command worked for me. Thanks
sudo launchctl start org.postfix.master
I felt silly once I got this, but it took me a long time. I also got the following error:
postfix/postfix-script: fatal: the Postfix mail system is not running
But I was able to get rid of it by first running:
sudo postfix start
THANK YOU. Perfect. Huge help.
Genial Ander! Muchas gracias por la info 🙂
Nice blog you have here!
If you have Google 2-steps authentication you will also need to create an application specific password for postfix. Then repeat from step 2 of this tutorial using your application specific password instead of you usual Gmail password. Enjoy.
Thanks a lot for your tutorial. This is THE solution that should pop up from the many solutions about this subject on the Web.
Genius!
hey you are awesome! I was struggling for about 5 hours, and finally did it with your tutorial 🙂
Thank you so much !
thanks!!! Worked well after reading 5 other tut’s
Hi! Thanks for the very helpful information. I’m running into this issue, any ideas appreciated:
postfix: warning: /etc/postfix/main.cf, line 692: overriding earlier entry: config_directory=/etc/postfix
/usr/sbin/postconf: warning: /etc/postfix/main.cf, line 692: overriding earlier entry: config_directory=/etc/postfix
/usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: use_sacl_cache=yes
/usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: imap_submit_cred_file=/Library/Server/Mail/Config/postfix/submit.cred
/usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: mydomain_fallback=localhost
postfix/postlog: warning: /etc/postfix/main.cf, line 692: overriding earlier entry: config_directory=/etc/postfix
postfix/postfix-script: fatal: the Postfix mail system is not running
line 692 is the start of the #Hotmail SMTP
I’m afraid that I may have tried to do this a year ago and perhaps tried using a comcast email account, but I don’t see any reference to comcast in the main.cf. I wish I remember what I did!!!! 🙂
On Yosemite I also had to add this to main.cnf:
smtp_sasl_mechanism_filter = plain
Thanks.. Started working on Yosemite after adding this line. Owner should put this in the note.
You should change your nickname to “God”. Thank you, saved my day 😀
I configured /etc/postfix/main.cf
I get the error message:
/usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: mydomain_fallback=localhost
I work with OS X Yosemite.
What do I have to change?
Hi…
Just comment (#) that line and add “noanonymous” (without quote) in this option
smtp_sasl_security_options = noanonymous
It’s work perfectly for me 🙂 ::cheers
thks! it works!
It works perfectly! Thanks 😉
If you get SASL authentication failed; and you are using the right username and pwd for gmail.
Then make sure, you select the option, “Access for less secure app” allowed.
https://www.google.com/settings/security/lesssecureapps
Or add
smtp_sasl_mechanism_filter = plain
to you /etc/postfix/main.cf
Hello,
I have followed the steps in this great article and all went fine until I noticed that my queued mail were not sent. I peek on the log file and noticed these following error message:
“EF049737495: to=, relay=smtp.gmail.com[74.125.68.109]:587, delay=4.7, delays=0.06/0.07/4.6/0, dsn=4.7.0, status=deferred (SASL authentication failed; cannot authenticate to server smtp.gmail.com[74.125.68.109]: generic failure)”
Are there any steps that I missed ?
Looking forward for your kind responses.
Cheers.
On Mac OS X 10.10.2
1. need comment line ‘mydomain_fallback’ and add a value of smtp_sasl_security_options
sudo vi /etc/postfix/main.cf
#mydomain_fallback = localhost
smtp_sasl_security_options=noanonymous
2. start server first before reload
sudo postfix start
Hello,
When I opened a terminal and edit the file main.cf, i couldn’t see the following lines.
mydomain_fallback = localhost
mail_owner = _postfix
setgid_group = _postdrop
So, what should I do next? Should I add these lines? If so, where should I place them?
Looking forward to your kind reply.
Cheers!
When I use mailq, it says
8B44F1E758FE 339 Sat Jun 13 14:15:20 [email protected]
(mail transport unavailable)
[email protected]
but in my passwd file, I used [email protected], I have no idea where does [email protected] comes from, anyone can help?
works fine! tks…
Thank you. These instructions were very helpful. Note that gmail will likely complain about less secure apps trying to access account if using gmail (smtp.gmail.com). Even after changing permissions on gmail to allow less secure apps, still failures. i switched to hotmail (smtp.live.com) keeping the above – plus adding “smtp_sasl_mechanism_filter = plain” to main.cf and everything worked.
Author
Great feedback lads, I have updated the post with the right configuration for OS X El Capitan. Thanks to everyone!
Thanks so much. This is the first time I’ve been able to get mail working with the free version of mamp and osx mavericks!
Your post is great. Helped me setup my mail in Mac! Thanks a ton!!
EN-AshutoshT:pid 11114$ sudo postfix start
postfix/postfix-script: starting the Postfix mail system
postfix/postfix-script: fatal: mail system startup failed
please help..
fatal: open lock file /var/lib/postfix/master.lock: unable to set exclusive lock: Resource temporarily unavailable
in the logs seeing above error
Hi,
I tried this tutorial, because I am having issues with the mail command under OS X yosemite. This was not working. First, when starting the postfix I got a warning:
/usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: mydomain_fallback=localhost
Perhaps not very important, but possible issue.
Later when running a mail command as:
echo “coucou” | mailx -s “subject” [email protected]
I did not received any mail, and in the /var/mail/mail.log I got:
Jan 22 00:22:09 pc1.home postfix/smtp[9897]: CLIENT wrappermode (port smtps/465) is unimplemented
Jan 22 00:22:09 pc1.home postfix/smtp[9897]: instead, send to (port submission/587) with STARTTLS
I have to use the port 465 to working with orange, and it seems postfix, is fixed on port 587, hence orange reject the submission.
Do you have any idea how to fix this ?
Is this coming from a difference between yosemite and el capitan ?
new to mac and using terminals; how do i go about this
Check out this simple guide here at http://www.plentipeppa.com/postfix-on-el-capitan/
It seems you also need to set “myhostname=smtp.gmail.com” in /etc/postfix/main.cf
Also, you can review configuration settings using postfix -n
NOTE:
In OS X 10.11.1 (El Capitan) the command “mydomain_fallback” doesn’t exist.
So is just did not use this command line and continue with the tutorial and everything will work perfectly.
Thank you.
🙂
Me, too.
I emit the line.
Even though, it works.
Thank you!!!!!!!!!!!!!!!!! wow!!
Many thanks! Worked like a charm.
(mydomain_fallback = localhost not a valid option on El Capitan.)
el capitan -I’m getting this error…
F3C3B244064F 341 Sat Apr 23 13:47:46 [email protected]
(delivery temporarily suspended: Host or domain name not found. Name service error for name=smtp.gmail.com type=MX: Host not found, try again)
How can I fix it?
To use smtp.office365.com as relayhost had to add this:
smtp_sasl_mechanism_filter = login, plain
Hello
I’ve edited files as required, and granted access to less secure apps, but I got this error from smtp.gmail.com:
said: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1
Any help ?
Tnx
Roberto
Tried on another pc, it works.
Sorry for my previous msg
Roberto
All, right it is working thanks.
I had trouble that postfix was enabled in MAMP too so couldn’t start it from cmd.
So need disable it in MAMP before doing this manual tweaks.
And one more useful command: postfix flush
Which will clear the mailq, it will resend all the emails so be warned
Cheers.
Hello,
I have tried doing this on a Mac Mini using Sierra, and sadly I am still unable to send an email. I continue to get the “postdrop: warning: mail_queue_enter: create file maildrop/694886.1687: Permission denied” error. I am really unsure of what else to try at this point. Anyone have any pointers? Thanks
Pretty dumb solution.
A primary reason to use Postfix is to *avoid* using Gmail’s smtp servers, to stop Gmail sticking their “on behalf of” message on any mail … and scrubbing the private data on the emails.
Postfix is an SMTP server, why relay through theirs?
Still works, thanks! It does the trick even 5 years later and that’s about all I need.
Awesome, Thanks for providing step by step process, Its very clear. +++++++
I dont see the lines in BigSur. Do we need to add it ?
mydomain_fallback = localhost
mail_owner = _postfix
setgid_group = _postdrop