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:

For Fedora/ RHEL:

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux-1.png" data-lazy- height="93" src="data:image/svg xml,” width=”861″>

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”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux-2.png" data-lazy- height="576" src="data:image/svg xml,” width=”1291″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux3.png" data-lazy- height="577" src="data:image/svg xml,” width=”1277″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux-4.png" data-lazy- height="577" src="data:image/svg xml,” width=”1273″>

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” maadiim.mm@gmail.com

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux-5.png" data-lazy- height="153" src="data:image/svg xml,” width=”868″>

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” maadiim.mm@gmail.com <<< ‘How are you?’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux-6.png" data-lazy- height="90" src="data:image/svg xml,” width=”1079″>

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

$ echo “How are you?” | mail -s “casual mail” maadiim.mm@gmail.com

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux7.png" data-lazy- height="86" src="data:image/svg xml,” width=”1074″>

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

$ mail -s “casual mail” maadiim.mm@gmail.com , hammadzahid@hotmail.com <<< ‘How are you?’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux8.png" data-lazy- height="111" src="data:image/svg xml,” width=”1288″>

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” maadiim.mm@gmail.com , hammadzahid@hotmail.com <<< ‘How are you? I am sending you an image’ -A /home/hammad/Downloads/index.png

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux9.png" data-lazy- height="117" src="data:image/svg xml,” width=”1282″>

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” maadiim.mm@gmail.com < /home/hammad/mymail.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux10.png" data-lazy- height="123" src="data:image/svg xml,” width=”1291″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux11.png" data-lazy- height="537" src="data:image/svg xml,” width=”1176″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux12.png" data-lazy- height="533" src="data:image/svg xml,” width=”1188″>

The mail and its detail have been opened:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux13.png" data-lazy- height="584" src="data:image/svg xml,” width=”857″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux14.png" data-lazy- height="150" src="data:image/svg xml,” width=”1112″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-mail-command-in-Linux15.png" data-lazy- height="329" src="data:image/svg xml,” width=”1169″>

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

Conclusion

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

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/hammad–150×150.jpg61c81f0ebc58d.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.