## 6.5. Electronic Mail

You may see a notice that says "You have mail" when you first log in to your system, or later, before a shell prompt. 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 also can read any messages that others may have sent to you.

There are a lot of email programs for Unix. If you'll use email often, we recommend that you start with whatever program other people in your group use.

We start with a brief section on addressing email. Next, you'll see how to send mail from a shell prompt with Berkeley mail. Then we introduce sending and reading mail with Pine, a popular menu-driven program that works without a window system. If you'd like to try a graphical program (which we won't discuss here), many web browsers have an email window. All programs' basic principles are the same though, and they all can send and receive messages from each other.[19]

> [19] Microsoft Windows users have an unfortunate habit of sending email "attachments" made with a Windows-specific program like Microsoft Word. On Unix systems, you can read these messages using popular word processing programs such as StarOffice, but it can be a pain. You might ask Windows users to send plain text messages, which everyone can read without special software.

### 6.5.1. Addressing an Email Message

Most addresses have this syntax:

username@hostname

username is the person's username, like jerry, and hostname is either the name of his computer or a central domain name for his entire organization, like oreilly.com. On many Unix systems, if the recipient reads email on the same computer you do, you may omit the @hostname. (An easy way to get a copy of a message you send is to add your username to the list of addressees.)

### 6.5.2. Sending Mail from a Shell Prompt

Most Unix systems have a fairly simple program from Berkeley Unix called Mail (with an uppercase "M"), mailx, or just mail. If you enter just the program name at a shell prompt, you can read your email, but its terse interface isn't very friendly. If you enter the program name, followed by addresses as arguments, you can send an email message. This is handy for sending a quick message from your keyboard. But it's best used with redirection (explained in Chapter 5) to email the output of a program or the contents of a file.

To send mail, give the address of each person you want to send a message to, such as one of the following:

Mail address1 address2 ...

mailx address1 address2 ...

mail address1 address2 ...

> WARNING: It's best to use simple addresses such as username@hostname on the command line. More complex addresses--with peoples' names or special characters such as `<` and `>`--can cause trouble unless you know how to deal with them.

After you enter mail and the addresses, if you're sending a message from the keyboard, in most cases the program (depending on how it's set up) prompts you for the subject of the message. Many versions of the program also accept a subject as a command-line argument after the -s option; be sure to put quote marks around the subject! Here are two examples of redirection: first sending the restaurant list you made in an earlier example, then sorting the list before you send it:

>     $ mail -s "My favorite restaurants" jerry@oreilly.com < food
>     $ sort food | mail -s "My favorite restaurants" jerry@oreilly.com

If you've redirected the standard input from a pipe or file, as in these two examples, your message will be delivered. Otherwise, mail will wait for you to enter the message body. Type in your message, line by line, pressing RETURN after every line. When you've finished entering text, type CTRL-D (just once!) at the start of a new line. You should get the shell prompt at this point, though it might take a few seconds.

>     $ mail alicja@moxco.chi.il.us
>     Subject: My Chicago trip
>     Alicja, I will be able to attend your meeting.
>     Please send me the agenda.  Thanks.

>     Jerry
>     ^D
>     $

If you change your mind before you type CTRL-D, you can cancel a message (while you're still entering text) with your interrupt character (see Section 1.1.8 in Chapter 1). The cancelled message may be 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.

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). So, if you change your mind about Alicja's meeting, you'll need to send her another message.

Please try the previous examples, substituting your address for the sample addresses shown. Once you've found the correct program name and the email address you can use to send a message to yourself, write them down. You'll probably find this is a very useful way to send yourself little reminder messages, the contents of files, and the outputs of programs:

___________ Name of email program that sends from a shell prompt

_________________________ My email address

### 6.5.3. Reading Email with Pine

Pine, from the University of Washington, is a popular program for reading and sending email from a terminal. It works completely from your keyboard; you don't need a mouse. Pine isn't a standard part of all Unix systems; if you don't have it, you can use other email programs. If you read this introduction but don't have Pine, ask your system staff to download and install it. Like most Unix software, Pine is free.

Start Pine by entering its name at a shell prompt. It also accepts options and arguments on its command line; to find out more, enter pine -h ("help"). If new email is waiting for you, but you want to experiment with Pine without taking chances, the -o (lowercase letter "O") option makes your inbox folder read-only; you won't be able to change the messages in it until you quit Pine and restart without the -o. Figure 6-5 shows the starting display, the main menu.

The highlighted line, which is the default command, gives a list of your email folders.[20] You can choose the highlighted command by pressing RETURN, pressing the greater-than sign >, or typing the letter next to it (here, l, a lowercase L; you don't need to type the commands in uppercase). But since you probably haven't used Pine before, the only interesting folder is the inbox, which is the folder where your new messages wait for you to read them.

> [20] Recent versions of Pine also let you read Usenet newsgroups. The L command takes you to another display where you choose the source of the folders, then you see the list of folders from that source. See Section 6.6, later in this chapter.

#### Figure 6-5. Pine main menu

The bottom of the display in Figure 6-5 shows that there are two messages waiting. Let's go directly to the inbox by pressing I (or by highlighting that line in the menu and pressing RETURN) to read the new mail. Figure 6-6 has the message index for Alicja's inbox.

#### Figure 6-6. Pine message index

The main part of the window is a list of the messages in the folder, one message per line. If a line starts with N, like the second message does, it's a new message that hasn't been read before. (The first message has been sitting in the inbox for some time now...) Next on each line is the message number; messages in a folder are numbered 1, 2, and so on. That's followed by the date the message was sent, who sent it, the number of characters in the message (size), and finally the message subject.

At the bottom of the display is Pine's reminder list of commands. When you aren't sure what to do, this is a good place to look. If you don't see what you want here, pressing O (the letter "o"; lowercase is fine) shows you more choices. For more information, ? gives detailed help.

Let's skip this first message and read the next one, number 2. The down-arrow key or the N key moves the highlight bar over that message. As usual, you can get the default action--the one shown in brackets at the bottom of the display (here, `[ViewMsg]`)--by pressing RETURN or >. The message from Jerry will appear.

Just as > took us forward in Pine, the < key generally takes you back to where you came from--in this case, the message index. You can type R to reply to this message, F to forward it (send it on to someone else), D to mark it for deletion, and TAB to go to the next message without deleting this one.

When you mark a message for deletion, it stays in the folder message index, marked with a `D` at the left side of its line, until you quit Pine. Type Q to quit. First Pine asks if you really want to quit. If you've marked messages for deletion, Pine asks if you want to expunge ("really delete") them. Answering Y here actually deletes the message.

There's much more to Pine than we can cover here. For instance, it lets you organize mail in multiple folders, print, pipe (output) messages to Unix programs, search for messages, and more. Recent versions of Pine can access mail folders on other computers using IMAP; this lets you use Pine (and other email programs) on many computers, but keep one main set of mail folders on a central computer.

### 6.5.4. Sending Email with Pine

If you're sending a quick message from a shell prompt, you may want to use the method shown in Section 6.5.2 earlier in this chapter. For a more interactive way to send email, try Pine. We'll take a quick tour.

If you've already started Pine, you can compose a message from many of its displays by typing C. (Though, as always, not every Pine command is available at every display.) You can also start from the main menu. Or, at a shell prompt, you can go straight into message composition by typing "pine addr1 addr2", where each addr is an email address like jerry@oreilly.com. In that case, after you've sent the mail message, Pine quits and leaves you at another shell prompt.

When you compose a message, Pine puts you in a window called the composer. (You'll also go into the composer if you use the Reply or Forward commands while you're reading another mail message.) The composer is a lot like the Pico editor, but the first few lines are special because they're the message header--the "To:", "Cc:" (courtesy copy), "Attchmnt:" (attached file), and "Subject:" lines. Figure 6-7 shows an example, already filled in.

#### Figure 6-7. Pine composer

As you fill in the header, the composer works differently than when you're in the message text (body of the message). The list of commands at the bottom of the window is a bit different in those cases, too. For instance, while you edit the header, you can attach a file to the end of the message with the "Attach" command, which is CTRL-J. However, when you edit the body, you can read a file into the place you're currently editing (as opposed to attaching it) with the CTRL-R "Read File" command. But the main difference between editing the body and the header is the way you enter addresses.

If you have more than one address on the same line, separate them with commas (`,`). Pine will rearrange the addresses so there's just one on each line, as shown in Figure 6-7.

There are several ways to give the composer the addresses where the message should be sent:

- Type the full email address, like jpeek@jpeek.com.

- If you're sending email to someone who uses the same computer you do, type their username. Pine will fill in `@``hostname` as soon as you move the cursor to the next line.

- Type a nickname from the address book. (See Section 6.5.4.1, later in this chapter.)

Move up and down between the header lines with CTRL-N and CTRL-P, or with the up-arrow and down-arrow keys, just as you would in Pico. When you move into the message body (under the "Message Text" line), type any text you want. Paragraphs are usually separated with single blank lines.

> NOTE: If you put a file in your home directory named .signature (the name starts with a dot, "`.`"), the composer automatically adds its contents to the end of every message you compose. (Some other Unix email programs work the same way.) You can make this file with a text editor like Pico, or from the Pine setup menu (see Section 6.5.5, later in this chapter). It's good Internet etiquette to keep this file short--no more than four or five lines, if possible.

You can use familiar Pico commands such as CTRL-J to justify a paragraph and CTRL-T to check your spelling. When you're done, CTRL-X ("exit") leaves the composer, asking first if you want to send the message you just wrote. Or CTRL-C cancels the message, though you'll be asked if you're sure. If you need to quit, but don't want to send or cancel, the CTRL-O command postpones your message; then, the next time you try to start the composer, Pine asks whether you want to continue the postponed composition.

### 6.5.4.1. Pine address book

The Pine address book can hold peoples' names and addresses, as well as a nickname for each person. When you compose a message, enter peoples' nicknames in the message header; Pine replaces that with the full name and address.[21]

> [21] Recent versions of Pine also let you store your address book on a central server, in order for you to access it, from whatever other computer you're using at the moment, via IMAP.

You can enter information by hand from the main menu by choosing A ("address book"), then adding new entries and editing old ones. Also, as you read email messages that you've received, the T ("take address") command makes new address book entries for that message's addressees.

Figure 6-8 shows the address book entry form. Edit each line as you would in the composer, then use CTRL-X to save the entry. The "Fcc" line gives the name of an optional Pine folder; when you send a message to this address book entry, Pine puts a copy in this folder. (If you leave "Fcc" blank, Pine uses the sent-mail folder.) All lines except nickname and address are optional.

#### Figure 6-8. Pine address book entry

Once you've saved that address book entry, if you go into the composer and type the nickname Jerry, here's the header you get automatically:

>     To      : Jerry Peek <jpeek@jpeek.com>
>     Cc      :
>     Fcc     : authors
>     Attchmnt:
>     Subject :

### 6.5.5. Configuring Pine

The Pine main menu (shown in Figure 6-5) has a Setup entry for configuring Pine. We assume that your system staff has configured important options, like your printer command, and we look at a few other settings you might want to change.

After you enter S (the "Setup" command), you can choose what kind of setup you want to do. From the setup screen, you can get to the option configuration area with C (the "Config" command).

The configuration screen has page after page of options. You can page through them with the space bar (to move forward one page), the - key (back one page), the N key (to move forward to the next entry), and the P key (back to the previous entry). If you know the name of an option you want to change, you can search for it with W (the "Whereis" command).

When you highlight an option, the menu of commands at the bottom of the screen will show you what can do with that particular option. A good choice, while you're exploring, is the ? ("Help") command, to find out about the option you've highlighted. There are several kinds of options:

- Options with variable values: names of files, hostnames of computers, and so on. For example, the personal-name option sets the name used in the "From:" header field of mail messages you send. The setup entry looks like this:

  >     personal-name        = <No Value Set: using "Robert L. Stevenson">

  "No Value Set" can mean that Pine is using the default from the system-wide settings, as it is here. If this user wants his email to come from "Bob Stevenson," he could use the C ("Change Val") command to set that name.

- Options that set preferences for various parts of Pine. For instance, the enable-sigdashes option in the "Composer Preferences" section puts two dashes and a space on the line before your default signature. The option line looks like this:

  >     [X]  enable-sigdashes

  The "X" means that this preference is set, or "on." If you want to turn this option off, use the X ("Set/Unset") command to toggle the setting.

- For a few options, you can choose one of many possible settings. The option appears as a series of lines. For instance, the first few lines of the saved-msg-name-rule option look like this:

  >     saved-msg-name-rule      =
  >                 Set    Rule Values
  >                 ---  ----------------------
  >                 (*)  by-from
  >                 ( )  by-nick-of-from
  >                 ( )  by-nick-of-from-then-from
  >                 ( )  by-fcc-of-from
  >                 ( )  by-fcc-of-from-then-from

  The "*" means that the saved-msg-name-rule option is currently set to by-from. (Messages will be saved to a folder named for the person who sent the message.) If you wanted to choose a different setting--for instance, by-fcc-of-from--you'd move the highlight to that line and use the * ("Select") command to choose that setting.

  These settings are trickier than the others, but the built-in help command ? explains each choice in detail. Start by highlighting the option name (here, saved-msg-name-rule) and reading its help file. Then look through the settings' names, highlight one you might want, and read its help file to see if it's right for you.

When you exit the setup screen with the E command, Pine asks you to confirm whether you want to save any option changes you made. Answer N if you were just experimenting or aren't sure.

### 6.5.5.1. Exercise: sending and reading mail

You can practice sending and reading mail in this exercise:

List logged-in users. | Enter `who`
Choose a user you know, else choose yourself; send a short message to that person using mail or your favorite email program. | Enter `mail` `username` or `pine` `username` or ...
Read the message or messages you got. | Enter `pine` or start your favorite email program; use its "read message" commands.
Reply to one of the messages. (It's okay to reply to a message from yourself.) | Press `R` in pine or use your email program's "reply" command. Send the completed reply.
Forward one of the messages. (It's okay to forward a message to yourself.) | Press `F` in pine or use your email program's "forward" command. Add a sentence or two of explanation above the forwarded message. Send the completed message.

Copyright © 2003 O'Reilly & Associates. All rights reserved.