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.
Most systems will have some kind of email command line tool like
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
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" firstname.lastname@example.org
The body of the email is a simple output using
echo command piped
email command. Subject is defined using
-s parameter. The destination email is
just added as the last parameter to
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" email@example.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" firstname.lastname@example.org
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.
echo "Email with attachment" | mutt -s "See attachment" email@example.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 exists on systems with
orPostfix server installed.
Lets start by creating a
sendmail compatible text file:
From: firstname.lastname@example.org To: email@example.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
cat test-email.txt | sendmail -t
Sending emails using command line or shell scripts is a common task for admins.
Dependent on the task you can use the simple
mutt for attachments or
sendmail to send composed emails using a simple text