Linux Mail Command Guide


1. Basic mail Command Usage

1.1 Send an Email

mail -s "Subject" recipient@example.com
  • Type the message, then press Ctrl+D to send.

  • Example:

    mail -s "Hello" user1@example.com
    This is a test email.
    (Press Ctrl+D)
    

1.2 Send from a File

mail -s "Subject" recipient@example.com < message.txt

Example:

echo "This is the email body" > body.txt
mail -s "Test Email" user1@example.com < body.txt

1.3 Specify Sender (From Address)

mail -s "Subject" -r "sender@example.com" recipient@example.com

Example:

mail -s "Meeting" -r "admin@company.com" user1@example.com

2. Reading Emails with mail

2.1 View Your Mailbox

mail

Commands inside mail: - Enter → View next message. - d <num> → Delete message (e.g., d 1). - u <num> → Undelete message. - q → Quit (saves changes). - x → Quit (discards changes).

2.2 View Another User’s Mail (as Root)

sudo mail -u username

Example:

sudo mail -u alice

3. Advanced mail Command Options

3.1 Send to Multiple Recipients

mail -s "Subject" user1@example.com,user2@example.com

Example:

mail -s "Team Update" alice@example.com,bob@example.com

3.2 Add CC and BCC

mail -s "Subject" -c "cc@example.com" -b "bcc@example.com" recipient@example.com
  • -c → Carbon Copy.

  • -b → Blind Carbon Copy (hidden from others).

3.3 Attachments (Using uuencode)

uuencode file.txt file.txt | mail -s "File Attached" user@example.com

Example:

uuencode report.pdf report.pdf | mail -s "Report" alice@example.com

4. Scripting with mail

4.1 Automated Email from Bash Script

#!/bin/bash
SUBJECT="System Alert"
TO="admin@example.com"
BODY="Disk space is running low!"

echo "$BODY" | mail -s "$SUBJECT" "$TO"

4.2 Send Command Output via Email

df -h | mail -s "Disk Usage Report" admin@example.com

Example (send top output):

top -b -n 1 | mail -s "Top Processes" admin@example.com

5. Troubleshooting

5.1 Check if mail is Installed

which mail

Installation:

  • Debian/Ubuntu:

    sudo apt install mailutils
    
  • RHEL/CentOS:

    sudo yum install mailx
    

5.2 Check Mail Queue

mailq # For Sendmail
postqueue -p # For Postfix

5.3 Find Mail Logs

tail -f /var/log/mail.log # Debian/Ubuntu
tail -f /var/log/maillog # RHEL/CentOS

6. Alternative Mail Clients

Command

Description

mutt

Advanced CLI email client

alpine

User-friendly terminal mail

thunder-

bird

GUI mail client

Example using mutt:

sudo apt install mutt # Install
mutt -f /var/mail/$USER # Open mailbox

7. Summary Cheat Sheet

Command

Description

mail -s "Sub" user@ex.com

Send email

mail -u username

Read another user’s mail (root)

echo "Hi" | mail -s "Test" user@ex.com

Pipe message

uuencode file.txt file.txt | mail ...

Send attachment

mailq

Check mail queue

tail /var/log/maillog

Debug mail issues


Notes:

  • The mail command is lightweight but lacks modern features (e.g., HTML emails).

  • For scripting, it’s reliable (e.g., cron jobs, alerts).

  • For advanced usage, consider mutt, sendmail, or Postfix.