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.
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.
mailx accepts most configuration in the command line directly from the
-S options in format
-S variable[=value] (check more in
Sets the internal option variable and, in case of a string option, assigns value to it. Note, that when setting from variable, domain name of host is automatically added if value does not contain any. If you want to enter from address with owner’s name, you can use, for example, following format: -S "from=System User
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 \ $TO_EMAIL_ADDRESS
$... above with the value that is actually used.
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 ...
Security notes on passwords
- you may consider not setting
-S smtp-auth-passwordand input it each time you use this command. Passwords in the command line can be seen by other users on the system.
- Gmail now supports "App Password" if you are using Google’s 2-Step verification and you will need to use it instead of your Gmail’s password. You can generate an "App Password" for
mailxso it can be easily revoked in case it leaked out. Go to this URL: https://myaccount.google.com/apppasswords . Click on “Select App” → “Other”, type “mailx”, and hit “Generate”. You will get a password used by
mailx. (thanks to Ben for the notes).
About nssdb used by
Most Linux system’s default nssdb should be already updated so that gmail is good with it. But if your system does not have an updated nssdb, you may consider using Firefox’s instead by adding such option
Find out Firefox’s profile directory in the home directory which commonly 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.
Use mailx 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 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)"
For better security, you may set the mode of
~/.mailrc to 600 by
chmod 600 ~/.mailrc.
The variables set are the same as those used in the "all in one command".
When sending mails, use this command:
$ mailx -v -s "$EMAIL_SUBJECT" $TO_EMAIL_ADDRESS
$ echo "The mail content" | mail -v -s "$EMAIL_SUBJECT" $TO_EMAIL_ADDRESS
Then, time to enjoy it!