Send Email from the Command Line using Gmail Account

Sometimes you want to send a quick/short E-mail to somebody, send a TODO item to yourself, or let a script/program Email you when it’s done. For these I found the Linux mail command very useful.

Using it is as easy as typing:

mail -s “Subject” receipt@domain.com

Then enter the message and end it with [ctrl]+[D].

However, if your ISP s!#$% like mine, you’ll discover that no mail was really sent.

To work around this, I wanted to configure my mail program to use Gmail’s SMTP server.

After a little Googling I found this howto. (Tested on Ubuntu).

1. First, you will need the postfix package:

sudo apt-get install postfix

2. Now configure it: Append the following to /etc/postfix/main.cf  (you need super user privileges):

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_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

3. Next, add your username and password to /etc/postfix/sasl_passwd (create the file if it isn’t already)

[smtp.gmail.com]:587      user.name@gmail.com:password

4. Change permissions of sasl_passwd:

sudo chmod 400 /etc/postfix/sasl_passwd

5. Translate sasl_passwd into “db” file so postfix will be able to read:

sudo postmap /etc/postfix/sasl_passwd

6. Create a certificate:

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

7. Restart postfix:

sudo postfix stop

sudo postfix start

8. Try it:

mail -s “Test” my.email@domain.com

(Then enter the message if any, and terminate with [ctrl]+[D])

That’s it. You’re done.


If you enjoyed this post, make sure you subscribe to my RSS feed!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

No related posts.

Tags: , , ,

Saturday, March 21st, 2009 Command Line

4 Comments to Send Email from the Command Line using Gmail Account

  • nareman abd el hade says:

    hey…slam…mr7ba
    ana…nareman bt3lam b el 7ashmal btw2a3 enak bt3rfne b el mnzar konet ma5de m3ak course mvne m7ashvem w esa sadot bde as2lak akam sha3′le b el mvne m7ashvem drore
    so b7yatk eza momken teb3atle el mail tb3ak aw nomret tlfonak

  • jimjams says:

    Thanks for this; most straightforward instructions I’ve found.

  • a Human says:

    Down with Israel

  • Leave a Reply

    my email
    my photo
    Hi,
    My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and a BSc. in electrical engineering from The Electrical Engineering school , Technion , Israel.
    I'm a Verification Engineer in Mellanox Technologies Ltd.
    I love Linux, the Command Line and the OpenSource Community.
    I used to write Poems (Arabic) when I was able to find time for this.
    March 2009
    S M T W T F S
    « Feb   May »
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  
    SEO Powered by Platinum SEO from Techblissonline

    Twitter links powered by Tweet This v1.7.4, a WordPress plugin for Twitter.