Mailx - Easy emailing from the terminal

If one wishes to get an email from a script, it would be nice if there were a simple way to send emails from the terminal. Luckily, there is a simple way. "Heirloom-mailx" is by far the easiest way to make this happen.

I primarily use Ubuntu Server, to install just type

sudo apt-get install heirloom-mailx

With mailx installed, you need to create and edit a new dot-file. Namely, .mailrc (if you already have one, then this might be a good time to take a backup of said file).

# ~/.mailrc
set smtp-use-starttls
set nss-config-dir=~/.mozilla/firefox/XXXXXX.default/
set ssl-verify=ignore
set smtp=smtp://smptpserver.domainname:SMTPPort
set smtp-auth=login
set smtp-auth-user="user@server.domainname"
set smtp-auth-password="password"
set from="user@server.domainname(Firstname Lastname)"

In this example, I've assumed that you're using a TLS/SSL smtp-service with login, which reads from firefox user folder in order to read certificates. In order to customize above to your needs, first find out what your firefox-profile folder is called and replace XXXXXX with the correct value. Second, replace the smtp-server info with the one for your needs. Then fill in the email-address you wish to send from with the correct password. Finally, make up a better name to be shown as the sender than "Firstname Lastname".

If you correctly managed to create the .mailrc and saved it to your homefolder, you should be able to send an email with

echo "Message" | mailx -v -s "Subject" recipient@otherDomain.domainname

If something goes wrong, the output of mailx will probably let you know since it is invoked with the verbose (-v) flag. Happy script-mailing!