---
title: "Sending Test Emails from Shell"
description: "Sending test emails from shell using command mail and mutt, adding content and even attachments."
url: "https://mailmum.io/posts/sending-test-emails-from-shell/"
---

# Sending Test Emails from Shell

Sending a test [email](https://simple.wikipedia.org/wiki/E-mail) over
[SMTP](https://mailmum.io/glossary/simple-mail-transfer-protocol-smtp/) from
[shell](https://simple.wikipedia.org/wiki/Shell_(computing)) 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](https://de.wikipedia.org/wiki/Bash_(Shell)) or
[zsh](https://de.wikipedia.org/wiki/Z_shell).

## Requirements

Most systems will have some kind of email command line tool like `mail` or
`mutt`. For systems with [Postfix](https://www.postfix.org) or
[Sendmail](https://www.sendmail.com/sm/open_source/) installed, there is the
`sendmail` command you can use for.

Using [MTA (email server)](https://mailmum.io/glossary/mail-transfer-agent-mta/)
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](https://www.debian.org) based systems. You can install them by running:

```sh
sudo apt install mailutils
```

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

```sh
sudo apt install mutt
```

## Let's start with a simple test email

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

```sh
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:

```sh
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:

```sh
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`.

```sh
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](https://www.sendmail.com/sm/open_source/)
or[Postfix](https://www.postfix.org) server installed.

Lets start by creating a `sendmail` compatible text file:

```txt
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:

```sh
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.
