Tuesday, November 4, 2014

Command line mail with Gmail SMTP on Mac OSX 10.10 Yosemite

    

I have been looking for a way to setup a command line mail (or mailx) working on OSX 10.10 (Yosemite) and I found a good blog posting by Anuj Gakhar for OSX Lion.  I elaborated a little bit more by Google search and found a quick fix for OSX Yosemite in http://stackoverflow.com/questions/26447316/mac-os-x-10-10-yosemite-postfix-sasl-authentication-failed.  I have added new information for OSX Yosemite.

This method is especially useful when you do not setup mail host on your Mac.

Here are the steps I followed.
  1. Configure Postfix for SMTP.

  1. Edit file /etc/postfix/main.cf
>> sudo vim /etc/postfix/main.cf

  1. Add in the following below the commented out relayhosts. relayhost can be any SMTP mail service such as [smtp.gmail.com]:587, [smtp.live.com]:587, etc.

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

  1. Add below line at the bottom if you are using OSX 10.10 Yosemite
smtp_sasl_mechanism_filter = plain

  1. Generate sasl_password if not already exists

>> sudo vi /etc/postfix/sasl_passwd

  1. Enter in the following (gmail or other SMTP setup used in 1.a. relayhost):
[smtp.gmail.com]:587 yourusername@gmail.com:yourpassword
  1. 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

  1. All set!  Let’s test.

Now, you should be able to send emails from within the command line.

>> date | mailx -s “date test” email@address.to
            
           email@address.to should have received an email from yourusername@gmail.com with subject "date test".