VMware ESXi and vSphere Cluster Management
Read and Send Local Email with the Linux mail Command
Learn to read local system mail and send messages with mail or mailx, including subjects, Cc, pipelines, mailbox files, delivery checks, and troubleshooting.
What the Linux mail Command Does
mail is a text-based command-line mail user agent. It can compose, send, and read email, especially messages delivered to local Unix accounts. Many systems provide mail through a mailx-compatible package. The related command name may be mailx, or mail may be a link or compatibility wrapper.
This command is different from a webmail application or an IMAP client. A typical mail command reads the current user's local system mailbox. It does not normally log in to a hosted inbox or browse a remote mailbox over IMAP.
Sending a message also involves a Mail Transfer Agent (MTA), such as Postfix, Sendmail, or Exim. The mail user agent submits the message; the MTA accepts and routes it. Local delivery may work even when external delivery is not configured. Sending to an external address requires suitable relay, DNS, hostname, network, and often authentication configuration.
Check Which Implementation Is Installed
Syntax differs among s-nail, Heirloom mailx, BSD mail, and distribution-provided compatibility packages. Do not assume that an option available on one system exists on another.
command -v mail
command -v mailx
man mail
command -v locates an executable in the current PATH. The manual page documents the options and interactive commands supported by the local implementation. If available, also inspect version information or package ownership using the tools provided by your distribution.
When this lesson uses an option such as -b for Bcc, verify it locally first. Some implementations use different options, while others may not provide the feature in the same form.
Local Mailboxes, Spools, and Queues
Local user mail is mail delivered to a Unix account on the same system or local mail domain. The destination is a local mailbox, often called the user's inbox. A system-managed collection of these mailbox files is the mail spool.
Common spool locations include:
/var/mail/username/var/spool/mail/username
The exact path, mailbox format, ownership, and permissions depend on the distribution and MTA configuration. To check common locations for the current user:
ls -l /var/mail/$USER /var/spool/mail/$USER 2>/dev/null
An mbox is both a traditional mailbox file format and a commonly used name for a saved-mail file. For example, a user's saved messages may be in ~/mbox.
A mail queue is different from a mailbox. The queue contains outgoing or deferred messages waiting for the MTA to process. It is not the user's inbox.
| Concept | Purpose | Typical location or owner | How it is accessed |
|---|---|---|---|
| Local inbox/mail spool | Stores messages delivered to a local account | /var/mail/username or /var/spool/mail/username | mail or the local mailbox tools |
| Outgoing MTA queue | Holds messages waiting for delivery or retry | Managed by the installed MTA | mailq where supported, plus MTA logs |
| Saved mbox file | Stores messages moved or saved after mailbox use | Often ~/mbox | mail -f ~/mbox |
Read the Current Local Mailbox
Run mail with no recipient arguments to open the default local mailbox for the current user:
mail
The program normally displays a numbered message list and then an interactive prompt. The exact columns vary, but they commonly include a message number, status, sender, date, size, and subject.
Message numbers are important: interactive commands usually operate on the current message or on a number you specify. Common actions include:
- Enter a message number, such as
1, to open that message. - Press
Enterto move to or open the next appropriate message, depending on the implementation. - Use a listing or print command such as
headersorhto display the message list again. - Use
porprintto display the current message in implementations that support those commands. - Use
?orhelpat the prompt to see supported commands. - Use
qorquitto leave the mailbox.
Interactive command names and their effects vary. Always use the local help command when in doubt.
Exiting can change message disposition. A message may be marked as read, deleted, or moved according to the command used and the implementation's rules. Messages that remain saved may be transferred to a saved mailbox such as ~/mbox. Avoid using deletion commands until you understand how your installed program handles them.
Read a Saved Mailbox File
Use -f to open a specified mailbox file instead of the default system mailbox:
mail -f ~/mbox
~/mbox is a conventional example, not a guaranteed path. Some systems use another file or mailbox format. Messages previously read may be saved, moved, or retained differently depending on the implementation and configuration.
Send an Interactive Message
The general form is:
mail [options] recipient...
Recipients are normally supplied as command arguments. After starting the command, type the plain-text message body at standard input. Finish the body by pressing Ctrl+D on a new line. Ctrl+D signals end-of-file; it is not normally part of the message.
mail -s "System notice" bob
Type the message, for example:
The scheduled maintenance begins at 22:00.
Then press Ctrl+D. The -s option supplies a visible Subject header.
Conceptually, the command-line recipient is part of the delivery envelope: it tells the mail system where to route the message. Visible headers such as To:, Cc:, and Subject: are message information shown to recipients. The exact headers generated by a particular implementation can vary.
Local and Remote Recipients
| Recipient form | Typical destination | Required infrastructure | Common failure causes |
|---|---|---|---|
bob | A local account or local mail domain | Existing account and functioning local delivery | Unknown account, stopped MTA, bad mailbox permissions, or full storage |
admin@example.com | An address outside the local system | Working MTA, DNS and network access, and an approved relay when required | Missing relay, SMTP rejection, DNS problems, blocked egress, or lack of authentication |
A local username may be enough when local delivery is configured. Use a complete address such as user@example.com for an external destination. Multiple recipients are supplied as separate arguments:
mail -s "Status" alice bob admin@example.com
Place options before the recipient arguments unless your local manual documents another form. A syntactically correct command can still fail during delivery if the host is not allowed to relay external mail.
Subject, Cc, and Bcc
Use -s to set the subject:
mail -s "Backup report" admin@example.com
Use -c to add a Cc recipient:
mail -s "Hello" -c jack bob
To identifies the primary visible recipients. Cc adds visible recipients whose addresses are shown to the other recipients. Bcc adds recipients whose addresses are hidden from the other recipients.
Some mail implementations provide Bcc through a -b-style option:
mail -s "Maintenance notice" -b auditor@example.com admin@example.com
Verify this option with man mail before using it. Never assume that every mail or mailx implementation has identical Bcc syntax.
Non-Interactive Mail in Scripts
For scripts and scheduled tasks, provide the body through standard input instead of waiting for interactive typing. Standard input is the stream a command reads from the terminal, a pipe, or a redirection.
printf '%s\n' "Backup completed successfully." | mail -s "Backup report" admin@example.com
You can also send command output directly:
df -h | mail -s "Disk usage report for $(hostname)" admin@example.com
Use an explicit subject in automated messages so recipients and monitoring systems can identify them. Shell quoting protects spaces, punctuation, wildcard characters, and variable expansions. Double quotes allow intended variable expansion, while single quotes preserve text literally. Be especially careful when inserting untrusted command output into a shell command.
Common Mail Tasks
| Task | Typical command | Notes |
|---|---|---|
| Open current local mailbox | mail | Reads the default mailbox for the current user. |
| Open a specified mailbox file | mail -f ~/mbox | Path and format depend on local configuration. |
| Send an interactive message | mail recipient | Type the body and finish with Ctrl+D. |
| Set a subject | mail -s "Subject text" recipient | Check local documentation for option details. |
| Add a Cc recipient | mail -s "Subject text" -c copy-recipient primary-recipient | Cc addresses are visible to recipients. |
| Send piped text or command output | printf '%s\n' "Message body" | mail -s "Subject text" recipient | Useful for scripts, cron, and monitoring. |
| Display implementation help | man mail | Also use help or ? inside the interactive prompt. |
Verify Delivery and Follow the Message Lifecycle
For local delivery, send a test message to a local account and then open that account's mailbox:
printf '%s\n' "Local delivery test" | mail -s "Test" bob
su - bob -c mail
The exact method for becoming another user depends on system permissions and administration policy. You can also ask the recipient to run mail or inspect the appropriate local mailbox.
For external delivery, the MTA may accept the message, place it in a queue, retry it, defer it, or return a bounce. Where supported, inspect the queue with:
mailq
Review the MTA's mail logs for SMTP responses, DNS failures, connection errors, authentication failures, and policy rejections. Log locations and service-management commands vary by MTA and distribution.
Troubleshooting
mail: command not found
- Run
command -v mailandcommand -v mailx. - If neither exists, install an appropriate mailx-compatible package using the distribution's package-management process.
- Read the installed program's manual because package implementations differ.
A local recipient does not receive mail
- Verify the account:
getent passwd username. - Confirm that local delivery and the MTA are running according to the system's service-management tools.
- Inspect the recipient's mailbox, permissions, available storage, and relevant mail logs.
External mail is deferred, bounced, or missing
- Inspect the queue with
mailqwhere available. - Read MTA logs for the exact SMTP rejection or connection failure.
- Check the configured relay host, authentication, DNS, hostname, reverse DNS, and network egress policy.
- Use an approved SMTP relay when direct delivery is not permitted.
The mailbox shows no expected messages
- Confirm the identity with
whoami. - Check both common spool paths.
- Confirm that the message was addressed to the account you are reading.
- Try
mail -f ~/mboxif messages may have been saved there.
An option behaves differently
The installed program may be a different mailx implementation or a compatibility symlink. Check its version or package information and read man mail. Adapt the command to the local implementation instead of assuming that all variants have identical options.
Limitations and Tool Selection
mail is intentionally basic. Its usual strength is plain-text local mail and simple administrative notifications. It is not a replacement for an IMAP client, webmail client, or full-featured desktop mail application.
Attachment support, MIME features, encryption options, and advanced address handling are implementation-dependent. Consult the local manual before relying on them. For remote inbox access, use an appropriate IMAP, POP3, webmail, or terminal mail client configured for that service.
Security and Operational Cautions
- Do not place passwords, API keys, or other credentials directly on a command line. Depending on the system, other local users or processes may be able to observe command arguments.
- Plain-text mail may expose sensitive content. Unencrypted or poorly configured relay paths may be unsuitable for confidential information.
- For external delivery, use properly configured authenticated SMTP or an approved organizational mail infrastructure.
- Limit sensitive information in automated reports and protect log files and generated message content.
Summary
- Use
mailwithout recipients to read the current user's local mailbox. - Use
mail -f fileto open a saved mailbox file such as~/mbox. - Use
-sfor a subject and-cfor visible carbon-copy recipients. - Finish interactive composition with Ctrl+D on a new line.
- Use a pipe or redirection for scripts and scheduled notifications.
- Local usernames and external addresses depend on different delivery infrastructure.
- Check the local manual because
mailandmailximplementations differ. - Use MTA logs and
mailqto investigate delivery after submission.
For related lessons, see Read and Send Mail.