Linux Commands

How to use mail command in Linux

Mail is the quickest way of communicating messages to your loved ones, there are different email client servers that allow users to send emails, besides this, Linux by default provides the feature of sending an email using the “mail” command through its terminal. We can write the subject, message along with the email address of the recipient and send it by just executing a single command.

There are different packages through which we can install the mail command, these packages are discussed in this write-up as well as the use of the mail command is also explained in detail.

Installation of mail packages for different distributions of Linux

Mail packages can be installed in different distributions of Linux using the below-mentioned commands.

For CentOS/Redhat:

$ sudo yum install mailx

For Fedora/ RHEL:

$ sudo dnf install mailx

For Debian/ Ubuntu/ Linux Mint:

$ sudo apt install mailutils

Installation of Mail package in Ubuntu

We are going to install the mail package in Ubuntu using the mailutils package:

$ sudo apt install mailutils -y

It will display the four options of the postfix with its details, read all the options, and click “Ok” after deciding which option you want to choose, as we decided to go with “Internet site”:

Choose the “Internet Site” and then press “Ok”:

Choose the domain name, we chose “linuxhint”, you can choose of your own choice, and click on the “Ok” button to proceed:

The mailutils is successfully installed.

How to use the mail command in Linux

The mail command in Linux is used to send emails to different people for communicating some messages, to use the mail command, we have installed the mailutils, now the general syntax of using the mail command will be:

$ mail -s "subject" [recipient-email-address]
CC [you can skip it by pressing the ENTER key]
<Type the text you want to send>
[Press CTRL + D]

Use the clause of mail to end the mail, the type “-s” to specify the subject, type the recipient email address, press the ENTER key, it will ask for the CC (carbon copy) give it or skip it by pressing the ENTER key, type the message, you want to communicate and finally press CTRL+D to send the message.

For the better understanding, we send a message using an email address, [email protected], we will use the command:

$ mail -s "casual mail"

And when the “cc” will be asked, we will skip it by pressing the ENTER key and then type the message:

On pressing the CTRL+D, the mail will be sent successfully, we can also send the same mail using the single command:

$ mail -s "casual mail" <<< 'How are you?'

Or the other way of sending an email using a single command:

$ echo "How are you?" | mail -s "casual mail"

Sometimes, we have to send the mail to multiple users, in that case, use the following syntax:

$ mail -s "casual mail" , <<< 'How are you?'

If we want to attach some file with the email address, use the “-a” flag along with the mail command like we have a png file with name, index.png, we attach it with the mail using the command:

$ mail -s "casual mail" , <<< 'How are you? I am sending you an image' -A /home/hammad/Downloads/index.png

If we have a message in a text file and we want to send the mail from the text file, execute the following command:

$ mail -s "casual mail" < /home/hammad/mymail.txt

To read the mails, use the command of “mail”:

$ mail

In the output, we can see there are thirteen mails, to open any mail, we can write its index number and press the ENTER key, for example, if we want to open the seventh mail, we will type “7” and press the ENTER key:

The mail and its detail have been opened:

Similarly, to delete the emails from 4 – 15, just type “d 4 15” and press the ENTER key:

To confirm the deletion of the mails, run the command of mail again:

$ mail

We can observe that the specified emails have been deleted from the inbox, in this way, we can manage the mail inbox as well.


In Linux, the mail command is a command-line utility that is used to send and manage the emails from the command line. The mail command is not used to send emails only but also to view and receive emails from the recipients. In this write-up, we have discussed the mail command and its installation commands in different distributions of Linux. We also discussed the usage of the mail command in detail in Debian and Ubuntu-based Linux distributions.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.