home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Learning the Unix Operating System

Learning the Unix Operating SystemSearch this book
Previous: 3.3 Protecting and Sharing Files Chapter 3
Your UNIX Account
Next: 3.5 Changing Your Password

3.4 Electronic Mail

When you log in to your system, you may see a notice that says "You have mail." Someone has sent you a message or document by electronic mail (email). With email, you can compose a message at your terminal and send it to another user or list of users. You can also read any messages that others may have sent to you.

Email has several advantages over paper mail: it's convenient if you're already logged in, it's delivered much more quickly, you can send it to any number of people almost as easily as to just one person, and the messages can be stored for later reference.

There are a lot of email programs for UNIX. Some UNIX systems have only an old, simple program named mail , which this book doesn't cover. Most UNIX systems have a Berkeley program called Mail (with an uppercase "M"), mailx , or just mail . A popular menu-driven program that's easier to learn is called pine . All programs' basic principles are the same, though. We'll cover the Berkeley mail program.

3.4.1 Sending Mail

Your mail's recipient doesn't have to be logged in. The messages you send are stored in the recipient's "mailbox," a file deep in the UNIX filesystem (often located in the directory /usr/mail ). Messages are kept there until the recipient logs in and decides to read them.

To send mail, give the address of each person you want to send a message to, like this:

mail address1 address2 ...

There are several kinds of addresses, too many to explain here. If you have questions, see one of the references in Appendix A or ask your system administrator or postmaster (the person who maintains your email system). The most common addresses have this syntax:


username is the person's username and hostname is the name of their computer. If the recipient reads email on the same computer you do, you may omit the @hostname . To keep a copy of your message, just add your username to the list of addresses.

After you enter mail and the addresses, in most cases the program (depending on how it's set up) will prompt you for the subject of the message. Type a one-line summary of the message (just like a paper memo) and press [RETURN] . Type in your message, line by line, pressing the [RETURN] key after every line, just as you would on a typewriter. When you have finished entering text, type [CTRL-D] (just once!) on a separate line. You should get the shell prompt at this point - though it might take a few seconds. If you change your mind before you type [CTRL-D] you can cancel a message (while you are still entering text) by entering ~q (a tilde character, then the letter "q") at the start of a line. The cancelled message is placed in a file called dead.letter in your home directory. To see other commands you can use while sending mail, enter ~? (tilde question mark) at the start of a line of your message, then press [RETURN] . To redisplay your message after using ~? , enter ~p at the start of a line.

% mail alicia@moxco.chi.il.us

Subject: My trip to Chicago is on!

Alicia, I will be able to attend your meeting.
Please send me the agenda.  Thanks.


You can't cancel a message after you type [CTRL-D] . (Unless you're a system administrator and you're lucky to catch the message in time, that is.) So, if you change your mind about Alicia's meeting, you'll need to send her another message.

3.4.2 Reading Your Mail

To read your mail, simply enter mail (or the name of your system's email command) at the shell prompt. You can do this at any time during the UNIX session, not just when you log in.

Let's read Jerry's message to Alicia:

% mail

Mail version SMI 4.0 Wed Oct 23 10:38:28 PDT 1991  Type ? for help.
"/usr/spool/mail/alicia": 2 messages 1 new
>U  1 bigboss         Sat May 22 06:56  19/529  In your spare time
 N  2 jpeek@jpeek.com Tue Nov  4 14:25  14/362  My trip to Chicago
& 2

Message 2:
Date: Tue, 4 Nov 1997 14:25:43 EST
From: jpeek@jpeek.com (Jerry Peek)
To: alicia@moxco.chi.il.us
Subject: My trip to Chicago is on!
Alicia, I will be able to attend your meeting.
Please send me the agenda.  Thanks.
& d

& q

Held 1 message in /usr/spool/mail/alicia

When you start mail , it prints a "message header" that shows whether each message is "new" (N) or "unread" (U), a message number, the sender, and when the message was sent. A ">" marks the current message. You can read any message by entering its number; if you use a command without a number, the command acts on the current message. If you read a message and don't delete it, the message is automatically moved to a file called mbox in your home directory.

The output of mail says that the message was sent by Jerry on Tuesday, November 4, at 2:25 p.m. The ampersand (&) on the last line is the mail program prompt. Just as the UNIX shell prompt is a sign that the shell is waiting for you to enter a command, the mail prompt is a sign that the mail program is waiting for you to enter a mail command. Your mail prompt may consist of a single character. Learn the mail prompt on your system and enter one of the commands in Table 3.2 . For instance, Alicia might have chosen to enter r to reply to Jerry before using d to delete his message.

Table 3.2: Some mail Commands (at mail Prompt)
Command Description
? Display menu of mail commands.
# Show message number #.
n Display the next message.
p Display current message.

Delete the message. Messages you read and don't delete are saved in mbox .

m addrs

Mail a message to the addresses addrs .

r Reply to sender of current message.
R Reply to sender and other recipients of current message.
s file Save a message in the named file .
file file Handle the messages in the named file .
file % Handle the messages in your system mailbox.
h Display summary of messages.
q Quit the mail program.

Exit the mail program, ignoring any changes you made during this session. Exercise: Sending mail

You can practice sending mail to your friends in this exercise. List the users logged on to the system and choose a name. You can also use your username to send mail to yourself. Enter the following message. Do not forget to press the [RETURN] key at the end of each line, and type [CTRL-D] on a line by itself when you're done.

List logged-on users. Enter who
Send mail to someone. Enter mail name
Hi there!
I'm just trying
the mail program.

Previous: 3.3 Protecting and Sharing Files Learning the Unix Operating System Next: 3.5 Changing Your Password
3.3 Protecting and Sharing Files Book Index 3.5 Changing Your Password

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System