The ability to send and receive emails directly from the command line is a powerful tool for Linux users. From system administrators to just someone who works well with the terminal, the Linux mail command provides a pretty quick and efficient way to handle emails without relying on the graphical client.
In this guide, we will cover everything from the basic syntax to the installation guide as well as troubleshooting common issues.
What is the Linux mail Command?
The mail command in Linux is one of the simplest command-line utilities that sends and receives emails directly from the terminal. It is a part of the traditional Unix mail system and is typically useful for system administrators who need to set up automated notifications, cron job alerts, or quick communication.
Depending on your Linux distro, the mail command varies from the different packages such as Heirloom mailx or BSD mail, but the core functionality remains the same.
- Send emails including a subject and a body
- Receive and read emails locally
- Pipe the output from the commands into an email
For example, you can send a simple email with:
echo “This is the email body” | mail -s “Test Subject” [email protected]
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
This makes mail a versatile tool for both manual email sending and automated workflows in Linux environments.
Related Article: Mastering Linux Commands: A Comprehensive Guide with Real-World Applications
Basic Syntax of the Linux Mail Command
The basic syntax of the Linux mail command is:
mail [options] [email protected]
Common options:
- -s “subject” → specify the subject of the email
- -c “[email protected]” → send a carbon copy
- -b “[email protected]” → send a blind carbon copy
- -a /path/to/file → attach a file (works in mailx)
For example:
echo “Message body” | mail -s “Subject Here” [email protected]
This sends an email with a subject and body to [email protected].
How to Send Mail from the Linux Command Line
Sending out emails with the Linux mail command can be done in multiple ways depending on your needs:
- Send a simple email
echo “Hello, this is a test email.” | mail -s “Test Email” [email protected]

This would send a plain hello email.
- Send an email with a body from a file
mail -s “Monthly Report” [email protected] < /home/user/report.txt
In this email the contents of the report.txt are used as the body in the email.
- Send to multiple recipients
echo “Meeting at 3 PM” | mail -s “Team Meeting” [email protected] [email protected]
Using this approach, you can send the same email to multiple recipients.
- Add CC and BCC recipients
echo “Reminder: Submit your tasks.” | mail -s “Task Reminder” \
-c [email protected] -b [email protected] [email protected]
In this method, the email will be sent to all the employees including the manager and the HR.
- Send command output as an email
df -h | mail -s “Disk Usage Report” [email protected]
This will automatically set the disk usage into the email body.
Advanced Usage of the Linux Mail Command
Here are a few of the advanced options that come with the Linux mail commands.
- Send attachments
The standard mail command does not support attachment, but using mailx will solve this problem.
echo “Please find the attached file.” | mail -s “Attachment Example” -a /path/to/file.pdf [email protected]
If not supported, you can use mutt:
echo “Attached report” | mutt -s “Report” -a report.pdf — [email protected]
- Send HTML emails
echo “<h1>Alert</h1><p>Server is down.</p>” | mail -a “Content-Type: text/html” -s “HTML Email” [email protected]
This will send the email formatted as an HTML page.
- Automating with scripts
You can add the mail into shell scripts for automated alerts or cron jobs. Here is an example:
0 8 * * * df -h | mail -s “Daily Disk Report” [email protected]
This command will send a report at 8 AM.
Checking Mail in Linux with the mail Command
Now that we have “sending” the emails out of the way, it is time to understand the receiving end of it. When your system receives an email, it is stored into the user’s local mailbox, usually stored in /var/mail/username. To check the emails, follow these steps:
- Run this command to display the list of all received emails with their message numbers, run: mail
- Type out the message number to read a message.
- Navigate between messages:
- n → next message
- d <num> → delete a specific message
- h → return to the message list
- Quit the mail client: Press q to exit.
This makes it easy to review system-generated emails, cron job alerts, or messages from other users on the server.
Common Errors and Troubleshooting
Error/Issue | Cause | Solution |
command not found: mail | The mail package (or mailutils/mailx) is not installed. | Install it using:• Ubuntu/Debian: sudo apt install mailutils• CentOS/RHEL: sudo yum install mailx |
Cannot send message: Process exited with a non-zero status | The system has no configured SMTP server or relay. | Install and configure postfix or sendmail, or use an external SMTP relay. |
Emails not received | Mail service is blocked or misconfigured. | Check mail logs (/var/log/mail.log or /var/log/maillog). Ensure ports 25/465/587 are open. |
No mail for user when checking inbox | No mail has been delivered to the user’s local mailbox. | Verify that the mail service is running and messages are actually being sent. |
Attachments not working | The default mail command often doesn’t support attachments. | Use mailx or mutt for attachment support. |
Emails marked as spam | Sending server lacks proper DNS/SMTP configuration. | Configure SPF, DKIM, and DMARC records for your domain. |
Alternatives to the Mail Command in Linux
While the Linux mail command is super lightweight, many admins prefer advanced tools that support attachments, HTML, and more.
- mailx – it is an extended version of mail with attachments.
- mutt – Terminal based mail client with attachments, PGP, and IMAP.
- sendmail – It is a Mail Transfer Agent for advanced configurations.
- postfix – A modern MTA widely used on Linux servers.
- ssmtp / msmtp – Simple clients for sending mail via an external SMTP.
- heirloom-mailx – Enhanced version of mailx with advanced scripting features.
Conclusion – The Linux mail Command
The Linux mail command enables you to send and receive emails without leaving the terminal. It is an excellent command to ensure that the alerts and corn job emails are sent on time!
Where does Linux store mail received by the mail command?
Emails are usually stored in /var/mail/username
or /var/spool/mail/username
, depending on your system configuration.
Can I use the mail command to send mail via Gmail or another external SMTP?
Yes, but you need to configure an external SMTP relay in your system (e.g., via ssmtp
or postfix
) before the mail command can deliver to external addresses like Gmail.
How do I send an email using the mail command in Linux?
You can use:
echo "Message body" | mail -s "Subject" [email protected]
This sends a quick email with a subject line.