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/main.cf

sudo vim /etc/postfix/main.cf

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

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

Generate sasl_password if not already exists

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

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" your@yourdomain.com

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: http://postfix.state-of-mind.de/patrick.koetter/smtpauth/smtp_auth_mailservers.html)

    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
    https://discussions.apple.com/message/15761877#15761877

    Postfix and SMTP AUTH on OS X Lion | Too busy to…
    http://www.toobusyto.org.uk/tooBusy/2011/08/07/postfix-and-smtp-auth-on-os-x-lion/

    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:
    smtp.gmail.com:587 user:pass

    with no [ ] or @gmail.com 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/main.cf, line 2: missing ‘=’ after attribute name: “e yhost = [smtp.gmail.com]: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 – worked great on Mavericks! Really appreciate your figuring this out and sharing.

  23. agudoe2 says:

    It works! Thanks!

  24. George B. says:

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

  25. asdasd says:

    4 years on a still very useful Thanks!

  26. Michael Bühlmann says:

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

  27. 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 🙂

  28. Mike says:

    As found in a more recent Github comment (https://gist.github.com/larrybotha/6009971#comment-1325112), for Yosemite I needed to add “smtp_sasl_mechanism_filter = plain” to the bottom of the other attributes added in main.cf before this would work.

    Also, if you use two-factor, make an App Password for your sasl_passwd file: https://security.google.com/settings/security/apppasswords.

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

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

  31. goutham says:

    postmap: warning: valid_hostname: invalid character 91(decimal): [smpt.gmail.com]:587
    postmap: fatal: file /etc/postfix/main.cf: parameter mydomain: bad parameter value: [smpt.gmail.com]:587

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

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

Leave a Reply

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 292 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: