TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Linux

Linux: Email From the Command Line

Read and send electronic mail, all from the Linux command line.
Aug 16th, 2025 7:00am by
Featued image for: Linux: Email From the Command Line
Feature image via Unsplash.

Linux has more commands than you will probably ever need to use. For desktop users, that is certainly the case. However, for admins who manage Linux servers, there are far more commands that you will need to take advantage of.

One such command is mail.

The Linux mail command allows users to send and receive emails directly from the command line. But don’t think this is a terminal replacement for the likes of Thunderbird or Geary. The mail command is all about GUI-less Linux machines, so emails can be sent via services and/or scripts.

Think about it this way:

You’ve created a backup script and want to have that script notify you upon either a successful or failed run. After all, you need to know if your backups are running correctly. Without that notification, you would have to manually check to see if a new backup file was created. Imagine how things would play out if you failed to check the backup for a few months and you lost everything.

Not good.

Let’s talk more about how mail is installed and used.

Installing mail on Popular Distributions

How you install mail will depend on the distribution you use because the mail command is found in different utilities.

For example, if your distribution of choice is Red Hat Enterprise Linux (or is based on RHEL), the command for installing mail is:


On the other hand, if your distribution is Ubuntu (or based on Ubuntu), mail is found in mailutils, which is installed with the command:


During the installation, you’ll be prompted to configure Postfix (the Linux mail transfer agent that routes and delivers email). If you plan on using mail in conjunction with SMTP, you’ll want to select “Internet site” for this.

Next, you’ll need to add the machine’s Fully Qualified Domain Name (FQDN). Once you’ve taken care of that, you should be good to go.

One thing to keep in mind is that if the emails are going to be sent only to users on the local system, and that local system doesn’t have an FQDN, you can select Local only.

Mail Command Syntax

Every Linux command has a specific syntax for its usage, and mail is no different. The basic syntax of the mail command is:


Naturally, there are quite a few options for the mail command, such as:

  • -A – Attaches a file.
  • -a=[header] – Used for appending specific messages to an email as well as cc’ing or bcc’ing.
  • -f – Allows you to specify an alternate mailbox.
  • -I – Forces interactive mode.
  • -s – Allows you to specify a subject for the email.

Examples of the Linux mail Command

Now, let’s take a look at some examples of the Linux mail command, starting with a simple, straightforward email with a subject and a body. Let’s say our subject is “Hello, New Stack” and our body is “How are you doing?” We want to send that mail to the Linux user newstack. Here’s what that command would look like:


Hit Enter on your keyboard and you’ll find yourself in interactive mode, where you can add a cc user, followed by the body of the email. Once you’ve typed the body and are pleased with it, hit Enter again and then hit the Ctrl+D combination, and the email is off to the recipient.

The recipient can then read the mail by typing the mail command with no options. They’ll be presented with a list of emails they can select from.

You can simplify this with the help of the echo command, which bypasses interactive mode, using echo to add the body of the email.

Here’s how that’s done (using the same example as above):


The newstack user will see the same email as they did when you ran mail in interactive mode. I’ll include all comments in the script, so you know what’s happening.

Using mail in a Bash Script

Let’s first see a simple example of using mail in a Bash script.

This example simply sends an email to a user:

As you can see, we define the recipient, subject and body as variables and then use them in the mail command.

Now let’s take a look at a far more complex bash script that also employs the mail command. This script runs a backup and then alerts the admin in the event of a success or failure:

As you can see, even within the complexity of the backup script, the mail command should be clear and obvious (even though it’s using declared variables).

And that, my friends, is the basics of the Linux mail command.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.