Linux – sending mail from command using mailutils

Linux command line tools to send mails are very handy for sending mails from bash scripts. As an example you can send daily php error logs (with some cap) to your email. Here are some ways you can send mail using Linux mail utility (mailutils package) on Ubuntu Linux.

Install mailutils

In case you dont have mailutils installed, install it using apt-get.

$ sudo apt-get install mailutils
$ dpkg -l mailutils
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                        Version            Architecture       Description
+++-===========================-==================-==================-============================================================
ii  mailutils                   1:2.99.98-1.1      amd64              GNU mailutils utilities for handling mail

Sending mail with specific “from email” and subject

To send email with a specific subject and piping mail body content:

$ echo "hello" | mail -aFrom:from@yourdomain.com -s test youremail1@gmail.com,youremail2@gmail.com

Note that -a is used to attach headers and can be repeated multiple times. You can also attach CC: and other headers using it. To pick mail body from a file, use the following command:

$ mail -aFrom:from@yourdomain.com -s test youremail1@gmail.com,youremail2@gmail.com < /path/to/mail/content/file

Sending attachment

To send attachment we'll use -A option.

$ echo "hello" | mail -aFrom:from@yourdomain.com -s test youremail1@gmail.com -A attachmentfilename.txt

Notes on spam

These mails (probably initially) may end up in user's spam folder if the machine has not implemented Sender policy framework. If you have access to receiver's email, you can mark them not spam and later these emails will have higher chances of hitting the inbox.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments