How to use Linux mail command?

Mail Command Featured Image

Linux provides a utility to manage our emails from the command line itself. The mail command is a Linux tool, that can be used to send emails via a command-line interface. In this tutorial, we will learn how to use this Linux command to send and receive mails from directly from the Terminal.

Installing the Linux Mail Command

To take advantage of this command, we need to install a package named 'mailutils'. It can be done by typing the following commands in the Ubuntu/Debian Terminal:

sudo apt install mailutils

Once this command is successfully installed, we can begin to learn its usage. However, one thing that must be kept in mind is that, 'mailutils' allows us to connect to a local SMTP (Simple Mail Transfer Protocol) server. Therefore, we can not expect sending mails to an external domain like '' or ''.

Let us now see how to write and send mails through the terminal.

Mail Command syntax

There can be multiple ways to send a simple mail using mail command. Let us look at all the possibilities:

Simple Mail Body

mail -s "<Subject>" <receiver@domain>
<Mail Body>
<Mail Body>
<CTRL + D>
Simple Mail
Simple Mail SyntaxSimple Mail Command example

In the above example, the mail is sent from root to the user named, 'aprataksh'. We will look at how to check our mailboxes later in the tutorial.

The option '-s' is used to denote the “Subject” of the mail.

It must be noted that, to end the body of the mail, the user must interrupt using 'CTRL + D'. 'Cc:' refers to as a carbon copy and is used in case we wish for other parties to get informed of the mail as a reference.

Passing body using ‘<<<‘

mail -s "<Subject>" < recipient address > <<< "<Mail Body>"
Mail Syntax Arro
Mail Syntax using ‘<<<‘

In the above example, we are creating an email using the mail command, and passing in the string as the message body.

Using ‘echo’ command

echo "<Mail Body" | mail -s "<Subject>" < recipient >
Mail Syntax Echo
Mail Syntax using ‘echo’

The echo command throws the string out. The use of pipe '|' is to pass the thrown string to the email created later.

Mail body from a file

In case we are weary of writing large messages for email on the command line, we can define the mail body as file and pass it to the 'mail' command by:

mail -s "<Subject>" <receiver@domain> < <FILENAME>
Mail Syntax File
Mail body in a file

The -s flag allows us to send the message, which is stored in a text file as the body of the mail. After looking at all the ways to send the mail, we will see other provisions of the mail command.

Multiple receivers for a single mail

In case, we wish to send the same email to multiple users, we can do so by specifying the mail addresses separated by a comma:

mail -s "<Subject>" "<receiver1@domain>,<receiver2@domain> <<< "<Mail-body>" 
Mail Multiple
Multiple receivers for a mail

Attachments to the mail

Like a normal Email application, mail command, supports the feature of attaching files to the emails.

mail -s "<Subject>" -A <PATH>/<FILENAME> <receiver@domain> <<< "<Mail body>"
Mail Attached
Attaching file to an email

The '-A' or '--attach' options are used to attach files. In the above example, the file — ‘my_program.cpp’ has been attached.

Cc and Bcc

A regular Email user would know the benefits of CC and BCC, with respect to emails. If you want to send these, we can use '-c'option for Cc, whereas '-b' option for Bcc.

mail -s "<Subject>" -c "<receiver@domain>" -b "<receiver@domain>" <receiver@domain> <<< "<Mail-body>"

The mailbox

There is no hassle to check the mailbox. All we need to type is 'mail' and press ENTER. Since, we were sending to the user 'aprataksh', we will check it that particular mailbox.

Mailbox Edited

Access an email

To open the mails, we need to mention the specific mail number associated to it. For instance, let us read the first mail.

Mail Value
Access an email

The mail system provides all sorts of information related to the mail.

Navigate through the emails

In order to open the next email, we can enter '+' in the prompt, whereas, '-' is used to access the previous mail in the mailbox.

Mail Plus
Next mail in the mailbox

Deleting unimportant mails

It is fairly easy to delete mails from the mailbox. All we have to do is open a mail using the number and press 'd'.

Mail Delete Edited
Deleting Email

There are techniques to delete multiple emails at once:

  • To delete emails numbered 6 and 9 – 'd 6 9'
  • To delete from 4 to 40 – 'd 4-40'
  • To delete all the mails – 'd*'

Extract the attachments

After sending an attached file through the mail command, how do we access it?

To answer the question, first we need to understand how the attachments are sent. The attachments go through base64 encoding. It basically converts them into a text, that can be decoded on the other side.

In the example for attachment, we sent the 'my_program.cpp' file. Let us look at it on the other side.

Mail Attached Edited 1
Attached File

The new task at hand is to decode the encoded text. To achieve this, we first need to save the encoded data in a file. Then we use the base64 command:

base64 -d encoded-data-file
Mail Decoded
Decoding the attachment

This is pretty much everything, for the functioning of the mailbox.

Quit the mailbox

After reading and deleting the mails, we can quit in the following ways:

  • 'CTRL + D' – If we quit using this method, all the emails we have already read will be saved and transferred to the local mailbox, stored in 'home/<username>/mbox' by default.
Mail Ctrl D Edited
Quitting using ‘CTRL + D’

As we can see, we accessed only one email, therefore the seen message is transferred to the local mailbox. The unread emails are not touched.

  • 'exit' or 'xit' – If we quit the mailbox using the 'exit' command, no changes are made to the mailbox contents.
Mail Exit
Quitting using ‘exit’
  • 'CTRL + Z' – This is a common tool for stopping a running process. Similar to 'exit' command, no changes are made to the current mailbox.

Access other mailboxes

As we told earlier, after reading the mails, mail command has a provision of saving mails in local mailbox. It can be accessed using ‘-f' option.

mail -f <PATH>/<FILENAME>
Mail Backup 1
Accessing other mailboxes

Wrapping Up: The Mail command in Linux

The Linux Mail command is really easy to use once you understand the basics of it. You can use this command to send and receive emails directly via the command line without having to use any kind of Graphical application. It gives the sender flexibility to send messages to their peers in multiple manners on the local SMTP server. You can also automate several things with the help of shell scripts so that your PC automatically sends a mail upon encountering any error or finishing up any task.