How to use mail command in Linux?

In Linux, the mail is the command utility that sends emails using the command line interface. It has a variety of functionalities, like sending mail to one or more recipients with a subject. All this is done through the commands.

This blog will demonstrate the usage of the “mail” command utility in Linux, and the blog content is:

Let us start with the installation of the mail command utility in Linux.

How to Install the Mail Package in Linux?

The package of “mail” comes in the Linux Distribution’s default repository when installed on the computer. To install the “mail” package in different distributions of Linux, use the following commands:

CentOS/Redhat$ sudo yum install mailx
Fedora/ RHEL$ sudo dnf install mailx
Debian/ Ubuntu/ Linux Mint$ sudo apt install mailutils

We will demonstrate the installation of “mail” in Ubuntu Jammy Jellyfish. Update all the packages of Ubuntu before proceeding towards the installation of “mail”:

$ sudo apt update

After updating all the packages, install the “mailutils” package in Ubuntu:

$ sudo apt install mailutils -y

It will display the “Postfix Configuration” options using which we can install the “mailutils” package in Ubuntu. Read the whole message and click on “OK” to proceed to the next step:

In these options, select the options according to the application of the usage. For example, if we want to use it with the “SMTP”, we will choose the “Internet Site” and then click “Ok”:

Now specify the domain name which will be used to send the emails using the mail command utility; we are using the “itsLinuxFoss,” but you can choose other than it as well:

The package of the “mailutils” has been installed on Ubuntu, and we can now send emails using the “mail” command utility.

How to Use mail Command in Linux?

To use the mail command utility in Linux, first, we have to understand the general usage of the mail command:

$ mail [Option] [Address]

The explanation of the general syntax of using the “mail” command is:

  • “Mail” refers to the keyword of the command.
  • “Option” is the list of options supported by the command.
  • “Address” represents the mail-address

Different options that can be used with the “mail” command are:

-ATo attach a file with the mail
-iTo ignore the interrupts if occurs
-pTo print all the emails in the standard output
-sTo mention the subject in the mail
-qTo cause interrupts for terminating the program
-tTo read the recipients from the header
–helpTo display the help list
-VTo display the installed version of the mail command utility

With the help of some examples, the usage of the “mail” command has been explained.

Send Emails With Subject

The email’s subject defines the email’s purpose to the recipient. To send the email with the subject, the “s” option is used. For example, we will send an “Introduction email” to the recipient “[email protected]” using the mail command:

$ mail -s "Introduction email" [email protected]

When the command is executed, it will give the option to keep some emails in “Carbon Copy (CC)” which is optional. After pressing the “ENTER” key, type the message you want to communicate and then use the “CTRL+D” shortcut key to send the email.

Use mail to Send Email in a Single Command

Another way to send the email is by using the “<<<” characters. To pass the message body of the email in single mail command, follow the below-mentioned usage syntax:

$ mail -s “subject” [address] <<< “[message body]”

To understand this syntax, we will consider the above example. And will send the above email by running the mail command:

$ mail -s “Introduction email” [email protected] <<< “Hi, How are you? I am Maddox. I am sending this email using the mail command.”

The email is sent by running a single command. This can be done using the “echo command”:

$ echo "Hi, How are you? I am Maddox. I am sending this email using the mail command" | mail -s "casual mail" [email protected]

In these two ways, we can send the email to the recipient using a single command.

Use the mail Command to Send Emails to Multiple recipients

We can also use the mail command to send the email to multiple recipients. For example, we will send the email to three recipients with the command:

$ mail -s “Introduction email” [email protected] , [email protected] , [email protected]   <<< “Hi, How are you? I am Maddox. I am sending this email using the mail command.”

The email is sent to all recipients.

Use the mail Command to Send Emails with the Attachments

We can use the “-A” option of the “mail” command to send emails with the attachments. For example, we have a png file that can be sent in the email:

$ mail -s "Introduction email" [email protected] <<< 'Hi, How are you? I am Maddox. I am sending this email using the mail command. I am sending you an image' -A /home/itslinux/attachment.png

The attachment is sent in the email.

Use the mail Command to Send Email from a File

We can type the message body in the text file and use the mail command to send that email using its file.

For example, we will run the command:

$ mail -s "Introduction Email" [email protected] < /home/itslinux/mail.txt

The mail is sent using the file.

Use the mail Command to Send to CC

The Carbon Copy (CC) and the Blind Carbon Copy (BCC) can be used with the “c” and “b” options of the mail command, respectively. For example, we will send the email:

$ mail -s “Introduction Email” b [email protected] c [email protected] [email protected] <<< “Hi, How are you?”

The CC and BCC are used with the mail option.

How to Manage the Mailbox Using the mail Command in Linux?

We can open the mailbox with the mail command:

$ mail

We have 10 mail in our mailbox, and their index number (shown in the output) can use. For example, we want to open the email with the 5 index number:

The output shows that the email sending failed, and its reason for failure is also explained. We can delete the emails as well by using the “d” option; for example, if we want to delete all the emails:

We can also use the “d” option:

Delete a specific email using their index number7th emaild 7
Delete the range of emails using the index number5th to 9th emailsd 5-9

Finally, to exit the mailbox use the “exit” and press the ENTER key:

You can also use the shortcut key of “CTRL+D” to exit from the mailbox.

This is all about the mail command =.

Conclusion

To use the mail command in Linux for sending emails to the recipients, use the syntax “mail [Option] [Address]”. This command line utility makes it convenient for us to send emails to a recipient or multiple recipients using a single command. In this blog, the usage of the “mail” command in Linux has been explained in detail with the help of examples.