On Unix-like operating systems, the mailx command sends and receives mail.

This page covers the GNU/Linux version of mailx.

Description

mailx is an intelligent mail processing system, which has a command syntax reminiscent of ed with lines replaced by messages. It is based on Berkeley Mail 8.1, is intended to provide the functionality of the mail command, and offers extensions for MIME, IMAP, POP3, SMTP, and S/MIME. mailx provides enhanced features for interactive use, such as caching and disconnected operation for IMAP, message threading, scoring, and filtering. It is also usable as a mail batch language, both for sending and receiving mail.

  • Description
  • Syntax
  • Examples
  • Related commands
  • Linux commands help

Syntax

mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr . . .

mailx [-BDdeEHiInNRv~] [-T name] [-A account] [-S variable[=value]] -f [name]

mailx [-BDdeEinNRv~] [-A account] [-S variable[=value]] [-u user]

Options

Sending mail

To send a message to one or more people, mailx can be invoked with arguments that are the names of people to whom the mail will be sent. The user is then expected to type in his message, followed by an ‘control-D’ at the beginning of a line, which signals the end of the message, and implicit approval to send it.

Reading mail

In normal usage mailx is given no arguments and checks the user’s mail out of the mail server, then prints out a one line header of each message found. The current message is initially the first message (numbered 1) and can be printed using the print command which can be abbreviated ‘p’). The user can move among the messages much as he moves between lines in ed, with the commands ‘+’ and ‘-’ moving backwards and forwards, and simple numbers.

Disposing of mail

After examining a message the user can delete (’d’) the message, or reply ‘r’) to it. Deletion causes the mailx program to forget about the message. This is not irreversible; the message can be undeleted (‘u’) by giving its number, or the mailx session can be aborted by giving the exit (‘x’) command. Deleted messages will, however, usually disappear never to be seen again.

Specifying messages

Commands such as print and delete can be given a list of message numbers as arguments to apply to many messages at once. Thus ‘delete 1 2’ deletes messages 1 and 2, while ‘delete 1-5’ deletes messages 1 through 5. In sorted or threaded mode (see the sort and thread commands), ‘delete 1-5’ deletes the messages that are located between (and including) messages 1 through 5 in the sorted/threaded order, as shown in the header summary. The following special message names exist:

A practical method to read a set of messages is to issue a from command with the search criteria first to check for appropriate messages, and then to read each single message by typing ’’’ (single quote) repeatedly.

(“real name” “source-route” “local-part” “domain-part”)

Replying to or originating mail

The reply command can be used to set up a response to a message, sending it back to the sender. Text the user types in then, up to an end-of-file, defines the contents of the message. While the user is composing a message, mailx treats lines beginning with the character ‘~’ specially. For instance, typing ‘~m’ (alone on a line) will place a copy of the current message into the response right shifting it by a tabstop (see indentprefix variable, below). Other escapes will set up subject fields, add and delete recipients to the message, attach files to it and allow the user to escape to an editor to revise the message or to a shell to run some commands.

Ending a mail processing session

The user can end a mailx session with the quit (‘q’) command. Messages which have been examined go to the user’s mailbox file, unless they have been deleted, in which case they are discarded. Unexamined messages go back to the post office (see the -f option above).

Personal and systemwide distribution lists

It is also possible to create a personal distribution lists so that, for instance, the user can send mail to the word ‘cohorts’ and have it go to a group of people. Such lists can be defined by placing a line like:

alias cohorts bill ozalp jkf mark [email protected]

in the file .mailrc in the user’s home directory. The current list of such aliases can be displayed with the alias command in mailx. System wide distribution lists can be created by editing /etc/aliases (see sendmail); these are kept in a different syntax. In mail the user sends, personal aliases will be expanded in mail sent to others so that they will be able to reply to the recipients. System wide aliases are not expanded when the mail is sent, but any reply returned to the machine has the system wide alias expanded as all mail goes through sendmail.

Recipient address specifications

When an address is used to name a recipient (in any of To, Cc, or Bcc), names of local mail folders and pipes to external commands can also be specified; the message text is then written to them. The rules are: Any name which starts with a ‘|’ character specifies a pipe, the command string following the ‘|’ is executed and the message is sent to its standard input; any other name which contains a ‘@’ character is treated as a mail address; any other name which starts with a ‘+’ character specifies a folder name; any other name which contains a ‘/’ character but no ‘!’ or ‘%’ character before also specifies a folder name; what remains is treated as a mail address. Compressed folders are handled as described for the folder command below.

MIME types

For any outgoing attachment, mailx tries to determine the content type. It does this by reading MIME type files whose lines have the following syntax:

type/subtype extension [extension . . .]

where type/subtype are strings describing the file contents, and extension is the part of a filename starting after the last dot. Any line not immediately beginning with an ASCII alphabetical character is ignored by mailx. If there is a match with the extension of the file to attach, the given type/subtype pair is used. Otherwise, or if the filename has no extension, the content types text/plain or application/octet-stream are used, the first for text or international text files, the second for any file that contains formatting characters other than newlines and horizontal tabs.

Character sets

mailx normally detects the character set of the terminal using the LC_CTYPE locale setting. If the locale cannot be used appropriately, the ttycharset variable should be set to provide an explicit value. When reading messages, their text is converted to the terminal character set if possible. Unprintable characters and illegal byte sequences are detected and replaced by Unicode substitute characters or question marks ("?") unless the print-all-chars option is set at initialization time.

The character set for outgoing messages is not necessarily the same as the one used on the terminal. If an outgoing text message contains characters not representable in US-ASCII, the character set being used must be declared within its header. Permissible values can be declared using the sendcharsets variable, separated by commas; mailx tries each of the values in order and uses the first appropriate one. If the message contains characters that cannot be represented in any of the given character sets, the message will not be sent, and its text will be saved to the ‘dead.letter’ file. Messages that contain NUL bytes are not converted.

Outgoing attachments are converted if they are plain text. If the sendcharsets variable contains more than one character set name, the [email protected] tilde escape will ask for the character sets for individual attachments if it is invoked without arguments.

Best results are usually achieved when mailx is run in a UTF-8 locale on a UTF-8 capable terminal. In this setup, characters from various countries can be displayed, while it is still possible to use more simple character sets for sending to retain maximum compatibility with older mail clients.

Commands

Each command is typed on a line by itself, and may take arguments following the command word. The command need not be typed in its entirety - the first command which matches the typed prefix is used. For commands which take message lists as arguments, if no message list is given, then the next message forward which satisfies the command’s requirements is used. If there are no messages forward of the current message, the search proceeds backwards, and if there are no good messages at all, mailx types ‘applicable messages’ and aborts the command. If the command begins with a # sign, the line is ignored.

The arguments to commands can be quoted, using the following methods:

  • An argument can be enclosed between paired double-quotes " " or single-quotes ’ ‘; any white space, shell word expansion, or backslash characters in the quotes are treated literally as part of the argument. A double-quote will be treated literally within single-quotes and vice versa. These special properties of the quote marks occur only when they are paired at the beginning and end of the argument.
  • A backslash outside of the enclosing quotes is discarded and the following character is treated literally as part of the argument.
  • An unquoted backslash at the end of a command line is discarded and the next line continues the command.

Filenames, where expected, are subjected to the following transformations, in sequence:

  • If the filename begins with an unquoted plus sign, and the folder variable is defined, the plus sign will be replaced by the value of the folder variable followed by a slash. If the folder variable is unset or is set to null, the filename will be unchanged.
  • Shell word expansions are applied to the filename. If more than a single pathname results from this expansion and the command is expecting one file, an error results.

The following commands are provided:

Tilde escapes

“Tilde Escapes” are characters that are given a special command behavior when a tilde (the ~ character) is placed in front of them, at the beginning of a new line. The following tilde escapes are recognized by mailx:

account myisp { set folder=imaps://[email protected] set record=+Sent set from="[email protected] (My Name)" set smtp=smtp.myisp.example}

define name { command1 command2 … commandN }

protocol://[[email protected]]host[:port][/file]

if receive commands . . . endif

if receive commands . . . else commands . . . endif

Variable options

Variable Options are controlled via set and unset commands; see their entries for a syntax description. An option is also set if it is passed to mailx as part of the environment (this is not restricted to specific variables as in the POSIX standard). A value given in a startup file overrides a value imported from the environment. Options may be either binary, in which case it is only significant to see whether they are set or not; or string, in which case the actual value is of interest.

Binary options

The binary options include the following:

String options

The string options include the following:

Environment variables

Besides the variables described above, mailx uses the following environment strings:

imaps://[email protected]

imaps://[email protected]/INBOX.

Files

Examples

The mailx command has two distinct usages, according to whether one wants to send or receive mail. Sending mail is simple: to send a message to a user whose e-mail address is, say, [email protected], use the shell command

mailx [email protected]

and then type your message. Mailx will prompt you for a message subject first; after that, lines typed by you form the body of the message. When you reach the end of the message, type an End Of Transmission character (control-d) at the beginning of a line, which will cause mailx to echo ‘EOT’ and return you to the shell.

If, while you are composing the message you decide that you do not wish to send it after all, you can abort the letter with a RUBOUT (control-c). Typing a single RUBOUT causes mailx to print ‘(Interrupt – one more to kill letter)’. Typing a second RUBOUT causes mailx to save your partial letter on the file ‘dead.letter’ in your home directory and abort the letter. Once you have sent mail to someone, there is no way to undo the send.

If you want to send the same message to other people, you can list their e-mail addresses on the command line, like this:

mailx [email protected] [email protected]

will send the reminder to [email protected] and [email protected].

To read your mail, at the command line, enter:

mailx

Mailx will respond by typing its version number and date and then listing the messages you have waiting. Then it will type a prompt and await your command. The messages are assigned numbers starting with 1, and later you will refer to the messages with these numbers. Mailx keeps track of which messages are new (have been sent since you last read your mail) and read (have been read by you). New messages have an N next to them in the header listing and old, but unread messages have a U next to them. Mailx keeps track of new/old and read/unread messages by putting a header field called Status into your messages.

To look at a specific message, use the type command, which may be abbreviated to t. For example, if you had the following messages:

O 1 [email protected] Wed Sep 1 19:52 18/631 “Fees” O 2 [email protected] Thu Sep 2 00:08 30/895

you could examine the first message by giving mailx the command:

type 1

which might cause it to respond with, for example:

Message 1: From [email protected] Wed Sep 1 19:52:25 2004 Subject: Fees Status: R Tuition fees are due next Wednesday. Don’t forget!

Many mailx commands that operate on messages take a message number as an argument like the type command. For these commands, there is a notion of a current message. When you enter the mailx program, the current message is initially the first (or the first recent) one. Thus, you can often omit the message number and use, for example,

t

to type the current message. As a further shorthand, you can type a message by giving its message number. Hence,

1

would type the first message.

Frequently, it is useful to read the messages in your mailbox in order, one after another. You can read the next message in mailx by typing a newline. As a special case, you can type a newline as your first command to mailx to type the first message.

If, after typing a message, you want to immediately send a reply, you can do so with the reply command. This command, like type, takes a message number as an argument. mailx then begins a message addressed to the user who sent you the message. You may then type in your letter in reply, followed by control-d at the beginning of a line, as before.

Note that mailx copies the subject header from the original message. This is useful in that correspondence about a particular matter will tend to retain the same subject heading, making it easy to recognize. If there are other header fields in the message, like ‘Cc:’, the information found will also be used.

Sometimes you will receive a message that was sent to several people and wish to reply only to the person who sent it. Reply with a capital R replies to a message, but sends a copy to the sender only.

If you want, while reading your mail, to send a message to someone, but not as a reply to one of your messages, you can send the message directly with the mail command, which takes as arguments the names of the recipients you want to send to. For example, to send a message to [email protected], you enter the following from within mailx:

mail [email protected]

To delete a message from the mail folder, you can use the delete command. In addition to not saving deleted messages, mailx will not let you type them, either. The effect is to make the message disappear altogether, along with its number.

Many features of mailx can be tailored to your liking with the set command. The set command has two forms, depending on whether you are setting a binary option or a valued option. Binary options are either on or off. For example, the askcc option informs mailx that each time you send a message, you want it to prompt you for a ‘Cc:’ header, to be included in the message. To set the askcc option, you would type

set askcc

Valued options are values which mailx uses to adapt to your tastes. For example, the record option tells mailx where to save messages sent by you, and is specified by

set record=Sent

for example. Note that no spaces are allowed in record=Sent.

Mailx includes a simple facility for maintaining groups of messages together in folders. To use the folder facility, you must tell mailx where you want to keep your folders. Each folder of messages will be a single file. For convenience, all of your folders are kept in a single directory of your choosing. To tell mailx where your folder directory is, put a line of the form

set folder=letters

in your .mailrc file. If, as in the example above, your folder directory does not begin with a ‘/’, mailx will assume that your folder directory is to be found starting from your home directory.

Anywhere a file name is expected, you can use a folder name, preceded with ‘+’. For example, to put a message into a folder with the save command, you can use:

save +classwork

to save the current message in the classwork folder. If the classwork folder does not yet exist, it will be created. Note that messages that are saved with the save command are automatically removed from your system mailbox.

To make a copy of a message in a folder without causing that message to be removed from your system mailbox, use the copy command, which is identical in all other respects to the save command.

The folder command can be used to direct mailx to the contents of a different folder. For example,

folder +classwork

directs mailx to read the contents of the classwork folder. All of the commands that you can use on your system mailbox are also applicable to folders, including type, delete, and reply. To inquire which folder you are currently editing, use:

folder

To list your current set of folders, use the folders command.

Finally, the help command is available to print out a brief summary of the most important mailx commands.

While typing in a message to be sent to others, it is often useful to be able to invoke the text editor on the partial message, print the message, execute a shell command, or do some other auxiliary function. Mailx provides these capabilities through tilde escapes, which consist of a tilde (~) at the beginning of a line, followed by a single character which indicates the function to be performed. For example, to print the text of the message so far, use:

~p

which will print a line of dashes, the recipients of your message, and the text of the message so far. A list of the most important tilde escapes is available by typing ‘~?’.

biff — Receive detailed mail notifications at the command line.echo — Output text.ed — A simple text editor.ex — Line-editor mode of the vi text editor.fmt — A formatter for simplifying and optimizing text files.lp — Print a file on the System V operating system.ls — List the contents of a directory or directories.mail — Read, compose, and manage mail.mailcompat — Provide SunOS 4.x compatibility for the Solaris mailbox format.more — Display text one screen at a time.newalias — Install new elm aliases.pg — Browse page by page through text files.sh — The Bourne shell command interpreter.vacation — Reply to mail automatically.vi — Text editor based on the visual mode of ex.