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.

ConceptPurposeTypical location or ownerHow it is accessed
Local inbox/mail spoolStores messages delivered to a local account/var/mail/username or /var/spool/mail/usernamemail or the local mailbox tools
Outgoing MTA queueHolds messages waiting for delivery or retryManaged by the installed MTAmailq where supported, plus MTA logs
Saved mbox fileStores messages moved or saved after mailbox useOften ~/mboxmail -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 Enter to move to or open the next appropriate message, depending on the implementation.
  • Use a listing or print command such as headers or h to display the message list again.
  • Use p or print to display the current message in implementations that support those commands.
  • Use ? or help at the prompt to see supported commands.
  • Use q or quit to 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 formTypical destinationRequired infrastructureCommon failure causes
bobA local account or local mail domainExisting account and functioning local deliveryUnknown account, stopped MTA, bad mailbox permissions, or full storage
admin@example.comAn address outside the local systemWorking MTA, DNS and network access, and an approved relay when requiredMissing 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

TaskTypical commandNotes
Open current local mailboxmailReads the default mailbox for the current user.
Open a specified mailbox filemail -f ~/mboxPath and format depend on local configuration.
Send an interactive messagemail recipientType the body and finish with Ctrl+D.
Set a subjectmail -s "Subject text" recipientCheck local documentation for option details.
Add a Cc recipientmail -s "Subject text" -c copy-recipient primary-recipientCc addresses are visible to recipients.
Send piped text or command outputprintf '%s\n' "Message body" | mail -s "Subject text" recipientUseful for scripts, cron, and monitoring.
Display implementation helpman mailAlso 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 mail and command -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 mailq where 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 ~/mbox if 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 mail without recipients to read the current user's local mailbox.
  • Use mail -f file to open a saved mailbox file such as ~/mbox.
  • Use -s for a subject and -c for 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 mail and mailx implementations differ.
  • Use MTA logs and mailq to investigate delivery after submission.

For related lessons, see Read and Send Mail.