Sending Test Emails from Shell

Reading time: 3 minutes

Sending a test email from shell using command line, send mails in shell script is a common task for an email admin. Mails were and still are used to inform admins regarding changes, warnings and problems.

Dependent on the task you want to achieve, some content should be in the body of an email or attached as file. Let’s figure out, how to do it using Linux or Unix command line like bash or zsh.

Requirements

Most systems will have some kind of email command line tool like mail or mutt. For systems with Postfix or Sendmail installed, there is the sendmail command you can use for.

Using MTA (email server) independent tools make it simpler to use, maintain and port shell scripts between systems. For simple tasks I use mail. They are in mailutils on Debian based systems. You can install them by running:

sudo apt install mailutils

If you want to use mutt to send emails, install it too:

sudo apt install mutt

Let’s start with a simple test email

Sending a simple test email to your email is pretty simple:

echo "Test body" | mail -s "Test subject" yourname@yourdomain.com

The body of the email is a simple output using echo command piped | into email command. Subject is defined using -s parameter. The destination email is just added as the last parameter to mail command.

Sending output of a command or text file through email

Sending output of a command like cat and sending it through email is pretty straightforward. In this example I want to have the Linux raid status mailed:

cat /proc/mdstat | mail -s "Linux Raid Status" yourname@yourdomain.com

You can use the same way to send text content of a file through mail:

cat /home/user/somefile.txt | mail -s "somefile.txt content" yourname@yourdomain.com

Sending file as an attachment

Sending bigger text files or binary files should be done as attachments. Especially if these files have an other encoding than the underlying system. To see the difference, let’s get some test image in binary format called test.jpg. This image should be send using command line.

As the mail does not support sending attachments, I will use mutt.

echo "Email with attachment" | mutt -s "See attachment" yourname@yourdomain.com -a test.jpg

ATTENTION: Attach the file with parameter -a test.jpg as last option, or you will see unrelated error messages.

Composing emails using a text file

For special cases it is easier to compose an email in a text file and send it using the file as input. This can be done using sendmail command.

ATTENTION: sendmail command exists on systems with Sendmail orPostfix server installed.

Lets start by creating a sendmail compatible text file:

From: server@yourdomain.com
To: you@yourdomain.com
Subject: This is a subject

This is a body of this email.

The empty line before body text is obligatory to separate email header from body.

From: server@yourdomain is optional. If you don’t set it, the current user name will be used as from address.

Sending this email is now strait forward using sendmail command:

cat test-email.txt | sendmail -t

Summary

Sending emails using command line or shell scripts is a common task for admins. Dependent on the task you can use the simple mail command from mailutils, mutt for attachments or sendmail to send composed emails using a simple text file.


Newsletter


See Also


Tags