Using MacOSX Lion command line mail with Gmail as SMTP

On December 9, 2011, in Mac, by Anuj Gakhar

I have been trying to get my command line mail working on my MacOS (Lion) today and I noticed that the normal postfix emails get treated as SPAM by Google and because I was sending emails to myself on my gmail account, that was an issue for me. So, I thought of using gmail as my outgoing SMTP server for this. Here are the steps I followed :-

Configure Postfix for Gmail SMTP

Edit file /etc/postfix/
sudo vim /etc/postfix/[/xml]

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

relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes

Generate sasl_password if not already exists

sudo vim /etc/postfix/sasl_passwd

and enter in the following:-

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

And you are done….

Now, you should be able to send emails from within the command line e.g. to send the contents of a directory as a tree to an email address

tree /var/www/somefolder | mail -s "contents"

Hope someone finds this useful!

Tagged with:  

39 Responses to Using MacOSX Lion command line mail with Gmail as SMTP

  1. amok says:

    Super!!! Thank you very much!!

  2. Brad Edelman says:

    Super helpful. Thanks! I’d followed these steps once before and they worked perfectly.

    I was just setting this up on a new Mac Mini and had to modify one thing. Not sure why, but the sasl_passwd file didn’t work until I made it into a “db” file e.g.:

    postmap hash:/etc/postfix/sasl_passwd

    (this was info I found in a similar article:

    Thought I’d share, as it was very confusing, and all the “here’s why your gmail smtp authentication is failing” articles that I found were red herrings. It simply wasn’t actually trying my credentials…

  3. Matt says:

    Finally a solution that works – thanks!

  4. grantdoug says:

    very nice!!

  5. Hey Anuj,

    +1 on finding it useful!!! This is the last part of information I needed to complete my postfix setup.

    For the other googlers out there, these are the two other posts that helped me setup postfix properly on Lion :

    Local Mail Server for Lion: Apple Support Communities

    Postfix and SMTP AUTH on OS X Lion | Too busy to…

    Thanks from Montreal!

    — Stéphane

  6. Guilherme Pim says:

    Thank you, these simple instructions are all there is to it!

  7. John Hinnegan says:

    Thanks for this.

    PS, I don’t have tree installed by default (or at least not on the path)

  8. Ken says:

    Thanks for a very helpful answer that just saved me $59 on MAMP Pro, which claimed to save me the “bag of hurt” of configuring Postfix. This worked, while theirs didn’t.

    I would, however, encourage you to correct the four commands you show after updating the password file. Brad Edelman is right that you need to create a hash, and if you do that after running those four commands, it still won’t work, until you stop and start the service again.

    But thanks again, this was very clear and very helpful. A big bag of help!

  9. Gilles Doge says:

    Thanks for this blog post.

    I use the local mail server to send mail in PHP via SMTP and having connection error.
    I fix this by following this instruction to create the file /etc/postfix/submit.cred.

    Work like a charm now!

  10. rock says:

    This works thanks! 🙂

  11. Peter R. Phillips says:

    I am running Lion on a MacBook Pro. I can now run sendmail successfully when I am using the computer normally. But there are times when I want the computer to wake up and send an email while I have the lid closed. sendmail does not work in this situation. But the message gets stored somewhere, and is automatically sent as soon as I open the lid.

    Is there a software fix for this? There is a simple hardware fix, of course; just use a spacer to keep the lid from closing all the way; but I find this inelegant.

  12. John Staško says:

    You rock! This works great on snow leopard! I only wish there were one more thing- that I could select between two different personae- one being my work email and the other being my personal.

  13. Bretticus says:

    Thanks Anuj,

    Best tutorial I could find and better yet, works as advertised!

  14. Jay Khimani says:

    What if you have 2-Step authentication enabled with your gmail account? Any idea how to make it work?

  15. Mark Bragg says:

    Any way to send attachments from the command line with Lion?

  16. sverre says:

    This works,as long as I run mail manually from terminal, or use this in a script which I run from terminal. However when launchd runs a script, this mail never worked so far for me. I am using Mountain Lion.

  17. foo central says:

    hi there.. not sure why mine was different but the syntax in the /etc/postfix/sasl_passwd file had to be: user:pass

    with no [ ] or as given in the blog above. other than that its a goer cheers !!

  18. […] your are a Mac user like me and need help to configure Postfix to send the mail, here is a great post where the author explain how to do it. […]

  19. Phil M says:

    How do I work around this?

    “send-mail: fatal: /etc/postfix/, line 2: missing ‘=’ after attribute name: “e yhost = []:587″”

  20. Dmitri Moore says:

    Nice. Worked on Mavericks like a charm!

  21. oserver says:

    Thanks, that’s exactly what I was looking for. Works like a charm.

  22. Thanks a lot!

  23. Thanks – worked great on Mavericks! Really appreciate your figuring this out and sharing.

  24. agudoe2 says:

    It works! Thanks!

  25. George B. says:

    No longer working on Yosemite. Getting SASL authentication error; was working fine with Mavericks.

  26. asdasd says:

    4 years on a still very useful Thanks!

  27. Michael Bühlmann says:

    Could you bring a Mac OS X Yosemite version of your document?

  28. zma0011 says:

    Thanks it worked!! and now I can send my dynamic public ip change notification and don’t need to use dyn or noip service. The use cases are endless 🙂

  29. Mike says:

    As found in a more recent Github comment (, for Yosemite I needed to add “smtp_sasl_mechanism_filter = plain” to the bottom of the other attributes added in before this would work.

    Also, if you use two-factor, make an App Password for your sasl_passwd file:

  30. […] is based on: Using MacOSX Lion command line mail with Gmail as SMTP Mac OS X 10.10 Yosemite Postfix SASL authentication […]

  31. […] given to Anuj Gakhar Although the article was for Lion, it works for Yosemite […]

  32. goutham says:

    postmap: warning: valid_hostname: invalid character 91(decimal): []:587
    postmap: fatal: file /etc/postfix/ parameter mydomain: bad parameter value: []:587

    ^ getting this error if i execute the postmap command. Please help, i m trying to learn to scripting. Thanks in advance

  33. […] Using MacOSX Lion command line mail with Gmail as SMTP […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar