Send mails from command line

We can easily send/receive mails from Linux command line: this is really useful when you have to test your mail server or to send messages from shell scripts.

Install mail package

Install the package bsd-mailx or mailutils (I use bsd-mailx, but don't ask me why) with

apt-get install bsd-mailx


apt-get install mailutils

Send mails

Useful parameters for the mail command are:

  • -s subject (The subject of the mail)
  • -c email-address (CC - send a carbon copy to email-address)
  • -b email-address (BCC - send a blind carbon copy to email-address)

For example

mail -s "Hello World"

sends a mail to with subject “Hello World”, while

mail -s "Hello World" -c

sends also a copy of the mail to a second mailbox.

It is possible to specify multiple recipients by joining them with a comma.

mail -s "Hello World",

To set the body of the message, you can pipe it to the mail command

echo "Message body here" | mail -s "Subject here"

or redirect the content of a file

mail -s "Subject here" < /home/user/mail-body.txt

You can also use the output of other tool. To send the system memory usage to the sysadmin, you could try something like

free -h | mail -s "System memory usage"

GTUBE (= spam filter test)

GTUBE is the Generic Test for Unsolicited Bulk Email. If your spam filter supports it, the GTUBE provides a test by which you can verify that the filter is installed correctly and is detecting incoming spam.

You can send yourself a test mail containing the following string of characters (in upper case and with no white spaces and line breaks):


You should send this test mail from an account outside of your network to avoid domain bypass rules.

notes/send-mail-from-cli.txt · Last modified: 2014/09/15 13:42 by admin
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Run by Debian Driven by DokuWiki