Sending Email from mailx Command in Linux Using Gmail’s SMTP
The 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/yyyyyyyy.defaultyyyyyyyy 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 $TOEMAILADDRESSmailx -v -s "$EMAILSUBJECT" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://smtp.gmail.com:587 -S from="$FROMEMAILADDRESS($FRIENDLYNAME)" -S smtp-auth-user=$FROMEMAILADDRESS -S smtp-auth-password=$EMAILACCOUNTPASSWORD -S ssl-verify=ignore -S nss-config-dir=~/.mozilla/firefox/yyyyyyyy.default/ $TOEMAILADDRESSReplace 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 ~/.mailrcset smtp-use-starttls set nss-config-dir=~/.mozilla/firefox/yyyyyyyy.default/ set ssl-verify=ignore set smtp=smtp://smtp.gmail.com:587 set smtp-auth=login set smtp-auth-user=$FROMEMAILADDRESS set smtp-auth-password=$EMAILACCOUNTPASSWORD set from="$FROMEMAILADDRESS($FRIENDLYNAME)"Change the $YYYY and yyyyyyyy to the right value for you. When sending mails, use this command:
$ mailx -v -s "$EMAILSUBJECT" $TOEMAILADDRESSThen, time to enjoy it!
Tags: Bash, Client config, Command line, Fedora, google, mail command, mail content, mozilla firefox, Server config, smtp auth, smtp server, Tutorial
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.
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” -fmail@domain.com mail@domain.com
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, -fmail@domain.com and mail@domain.com 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?
Thanks
Hi TAG,
From: http://www.sendmail.org/~ca/email/man/sendmail.html
I am not sure the `sendmail` command you are using will read the configuration from ‘~/.mailrc’.
I suggest using `mailx` directly. The manual:
http://www.pkill.info/linux/man/1-Mail/.
For your command, the `mailx` version may be:
mailx -s “SUBJECT” -r mail@domain.com mail@domain.com
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
mailx-8.1.1-44.2.2
rpm -qa |grep sendmail
sendmail-8.13.8.8.el5
Maybe I need a different version of mailx? This is the one out-of-the-box on RHEL5.8.
Thanks.
Ah.. my fault. You are using an older version of `mailx`. On my box, the mailx is:
mailx-12.5-6.fc17.x86_64
If updating mailx is possible, after installing a newer version of it, you should be able to use ‘-r’.
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…
If you like, building mailx by yourself may be an option.
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…
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 173.194.79.108:587 . . .Connecting to 173.194.79.109:587 . . .could not connect: Connection timed out
“/home/agam/dead.letter” 12/357
. . . message not sent.
Some command line tools accept proxy setting via environmental variables:
export http_proxy=http://IP:PORT
and
proxy_username=USERNAME
proxy_password=PASSWORD
if username:password is needed.
I am not sure mailx accepts these variables. But it may worth a try.
It didn’t work. It does look like mailx doesn’t work with proxy. Anyway, thanks for the reply.
Thanks. This really helped me.
Cheers.
Here’s a version wrapped in a function that asks for your password each time (avoid storing it on the disk):
https://gist.github.com/2307654
I could not find the firefox’s profile directory.. I would be really grateful if someone would help me out…
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.
I was looking for this one, you saved me a lot of time!
Thank you!!!
mailx have no option ‘-S’ (ubuntu 10.10) :(
That’s strange. I find from Ubuntu’s site the mailx has -S option. Please try again and check whether the package you are using is right.
we find answer. two mailx exists. heirloom-mailx have -S option, but mailutil’s mailx haven’t :)
@ Frank,
For sending binary attachments: using the “-a file” parameter: http://www.pkill.info/linux/man/1-mailx/#lbAD
You may need to write html by yourself if you want to use HTML: http://en.wikipedia.org/wiki/HTML_e-mail Or I think you can try sending html as an attachment.
Hi,
and how can I send html and binary attachments with mailx ?
regards
Worked like a charm. Thanks.
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.
Jim!
@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.
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.
Jim!
Very interesting .. thanks you
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.
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.