Linux Mail Command: How to Send Emails from the Command Line

Linux Mail Command

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

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]

Tech Delivered to Your Inbox!

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: 

  1. Send a simple email

echo “Hello, this is a test email.” | mail -s “Test Email” [email protected]

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

This would send a plain hello email. 

  1. 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. 

  1. 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. 

  1. 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. 

  1. 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. 

  1. 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]

  1. 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. 

  1. 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:

  1. Run this command to display the list of all received emails with their message numbers, run: mail
  2. Type out the message number to read a message. 
  3. Navigate between messages:
  • n → next message
  • d <num> → delete a specific message
  • h → return to the message list
  1. 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/IssueCauseSolution
command not found: mailThe 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 statusThe system has no configured SMTP server or relay.Install and configure postfix or sendmail, or use an external SMTP relay.
Emails not receivedMail 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 inboxNo 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 workingThe default mail command often doesn’t support attachments.Use mailx or mutt for attachment support.
Emails marked as spamSending 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. 

  1. mailx – it is an extended version of mail with attachments. 
  2. mutt – Terminal based mail client with attachments, PGP, and IMAP.
  3. sendmail – It is a Mail Transfer Agent for advanced configurations. 
  4. postfix – A modern MTA widely used on Linux servers.
  5. ssmtp / msmtp – Simple clients for sending mail via an external SMTP. 
  6. 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.

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!