Home » Linux

Sending Email from mailx Command in Linux Using Gmail’s Smtp

mailx or mail command in Linux is still providing service for guys like me, especially when we need to send email automatically by script. gmail is great. Now, how to use gmail’s smtp in mailx/mail? gmail is a little special since gmail’s smtp server requires tls authorization. The good news is that mailx supports it. Let’s look at how to use it.

First, find out Fixforx’s profile directory in the home directory (I believe most of the users on Linux use Firefox. If you are not using Firefox, what you need to do is try it ;) . It has a format like this:

~/.mozilla/firefox/xxxxxxxx.default

xxxxxxxx is a random string that’s different for different users. You can easily find it out by looking into the directory ~/.mozilla/firefox.

There are two ways to do this: using all-in-one command or putting configurations into profile. The all-in-one-command way needs no other configurations except the command line itself, while the way using configuration has a clearer command.

All-in-one command

This is an all-in-one command that sends email to $TO_EMAIL_ADDRESS

mailx -v -s "$EMAIL_SUBJECT" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://smtp.gmail.com:587 \
-S from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)" \
-S smtp-auth-user=$FROM_EMAIL_ADDRESS \
-S smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD \
-S ssl-verify=ignore \
-S nss-config-dir=~/.mozilla/firefox/xxxxxxxx.default/ \
$TO_EMAIL_ADDRESS

Replace the $XXX with the value that is actually used. The meaning is obvious. And remember to change xxxxxxxx to the string that’s part of the Firefox profile directory.

This command will ask for the email content. Type in the mail content and after finishing the email, use “Ctrl+d” to tell mailx you have finished. Then this mail will be sent out through gmail’s smtp server. You can also use pipe like this:

echo "The mail content" | mail -v -s ...

Using configuration file

There are too many options in the above command? Yes… I must confess so. We can write most of them into mailx/mail’s configuration file ~/.mailrc

set smtp-use-starttls
set nss-config-dir=~/.mozilla/firefox/xxxxxxxx.default/
set ssl-verify=ignore
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=$FROM_EMAIL_ADDRESS
set smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD
set from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)"

Change the $XXX and xxxxxxx to the right value for you. When sending mails, use this command:

mailx -v -s "$EMAIL_SUBJECT" $TO_EMAIL_ADDRESS

Then, time to enjoy it!

Updated history
Mar. 11 2010
Apr. 29 2010. Add title.
Jul. 12, 2010. Revise the article.
Jul. 26, 2010. Add highlight colour to pre tags.

Read more:

Digg del.icio.us Stumble Techorati Facebook Newsvine Reddit Twitter
Mixx LinkedIn Google Bookmark Yahoo Bookmark MySpace LiveJournal Blogger RSS feed
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

4 Comments »

  • Adam said:

    Fedora 13 has a system directory for the network security services (NSS) files.
    nss-config-dir=/etc/pki/nssdb/

    Ubuntu 10.4 doesn’t seem to have a system directory for NSS files.

  • Eric (author) said:

    Hi Adam,

    Yes. Some systems do not have the /etc/pki/nssdb/ directory. As far as I now, the nss files in Fedora do not work well with gmail.

    So we use Firefox’s version of these nss files which supports gmail quite well.

  • Setting up Git Commit Email Notification | Pkill.info said:

    [...] This script use smtp server to send email. You need to change the smtp server address at the end of the script. Simply change smtp=”smtp://smtp.cse.ust.hk” to the smtp server you use. You can not use my smtp server which is only allowed to send email without authorization inside of CSD in HKUST. If you like to use Gmail’s smtp, please refer to Sending Email from mailx Command on Linux Using Gmail’s Smtp. [...]

  • Adam said:

    Very interesting .. thanks you

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.