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
[xml]
sudo vim /etc/postfix/main.cf[/xml]
and add in the following below the commented out relayhosts :-
[xml]
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
[/xml]
Generate sasl_password if not already exists
[xml]
sudo vim /etc/postfix/sasl_passwd
[/xml]
and enter in the following:-
[xml]
[smtp.gmail.com]:587 username@gmail.com:password
[/xml]
Run the following commands
[xml]
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
[/xml]
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
[xml]
tree /var/www/somefolder | mail -s "contents" your@yourdomain.com
[/xml]
Hope someone finds this useful!
Super!!! Thank you very much!!
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…
Thanks for the tip, Brad.
Finally a solution that works – thanks!
very nice!!
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
Hi Stephane, glad you found it useful. Thanks for stopping by and for the links.
Thank you, these simple instructions are all there is to it!
Thanks for this.
PS, I don’t have tree installed by default (or at least not on the path)
You can install tree using homebrew – brew install tree is the command.
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!
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!
This works thanks! 🙂
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.
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.
Thanks Anuj,
Best tutorial I could find and better yet, works as advertised!
What if you have 2-Step authentication enabled with your gmail account? Any idea how to make it work?
Unfortunately it throws an error on Mountain Lion (postfix: fatal: chdir(/Library/Server/Mail/Data/spool): No such file or directory) – this could be fixed by following the instructions mentioned in this thread: https://discussions.apple.com/thread/4136501?start=0&tstart=0
# mkdir -p /Library/Server/Mail/Data/spool
Then tried to start up:
# /usr/sbin/postfix start
postfix/postfix-script: warning: group or other writable: /Library/Server/Mail/Data/mta
postfix/postfix-script: starting the Postfix mail system
You need to go to google account settings and generate your self an app-specific password. Then use that password in sasl_passwd.
Any way to send attachments from the command line with Lion?
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.
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 !!
[…] 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. […]
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″”
Nice. Worked on Mavericks like a charm!
Thanks, that’s exactly what I was looking for. Works like a charm.
Thanks a lot!
Thanks – worked great on Mavericks! Really appreciate your figuring this out and sharing.
It works! Thanks!
No longer working on Yosemite. Getting SASL authentication error; was working fine with Mavericks.
4 years on a still very useful Thanks!
Could you bring a Mac OS X Yosemite version of your document?
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 🙂
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.
Thanks Mike! That fixed it on Yosemite for me too
[…] is based on: Using MacOSX Lion command line mail with Gmail as SMTP Mac OS X 10.10 Yosemite Postfix SASL authentication […]
[…] given to Anuj Gakhar Although the article was for Lion, it works for Yosemite […]
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
[…] Using MacOSX Lion command line mail with Gmail as SMTP […]