The mail Application

A UNIX utility named mail is included with all UNIX and Linux distributions. Because it has a very Spartan interface, it is not commonly used interactively. However, mail is still an extremely useful program to use in shell scripting because it can take all the information it needs to send mail from the command line. No user interaction is required. mail is also useful for sending quick notes because it starts up quickly and doesn't require going through menus to send a message.

Using mail

To begin sending a message in mail, simply type mail at the shell prompt, followed by the email address that you want to send mail to:

$ mail [email protected]

mail then prompts you for the subject of the message. Enter the subject and press Enter. The cursor then moves down to the next blank line. You can now start entering the body of your message. When you finish, press Ctrl+D on a blank line to exit the mail program and send the message. (You might be prompted for Cc:. If you do not want to send carbon copies to anyone, simply press Enter.) The message is handed off to the MTA, and your shell prompt returns. To abandon a message you are currently writing, press Ctrl+C.

If you want to retrieve your email using the mail program, simply type mail at the shell prompt. The system responds with something similar to the following:

Mail Type ? for help.

"/var/mail/andrew": 1 message 1 unread

>U 1 [email protected] Sun Mar 20 07:15 18/551 "Birthday" >N 2 [email protected] Sun Mar 20 08:25 18/542

"A large and obnoxious spam message"

>N 3 [email protected] Sun Mar 20 09:21 17/524 "Ubuntu 4 Update"

>N 4 [email protected] Sun Mar 20 09:24 17/528 "Issues"

The & prompt is mail's way of prompting you for input.

The first column in the list of messages is a flag that indicates the status of the message. For example, u means that the message is unread. n means that the message is new. The rest of the columns are self-explanatory.

To read one of these messages, simply type the message number and press Enter. For example,

Message 4

From [email protected] Sun Mar 20 09:24:35 2003 Date: Sun, 29 Mar 2006 09:24:34 -0500 From: [email protected] To: [email protected] Subject: Issues

Hey Nick,

The new issue looks great! Where did you get the idea for the cover from?

Seeya &

If you want to respond to this message, you can type respond and press Enter. You can also simply type r and press Enter. By default, the command is applied to the currently active message, which is indicated in the message list by a, (and will be the last message that you read). If you want to have the command applied to a different message, you can simply specify the message number after the command. For example, typing r 2 will respond to message number 2.

A complete list of commands is available within mail by typing ? at the prompt. Note that you can abbreviate all commands to the shortest abbreviation that is not ambiguous.

Shell Scripting with mail

As mentioned previously, mail's most useful application is its use in shell scripting. The complete instruction to create and send a message can be done from the command line. For example, suppose that we have a shell script that generates a report and stores it as a text file. Now we want to email that text file to a user at the end of the script. With mail, we could use a command such as the following:

$ mail [email protected] -s "Report from shell script" < report.txt

The -s specifies a subject line on the command line, and then we use simple shell redirection to use the file report.txt as the body for the message (see Chapter 15, "Automating Tasks"). It is simple, clean, and requires no user interaction to send the message.

For other options available on the mail command line, see the man page for mail.

mutt mutt is a relatively new command-line mail client that is rapidly becoming popular with users. The client is called mutt because it is known as "the mongrel of email clients" in that it attempts to combine the best features of several other clients such as elm and pine. Mutt is an extremely feature-rich email client, although it is not as easy to use as pine. It is, however, more secure than pine. Figure 8.14 shows the mutt interface.

Was this article helpful?

0 0

Post a comment