User Tools

Site Tools


notes:send-mail-from-cli

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

or

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" user@example.com

sends a mail to user@example.com with subject “Hello World”, while

mail -s "Hello World" -c copyuser@example.com user@example.com

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" user1@example.com,user2@example.com

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

echo "Message body here" | mail -s "Subject here" user@example.com

or redirect the content of a file

mail -s "Subject here" user@example.com < /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" sysadmin@example.com

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):

XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X

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

echo "XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X" | mail -s "GTUBE test mail" user@example.com
notes/send-mail-from-cli.txt · Last modified: 2014/09/15 19:42 by admin