In this tutorial, you will learn to use the Linux
- A system running Linux.
- Access to the terminal (Ctrl+Alt+T).
- An account with root privileges to install the utility.
Install Linux mail Command
In RHEL-based systems, the
mailx rpm packages, while in Debian-based systems, the command is a part of the
mailutils package. Check if the utility is installed by running:
If the output states that the command is not installed, follow the steps below to install
1. Run one of the following commands to install mail, depending on your operating system:
- For RHEL/Rocky Linux/CentOS:
sudo yum install mailx
- For Ubuntu/Debian/LinuxMint:
sudo apt install mailutils
2. When prompted to configure the
postfix package, press TAB to select Ok and confirm with Enter.
2. Select Internet Site in the package configuration window. Press TAB to select Ok and press Enter to confirm.
3. Complete the basic configuration by entering the system mail name (the machine's fully qualified domain name).
Wait for the installation to complete.
Linux mail Command Syntax
The basic syntax for sending an email using the
mail [ options ] -s [ subject ] [recipient_address]
- The available
[options]are explained in the section below.
-sflag specifies the email subject.
[recipient_address]is the email address/username of the person you are emailing.
After entering the command,
To send the email, press Ctrl+D.
mailutils package allows users to connect to a local SMTP (Simple Mail Transfer Protocol) server. Although it is possible to configure it for use with external domains, it requires additional
postfix configuration. Moreover, the latest update from Google complicates the setup for Gmail as Google no longer allows less secure apps to sign into a Google account (except for Google Workspace or Google Cloud Identity clients).
Linux mail Command Options
Some of the available options are:
|Attaches a file to the email.|
|Allows users to append the specified message to the email. It can also be used to send Carbon copies or Blind carbon copies of the email.|
|Outputs various information useful for debugging.|
|Prevents sending messages with an empty body.|
|Use to specify an alternate mailbox. Defaults to the user's |
|Forces the interactive mode, even when input is not a terminal. For example, the special ~ command character is only available in interactive mode.|
|Causes mail to ignore tty interrupt signals.|
|Prevents initial message headers when reading mail or editing a mail folder.|
|Inhibits reading /etc/mail.rc on startup.|
|Used to specify the email subject on the command line. Encase the subject in double quotes.|
Refer to the sections below for concrete examples of using the
Linux mail Command Examples
In this tutorial, we will send mail to specific users on the system by specifying the username in place of the recipient address. To send mail to external domains, make sure to set up postfix to relay an external SMTP server.
Example 1: Send Mail with Subject and Body
The basic way of using
-s flag and add the recipient. The command then enters the interactive mode, asking if you want to send Ccs and asking for the email body.
After entering all the information, press Ctrl+D to send the email.
For example, the following email is sent from the user 'bosko' to the 'root' user:
After sending the email, the command exits the interactive mode.
Example 2: Send Mail Using echo
Use the echo command to send an email without entering the interactive mode of the
echo command output to the
The syntax is:
echo "[email body]" | mail -s "[subject]" [recipient]
echo command takes the string, and the pipe (
|) passes the string to the
Example 3: Send Email Using Redirection
Another way to provide the email body is to use redirection (
<<<). Using redirection also avoids the
The syntax is:
mail -s "[subject]" [recipient] <<< "[email body]"
In the above example, we create an email using the
Example 4: Send Email from File
The syntax is:
mail -s "[subject]" [recipient] < [file_path]
The command takes input from the specified file and sends it as the email body. Providing input from files is useful when calling the
Example 5: Send Mail to Multiple Receivers
To send mail to multiple receivers, specify the list of recipients in a comma-separated list. The syntax is:
mail -s "[subject]" [recipient1], [recipient2] <<< "[mail body]"
Alternatively, send a carbon copy by entering the command's interactive mode. To send a blind carbon copy of the email, use the
--append) flag. The syntax is:
mail -s "[subject]" --append='BCC:[recipient1],[recipient2]...'
First, we specify the subject and the Bcc recipients, and then the command enters interactive mode, asking for the email recipient, Cc recipients, and email body. The difference between Cc and Bcc is that Cc allows all recipients to see who else got the email, which isn't the case with Bcc.
Example 6: Send Attachments
-A flag. Specify the file path to attach the file to the email. Note that the file is base64 encoded before sending and that the recipient must decode it upon receiving.
The syntax is:
mail -s "[subject]" -A [file_path]
Press Ctrl+D to send the mail with the encoded attachment. Refer to the section below on saving attachments to see the result.
Example 7: Read Mail
Check the mailbox for the current user by running the
The output shows the new messages, the sender, the date it was received, and the subject. To open an email, enter the email number and press Enter. For example, we will open email number 8:
The output shows the sender's address, the email subject, the recipients, the email date and time, and the contents.
After opening the email, the command waits for further input from the user. Open the next email by entering (
+) in the prompt, or access the previous email by entering (
-). Return to the mailbox by pressing Ctrl+D.
Example 8: Delete Mail
To delete an email, open the email you want to delete, press d, and confirm with Enter. To delete multiple emails at once, open the mailbox and enter d followed by the email numbers you want to delete.
For example, the following command deletes emails numbered 1 and 2:
d 1 2
To delete a range of emails, specify the first and last email in the range separated by a dash. For example, to delete emails 1-10, open the mailbox and run:
To delete all received mail, open the mailbox and run:
Example 9: Save Attachments
For example, we have previously sent an email with an attachment to the
root user. After opening the email, the output states that it contains the email body (text) and additional content encoded in base64:
To decode the attachment, save the encoded data in a file, and use the
base64 command to decode it. The syntax is:
base64 -d [encoded-data-file-path]
For example, the decoded file we sent looks like this:
Example 10: Incorporate mail in Scripts
1. Create a script using a text editor of your choice.
2. Paste the following lines and replace the
[recipient] with the address/es you want the report to go to:
#!/bin/bash du -sh | mail -s "Disk usage report" [recipient]
3. Save the file and execute the script using the following syntax:
The script executes and sends an email to the specified address with the
du command output.
This tutorial showed how to use the
Read our tutorial to learn more about important Linux commands and download a cheat sheet to always have them at hand.