LinuxCommandLibrary

mailx

Send and receive email messages

TLDR

Send mail (the content should be typed after the command, and ended with )

$ mailx [[-s|--subject]] "[subject]" [to_addr]
copy

Send mail with content passed from another command
$ echo "[content]" | mailx [[-s|--subject]] "[subject]" [to_addr]
copy

Send mail with content read from a file
$ mailx [[-s|--subject]] "[subject]" [to_addr] < [content.txt]
copy

Send mail to a recipient and CC to another address
$ mailx [[-s|--subject]] "[subject]" [[-c|--cc]] [cc_addr] [to_addr]
copy

Send mail specifying the sender address
$ mailx [[-s|--subject]] "[subject]" [[-r|--from-address]] [from_addr] [to_addr]
copy

Send mail with an attachment
$ mailx [[-a|--attach]] [path/to/file] [[-s|--subject]] "[subject]" [to_addr]
copy

SYNOPSIS

mailx [options] [address ...]
mailx [options] -f [file]

PARAMETERS

-s subject
    Sets the Subject header field of the message.

-a file
    Attaches the specified file to the message.

-c address
    Sends carbon copies (CC) of the message to the specified address(es).

-b address
    Sends blind carbon copies (BCC) of the message to the specified address(es).

-r address
    Specifies the 'From' address for the message.

-h hostname
    Specifies the local hostname to be used in outgoing messages.

-u user
    Reads or sends mail for the specified user instead of the current user.

-f [file]
    Reads messages from the specified file instead of the system mailbox. If file is omitted, it defaults to the user's 'mbox' file.

-F
    Records the message in a file named after the first recipient.

-i
    Ignores interrupts. Useful in scripts to prevent premature termination.

-n
    Prevents initialization from the system default `mailx.rc` or personal `~/.mailrc` files.

-N
    When reading mail, suppresses the initial display of the header summary.

-q
    Terminates the program cleanly even if an interrupt is received, by saving changes.

-v
    Enables verbose mode, displaying details of mail delivery steps.

-A account
    Uses a specific configured account (defined in `~/.mailrc`) for sending or receiving mail.

-t
    Reads recipient addresses (To, Cc, Bcc) from the message body itself.

DESCRIPTION

The mailx command is a versatile utility used for sending and receiving electronic mail directly from the command line. It acts as an interactive message processing system, allowing users to compose, send, read, and manage mail messages. While it can be used interactively for reading and replying to mail, its strength often lies in its ability to send mail from scripts and automated processes.


It supports a wide range of features, including attaching files, specifying carbon copies (CC) and blind carbon copies (BCC), setting custom subjects, and managing various mail headers. Modern implementations of mailx often include support for secure connections (SSL/TLS) for sending mail via SMTP and retrieving mail from POP3/IMAP servers, making it suitable for contemporary email infrastructures. It is a robust and flexible tool for system administrators and developers alike.

CAVEATS

mailx relies on a properly configured Mail Transfer Agent (MTA) on the system (e.g., Postfix, Sendmail, Exim) for sending external mail. Without an MTA, it can only send local mail or connect directly to a remote SMTP server if configured to do so. Security considerations are important when sending sensitive data; ensure SSL/TLS is used for external SMTP/IMAP/POP3 connections. Configuration can be complex, often requiring edits to ~/.mailrc or system-wide configuration files.

MODES OF OPERATION

mailx operates in two primary modes: sending mail (when recipient addresses are specified on the command line, it enters send mode) and reading/managing mail (when invoked with options like -f or without arguments, it enters interactive receive mode to display and manage messages).

CONFIGURATION FILES

The behavior of mailx is highly customizable via system-wide (e.g., /etc/mail.rc, /etc/mailx.rc) and user-specific (~/.mailrc) configuration files. These files can define variables, aliases for recipients, and macro commands to streamline common tasks and set up connections to remote mail servers.

HISTORY

mailx has a long history, evolving from the original mail command found in early Unix systems. It was designed to provide more advanced features and a more user-friendly interface than its predecessors. Over time, it gained enhancements and became part of various Unix standards, including POSIX and System V Release 4 (SVR4). Its development has focused on improving its capabilities for both interactive use and, crucially, for scripting automated email notifications, making it a staple in shell scripts and system automation workflows. Modern versions often include support for contemporary email protocols like SMTP, POP3, and IMAP, extending its utility beyond local mail delivery.

SEE ALSO

mail(1), sendmail(8), mutt(1), alpine(1), procmail(1)

Copied to clipboard