The mail command

OpenBSD manual page for the mail, mailx, and Mail commands: “BUGS” section notes, “Usually, mail and mailx are just links to Mail, which can be confusing.” So, note that this program may often have different ways that it may be started.

Using the -v parameter may turn on verbose mode.

Using the command line
[#malfrcli]: Sending E-Mail from the command line using the mail
mail -s "E-Mail subject" user@example.com

The quotation marks above are likely to be used by the shell to make the multiple words “E-Mail subject” appear as just one parameter. The -s parameter simply causes the next parameter to be used as an E-Mail subject. If the desired subject is just a single word, then the quotation marks are not necessary.

Note that specifying a subject on the command line is not required. One may use:

mail user@example.com

Running the program with a recipient specified, but without a subject, results in the program showing a line that says “Subject: ”. A line may then be typed and that line will then be used as a subject.

The previous example shows how to simply send mail. If there is a desire to see more details as the message gets sent, try:

mail -vs "E-Mail subject" user@example.com
Things to test, and which may get added to this section here:
  • Typing a message in the program,
  • (ending E-Mail with Ctrl-D / aborting with Ctrl-Cx2),
  • piping (is Ctrl-D required then?),
  • multiple recipients?
  • attachments?

Using the program interactively

(This guide currently does not have details about sending E-Mail using this method.)

The mail command may not have the easiest interface: using a full-screen program like Alpine may be easier for many users. However, one benefit of using this software is that it may be bundled with an operating system. Also, it may be somehow beneficial if someone uses a terminal that doesn't provide nice/working full-screen support. (However, if one is simply interested in reading mail, and is less concerned about other mail-related functions like marking the E-Mail message as a mesasge that has already been read, then using software designed to view files (particularly using an option that allows for searching through files) may be a sufficient alternative.

For users who are running mail for the first time, it may be worthwhile to back up some files, such as all of /var/mail/ and, in case it already exists, the ~/mbox file. However, while that may be practical on a relatively brand new system with very little mail, it may be far less practical on a production system serving the mail of many users. Perhaps it will be practical to copy /var/mail/root and the /var/mail/username file that is named after the current user that is logged in.

Running the mail command with no parameters is likely to check the terminal type and then use the mail command's interactive mode. Specifying the -I parameter can help to force the interactive mode.

Quitting, and what happens

Using “q” may show results such as:

Saved 3 messages in mbox
Held 72 messages in /var/mail/root

If that happens, then those messages (which were “unresolved”, according to the terminology used by the mail program's internal help) are moved to an mbox file (which may be stored in the $HOME directory?).

Using the “x” command might not do that?

When quitting with changes saved, OpenBSD's manual page for the mail command (specifically in the section describing the -f parameter) notes, “mail writes undeleted messages back to” the file being used. (If a mailbox has many messages, this could result in writing a lot of data just to effectively be deleting one E-Mail message.)

Interactively looking at the right mailbox

To view the E-Mail of the currently logged in user, simply run:

mail

To view the E-Mail of another user, such as the “root” account's mail, either provide permissions to read the file or, perhaps easier, obtain temporarily elevated permissions by running:

sudo -u root mail

The -u root shown in the above example is the default. Therefore, it doesn't need to be specified. Other usernames could be specified, but if root's E-Mail is the E-Mail being read, then a slightly simpler command may be used:

sudo mail

Note that using the sudo command may cause root's mail to be read (by default).

This will provide the version of the mail, as shown in the following example output:

Mail version 8.1.2 01/15/2001.  Type ? for help.

Then details of the current mailbox are shown. (This is the same output that will occur if running the fi command, with no parameters, from the & prompt that is within the mail command.) For example, the text may look something like this:

"/var/mail/root": # messages # unread

Then, if the mail command is not using the -N parameter to surpress showing E-Mail message summarized header lines, a list of messages will show. If there are more than 18 messages on a terminal with 24 lines, only information about the first 19 messages are shown.

The end user should be treated with a & prompt. Look towards the top where it shows the name of the mailbox in quotation marks (just before listing the number of messages). If that is set to a desirable value, then the desired mailbox is being viewed. If not, feel free to change it as needed. That can be done within the mail program, by entering the following into the prompt:

fi /var/mail/root

That may show a message such as:

Held # messages in /var/mail/root
"/var/mail/root": # messages # unread

Then summarized header lines of the messages are then shown again. (Note that the term “headers” used in this sense is just showing basic details like the sender of the mail and the subject line of the E-Mail. It doesn't mean full E-Mail headers.)

There are a couple of other approaches that can be done so that the correct mailbox doesn't need to be specified within the program. One is to run:

env MAIL=/var/mail/root sudo mail

or, perhaps the following will be easier to remember:

sudo mail -f /var/mail/root
Seeing how many E-Mail messages exist

This is shown at the start of the program, but it may be nice to review that again at a later time. This may be done by running the fi command with no paramaters.

Viewing summarized header lines for all of the messages

If there are more than 18 messages on a terminal with 24 lines, only information about the first 19 messages are shown when the mailbox is first opened. To see twenty-one more messages, use:

f 20-40

Specifying more messages may cause some summarized header lines to scroll off the screen. Note that the last number needs to be an existent message number.

Reading a message

There are various ways. Using “t 3” will type the third message. Then “n” will type the next message. Using “p 7” will print the seventh message. Using “more 9” will view the ninth message with the external $PAGER command.

One message is considered to be the currently selected message. When the results of the f command show the currently selected message, the first column of output will show a “greater-than” sign by whichever message is the currently selected message. Just running the f command with no parameters will show just information about current message, so that is a way to determine what message is currently selected. Another command to read a message is the n command which views the “next” message.

After a message is read, it may be considered to be “unresolved”.

Sending

There are a few ways to start writing a message:

Starting a new message
m user@example.com,someone@example.net
Replying

Identify the message number(s) to reply to.

To reply to an existing E-Mail message, use:

R 1

If it is desired to reply to all recipients, use:

r 1

The mail command will show a line that says “Subject: ”.

Then, type a message. Press Ctrl-D to send. To abort sending the E-Mail, press Ctrl-C twice.