Command Line Email
Objective
Send and receive email from the Linux command line using mail/mailx utilities, and understand how local and remote mail delivery works.
Tools & Technologies
mailmailxmuttsendmail
Key Commands
echo 'body' | mail -s 'Subject' user@hostmail -s 'test' admin@localhost < report.txtmailx -r from@domain -s 'subj' to@domainArchitecture Overview
sequenceDiagram
participant U as User (CLI)
participant M as MTA (Postfix/sendmail)
participant Q as Mail Queue
participant R as Recipient MTA
participant MB as Mailbox
U->>M: mail -s 'subject' user@host
M->>Q: Queue message
Q->>R: SMTP delivery
R->>MB: Store in mailbox
Note over U,MB: Local mail goes directly to /var/spool/mail/
Step-by-Step Process
01
Send Basic Email
Use the mail command to send a message from the command line.
# Simple one-liner
echo 'This is the body' | mail -s 'Test Subject' user@localhost
# From a file
mail -s 'Monthly Report' [email protected] < report.txt
# Multiple recipients
echo 'See attached' | mail -s 'FYI' alice@host bob@host
02
Read Mail
The mail command is also an interactive mail reader.
mail # open mailbox
# Interactive commands:
p # print/read message
d # delete message
r # reply
q # quit
h # list headers
03
Check Mail Queue & Logs
Monitor mail delivery status and troubleshoot delivery failures.
# View mail queue
mailq
# Force queue flush
sudo sendmail -q
# Check mail log
sudo tail -f /var/log/mail.log
sudo journalctl -u postfix
Challenges & Solutions
- Local mail goes to /var/spool/mail/username — check if MTA is running
- External mail requires properly configured DNS MX records and may be blocked by ISP
Key Takeaways
- mail command doubles as both sender and reader
- mutt and neomutt are far more capable TUI mail clients for daily use