
mail Command in Linux
The mail command in Linux is a powerful tool used for sending and receiving emails directly from the command line. This command is particularly useful for system administrators, developers, and anyone who needs to automate email notifications or manage emails without a graphical interface.
Table of Contents
Here is a comprehensive guide to the options available with the mail command −
- Understanding mail Command
- Syntax of mail Command
- mail Command Options
- Examples of mail Command in Linux
- Advanced Features of mail Command
Understanding mail Command
The mail command is part of the mailutils package, which provides a suite of utilities for handling email. It allows you to send emails, read incoming emails, and manage your mailbox from the command line. This command is especially useful for scripting and automation, as it can be easily integrated into shell scripts and cron jobs.
Let's install it −
sudo apt install mailutils

Syntax of mail Command
The basic syntax for the mail command is −
mail [options] [recipient]
Without any options, the command opens the interactive mail interface, allowing you to read and manage your emails.
mail Command Options
Here are some of the most commonly used options with the mail command −
Options | Description |
---|---|
-s [subject] | Specify the subject of the email. |
-c [cc] | Specify the CC (carbon copy) recipients. |
-b [bcc] | Specify the BCC (blind carbon copy) recipients. |
-a [attachment] | Attach a file to the email. |
-r [from] | Specify the sender's email address. |
-f [file] | Read messages from the specified file. |
-n | Do not initialize from the system mailrc file. |
-v | Verbose mode. Display detailed information about the email being sent. |
-q [file] | Read the body of the email from the specified file. |
-A [account] | Specify the account to use for sending the email. |
Examples of mail Command in Linux
Let's explore some practical examples to understand how to use the mail command effectively.
Send a Simple Email
This command sends an email with the specified subject and body to the recipient. The body of the email is provided using the echo command and piped to mail −
echo "This is the body of the email" | mail -s "Subject of the email" [email protected]

Send an Email with CC and BCC
This command sends an email with CC and BCC recipients. The -c option specifies the CC recipients, and the -b option specifies the BCC recipients −
echo "This is the body of the email" | mail -s "Subject of the email" -c [email protected] -b [email protected] [email protected]

Send an Email with an Attachment
This command sends an email with an attachment. The -a option specifies the file to attach to the email −
echo "This is the body of the email" | mail -s "Subject of the email" -a /path/to/attachment.txt [email protected]

Specify the Sender's Email Address
This command sends an email with a specified sender's email address. The -r option specifies the sender's email address −
echo "This is the body of the email" | mail -s "Subject of the email" -r [email protected] [email protected]

Read Emails from a File
This command reads emails from the specified file. The -f option specifies the file to read from. This is useful for reading emails stored in a specific mailbox file −
mail -f /var/mail/username

Send an Email in Verbose Mode
This command sends an email in verbose mode, displaying detailed information about the email being sent. The -v option enables verbose mode −
echo "This is the body of the email" | mail -s "Subject of the email" -v [email protected]

Read the Body of the Email from a File
This command reads the body of the email from a specified file. The -q option specifies the file to read the body from −
mail -s "Subject of the email" -q /path/to/body.txt [email protected]

Specify the Account to Use for Sending the Email
This command sends an email using a specified account. The -A option specifies the account to use −
echo "This is the body of the email" | mail -s "Subject of the email" -A account_name [email protected]

Send an Email with Multiple Recipients
This command sends an email to multiple recipients. You can specify multiple recipients by separating their email addresses with spaces −
echo "This is the body of the email" | mail -s "Subject of the email" [email protected] [email protected]

Send an Email with HTML Content
This command sends an email with HTML content. The -a option is used to specify the content type as text/html −
echo "This is the body of the email" | mail -s "Subject of the email" -a "Content-Type: text/html" [email protected]

Advanced Features of mail Command
In addition to the basic options and examples, the mail command provides advanced features that can be useful in specific scenarios. Let's explore some of these features with practical examples.
Automating Email Notifications with Cron Jobs
This cron job sends a daily email report at 8 AM. The mail command is used to send the email, and the cron job schedules it to run daily −
0 8 * * * echo "Daily report" | mail -s "Daily Report" [email protected]

Using mail in Shell Scripts
This shell script checks if a file exists and sends an email notification based on the result. The mail command is used to send the email notifications −
#!/bin/bash if [ -f /path/to/file ]; then echo "File exists" | mail -s "File Check" [email protected] else echo "File does not exist" | mail -s "File Check" [email protected] fi
Reading Emails Interactively
This command opens the interactive mail interface, allowing you to read and manage your emails. You can navigate through your mailbox, read emails, and perform various actions using the interactive interface −

Filtering Emails
This command reads emails from a specified file and filters them based on the subject. The grep command is used to filter the emails −
mail -f /var/mail/username | grep "Subject: Important"

Sending Emails with Custom Headers
This command sends an email with a custom header. The -a option is used to specify the custom header −
echo "This is the body of the email" | mail -s "Subject of the email" -a "X-Custom-Header: CustomValue" [email protected]

Conclusion
The mail command is an essential tool for anyone working with Linux systems, providing a simple and effective way to send and receive emails directly from the command line. By mastering the various options and examples provided.