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

The heirloom mailx 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 Firefox’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:


yyyyyyyy 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://
-S smtp-auth-user=$FROM_EMAIL_ADDRESS
-S smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD
-S ssl-verify=ignore
-S nss-config-dir=~/.mozilla/firefox/yyyyyyyy.default/

Replace the $XXX with the value that is actually used. The meaning is obvious. And remember to change yyyyyyyy 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 ...

Use 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/yyyyyyyy.default/
set ssl-verify=ignore
set smtp=smtp://
set smtp-auth=login
set smtp-auth-user=$FROM_EMAIL_ADDRESS
set smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD

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


Then, time to enjoy it!

Eric Zhiqiang Ma

Eric Zhiqiang Ma is interested in operating systems and distributed computing and processing systems. Find Eric on Facebook, Twitter, LinkedIn and Google+. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.


  1. Fedora 13 has a system directory for the network security services (NSS) files.

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

  2. 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.

  3. This was very helpful but why the requirement of the ‘set nss-config-dir=~/.mozilla/firefox/xxxxxxxx.default/’ variable. What is it’s purpose, it seems to work perfectly without it.


  4. @Jim!
    In my Linux box (Fedora 12, mailx -V: 12.4 7/29/08). mailx doesn’t work without ‘set nss-config-dir=…’. I think it depends on the default mailx configuration file (/etc/mail.rc) which may different on different Linux distros. I look into the mail.rc file in my system, there is no ‘nss-config-dir’ configuration. So I need to set it in ~/.mailrc

    In my system, there is a directory /etc/pki/nssdb which can also be used as ‘nss-config-dir’. It works while we get a error message every time:
    Error in certificate: Peer’s certificate issuer is not recognized.

    Only firefox’s directory works perfectly from my experiment. As I have pointed out, it may also work without setting it.

  5. Hey there, I’m sending from a sidux (debian sid) laptop and edited the /etc/nail.rc file (mailx formerly called nail) then symlinked to /etc/mail.rc. File didn’t exist but that may be due to my ensuring sendmail/postfix/etc. were purged from my system.

    Once I my server back up and working (Fedora 12/Amahi)‘set nss-config-dir=~/.mozilla/firefox/xxxxxxxx.default/’ may be of use.

    BTW you’re original article is concise and to the point unlike others I came across so thank you.


  6. I could not find the firefox’s profile directory.. I would be really grateful if someone would help me out…

    1. It is usually under ~/.mozilla/firefox with a name xxxxxxxx.default .

      If you never run Firefox before, this directory may not exist. You can install it and run it once, then the directory should be there.

  7. I am having some problems. The following message comes on the terminal.
    Anyone knows what can be the problem. Does this “mail” command use system proxy settings on its own or i need to set it. Thanks in advance.

    Connecting to . . .Connecting to . . .could not connect: Connection timed out
    “/home/agam/dead.letter” 12/357
    . . . message not sent.

    1. Some command line tools accept proxy setting via environmental variables:

      export http_proxy=http://IP:PORT



      if username:password is needed.

      I am not sure mailx accepts these variables. But it may worth a try.

  8. Thanks, this is great stuff.
    Having done your setup with putting settings in ~/.mailrc on RHEL5.8, I’m trying this:
    #cat ~/mailbody.txt | sendmail -s “SUBJECT”
    I’m using sendmail as I can change the ctladdr/from-address to something else instead of USER@LOCALHOST which allows recipients to reply to the mail.
    I had this working for a short time, but now sendmail seems to parse the command arguments incorrectly. If I add -s “SUBJECT” sendmail will treat SUBJECT, and as recipients all… If I leave out -s “SUBJECT” it will parse the remaining arguments correctly, but with no subject mails are treated as spam.
    Any ideas?

    1. Hi TAG,


      Sendmail is not intended as a user interface routine; other programs pro-
      vide user-friendly front ends; sendmail is used only to deliver pre-for-
      matted messages.

      I am not sure the `sendmail` command you are using will read the configuration from ‘~/.mailrc’.

      I suggest using `mailx` directly. The manual:

      For your command, the `mailx` version may be:

      mailx -s “SUBJECT” -r

      1. Thanks for your reply.

        Well, if I pass -r I get mailx: invalid option — r

        I already looked into man mailx to see if mailx would do this, and found that it didn’t. That’s why I tried sendmail. Using sendmail works – I can send mails this way – it’s just that when using -s it does as descirbed in previous post.

        rpm -qa | grep mailx

        rpm -qa |grep sendmail

        Maybe I need a different version of mailx? This is the one out-of-the-box on RHEL5.8.


        1. Ah.. my fault. You are using an older version of `mailx`. On my box, the mailx is:


          If updating mailx is possible, after installing a newer version of it, you should be able to use ‘-r’.

          1. Well, you couldn’t what version I had running… Thanks, but 8.1.1-44.2.2 seems to be it for RHEL5… RHEL6 has mailx-12 ootb, but I’m stuck with RHEL5 on the box in question for now.
            Might have to let the script run on some other machine with mailx-12, then, and have it ssh into the other box for the non-mail-stuff it needs to do there… Just seems a little foolish to have to build and extra machine for that purpose alone…

          2. Thanks, but I don’t know anything about building. Just tested on a FC14 box. Works like a charm, except for a certificate issuer warning even though I specified nss location as the sending users firefox xxxxxx.default file… The prompt sort of hangs after that, so that might be an issue when scripting. I haven’t started on the actual script yet, as I wanted to get mailing to work first. Did not give warning on RHEL5.8…

  9. Nice tip; working great for me. Thank you! Wish I’d found this 2 years ago when I first tried it; the guide I used did not mention the nss-config-dir parameter, and leaving that out causes authentication errors (think it was MX validation stuff). I believe it worked without that at one time, but not for several years now.

  10. I got a PHP Mail Sender but I want it to use random proxy for sending emails.

    I have API for the proxy

    Change mail.php:


    To email you want to receive test emails.

  11. Notice that mailx has different alternatives. For me this works when using heirloom-mailx but mail.mailutils fails, it does not know the -v switch.

Leave a Reply

Your email address will not be published. Required fields are marked *