LinuxCommandLibrary

mail

Send and receive electronic mail

TLDR

Open an interactive prompt to check personal mail

$ mail
copy

Send a typed email message with optional CC. The command-line below continues after pressing . Input message text (can be multiline). Press to complete the message text
$ mail --subject "[subject line]" [to_user@example.com] --cc "[cc_email_address]"
copy

Send an email that contains file content
$ mail --subject "[$HOSTNAME filename.txt]" [to_user@example.com] < [path/to/filename.txt]
copy

Send a tar.gz file as an attachment
$ tar cvzf - [path/to/directory1 path/to/directory2] | uuencode [data.tar.gz] | mail --subject "[subject_line]" [to_user@example.com]
copy

Display help
$ mail [[-h|--help]]
copy

SYNOPSIS

mail [OPTIONS] [RECIPIENTS...]
mail [OPTIONS] -f [FILE]
mail [OPTIONS] -u [USER]

PARAMETERS

RECIPIENTS...
    One or more email addresses to which the message will be sent. If no recipients are specified and neither -f nor -u is used, mail enters interactive mode for reading mail.

-s SUBJECT
    Sets the subject line of the email.

-c ADDRESS
    Sends a carbon copy (CC) of the message to the specified ADDRESS.

-b ADDRESS
    Sends a blind carbon copy (BCC) of the message to the specified ADDRESS. This address is not visible to other recipients.

-r ADDRESS
    Specifies the 'From' address for the outgoing mail. Its effectiveness depends on MTA configuration and permissions.

-a FILE
    Attaches FILE to the message. The availability and exact behavior of this option can vary depending on the mail implementation (e.g., heirloom-mailx vs. mailutils).

-f [FILE]
    Reads messages from FILE instead of the user's default system mailbox (typically /var/mail/USER or /var/spool/mail/USER). If FILE is omitted, it defaults to mbox in the user's home directory.

-u USER
    Reads messages from the system mailbox of the specified USER. This usually requires appropriate read permissions.

-H
    Displays a summary list of mail headers only, without entering the full interactive mail reader.

-v
    Enables verbose mode, showing a transcript of the mail session. This can be useful for debugging mail delivery issues.

DESCRIPTION

The mail command is a fundamental Unix-like utility for sending and receiving email. It functions as a basic Mail User Agent (MUA), enabling users to compose and send messages, as well as read and manage incoming mail. When used for sending, mail typically accepts the message body from its standard input, making it highly suitable for scripting purposes, automated notifications, and sending quick, simple messages. It relies on an underlying Mail Transfer Agent (MTA) like sendmail or Postfix for actual mail delivery. While its sending capabilities are robust for command-line use, its interactive mode for reading mail is more minimalist compared to feature-rich graphical or terminal-based email clients. It's often employed in system administration for alerts, log distribution, and basic internal communication.

CAVEATS

The functionality and specific options of the mail command can differ significantly across various Linux distributions and their underlying implementations (e.g., those provided by bsd-mailx, heirloom-mailx, or mailutils packages). Users should always consult the man page for their system's installed version for precise details. Sending complex emails or those with certain types of attachments may not be fully supported by all basic mail versions. For advanced email management, more robust clients like mutt or graphical MUAs are generally recommended. Using mail in scripts requires careful input validation to prevent potential security vulnerabilities like command injection.

BASIC SENDING EXAMPLE

To send a simple email from the command line:
echo "Hello, this is the body of the email." | mail -s "My Subject" user@example.com

READING MAIL

To enter the interactive mail reader and view your inbox:
mail
Once inside, common commands include p (print message), d (delete message), s (save message), and q (quit).

HISTORY

The mail command traces its origins back to the very early days of Unix, serving as one of the original utilities for inter-user communication within a multi-user computing environment. As email systems evolved, more sophisticated clients emerged, but mail maintained its role as a lightweight, script-friendly interface for sending and basic reading of messages. The mailx command developed as an enhanced successor, often aliased or symlinked to mail on modern systems. Different implementations, such as heirloom-mailx and GNU mailutils, have continued its legacy, providing varying levels of features and POSIX compliance, ensuring its place as a fundamental command-line email tool.

SEE ALSO

mailx(1), sendmail(8), mutt(1), pine(1), procmail(1)

Copied to clipboard