Send mail from the command line on Mac OSX

I’ll be using my Gmail account to send mail. The first step is to configure the local Postfix server as a relay. Edit /etc/postfix/main.cf:

sudo vim /etc/postfix/main.cf

Search for a “relayhost=” line and add the following after it:

relayhost = smtp.gmail.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options =
smtp_sasl_local_domain = yourdomain.com
#smtpd_sasl_application_name = smtpd
broken_sasl_auth_clients = yes
smtpd_pw_server_security_options = noanonymous
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

Next create /etc/postfix/sasl_passwd and add the following (replace username and password accordingly):

smtp.gmail.com:587 username@gmail.com:password

Next run the following commands:

$ sudo postmap hash:/etc/postfix/sasl_passwd
$ sudo chown root:wheel /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
$ sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

You should be able to send mail from the command line now:

$ mail -s "Test" username@domain.com

You can check mail’s log file at /var/log/mail.log

To be able to send attachments from the command line (true attachments that graphical mail clients understand) install mutt:

$ curl -O ftp://ftp.mutt.org/mutt/devel/mutt-1.5.20.tar.gz
$ tar xfzvv mutt-1.5.20.tar.gz 
$ cd mutt-1.5.20/
$ ./configure
$ make
$ sudo make install

To send attachments use the following command:

echo "text body" | mutt -s "subject" -a file.dat -- username@domain.com

Or even better:

for i in {1..1000}; do echo "text body" | mutt -s "subject" -a file.dat -- username@domain.com; done

Happy mailing!

Share this post:

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Related posts:
  1. Opening files from the command line on Mac OSX
  2. DNS query to find mail servers
  3. Manage VMware virtual machines from the command line
  4. WordPress Complete Backup Solution
  5. Hotmail in your favourite mail client

2 Comments

  1. kate says:

    Hi, Jorge,

    Thank you for your post, it works absolutely fine. But I have a small question : how to change the name of the sender ? Because when I recieve the test mail in the field “From:” I see the name of the account under which the mail was sent — how to change that ?

    Thank you in advance,
    Kate.

  2. admin says:

    Probably you’ll have to edit Mutt’s configuration, I don’t see any command line switch to specify the “From: ” field. Maybe this is explained in Mutt’s documentation, take a look there. I’m sorry for not being of more help :)

Leave a Reply