Chapter 7 Using Mail

SunOS provides a program called mailx for sending and receiving electronic mail (email). mailx provides features for reading, writing, sending, receiving, saving, and deleting messages. The mailx program is not window based, and can therefore be run on any terminal. Although you might prefer to use the window-based mail, you might find that the mailx program is handy when you are dashing off a quick note. Also, if you want to set up your own mail aliases, you can read about it here.

Note –

If you are in a window environment and the Mail Tool icon appears on your screen, quit from the Mail Tool before trying the examples in this chapter. Otherwise, you will have two mail processes active, and these concurrent processes might generate error and warning messages. You can safely send mail messages in a Command Tool or Shell Tool window, but if you read your mail and save or delete messages, this affects your “in tray,” thus confusing Mail Tool.

mailx Basics

This section describes how to perform basic tasks in mailx. Later sections describe features and functions that can enhance your ability to use this program.

An intended recipient's login name and machine name serve as a unique address for the mailx program. If the intended recipient is on the same machine as the sender, the login name is all that is required. Each user has a mailbox in which to receive mail. This mailbox is generally located in the /var/mail/username directory, where username is your login name.

The mailx program notifies you when you receive mail and places the mail in your mailbox. After you read your mail, mailx automatically places these messages in a storage file called mbox, which is also located in your home directory.

Starting mailx

Start mailx by typing the following command at a prompt and then pressing the Return key:

$ mailx

If you don't have any mail waiting for you, your terminal displays the message:

No mail for username $

In this message, username is your login name.

Sending Yourself a Sample Message

To see at a glance how mailx works, you can begin by sending yourself a sample message. At the prompt, give the mailx command again, but this time include your address (your login name plus your machine name). For example, if your login was rose and your machine name was texas, your address would be rose@texas. (The @ symbol is read as “at.”) You might be able to use just your login on a local network—consult your system administrator when in doubt.

$ mailx rose@texas

The program responds with a Subject: line:

$ mailx rose@texas
Subject:

If you like, type a word or two here about the content of the message you're sending yourself and press Return. Now type the body of the message. Use short lines and press Return at the end of each line. Note that you can only make corrections as you go by moving back a space and retyping lines before you press Return.

Your sample message might look something like this (the spaces between lines are made by pressing Return twice):

$ mailx rose@texas
Subject: to someone who really cares
Dear Rosey,
From the ends of your fingers
To the tip of your nose
You're a cool breeze in August
My sweet Texas Rose.
See you soon,
Rose

To send your sample message, press Return to complete the last line of the message and then press Ctrl-D. After your message has been sent, the system returns a command prompt.

The first line identifies the version of mail that you are running. The second line indicates your mailbox, usually located in /var/mail/username, where your incoming mail is deposited. The third line in this example is the header of the message you sent yourself. The “N” at the beginning of the line means that it's a “new” message. A “U” (unread) means the message was new, but was not read before quitting the mailx program previously. The information in this screen is discussed in greater detail in Reading Messages.

Every message is assigned a number as it is received: Rose's message to herself is shown as message number 1.

To read a message, type an ampersand (&) and the message number at the mailx prompt, as follows:

Quitting mailx

When you have finished your work in mailx, you can quit the program by using one of two commands: q (quit) or x (exit).

If you type q at the mailx prompt and then press Return, you see a message similar to the following:

Saved one message in home_directory/mbox.

In this message, home_directory is the path name to your home directory.

When you use q to quit mailx after reading messages, mailx moves the messages from your mailbox and saves them in the mbox file in your home directory. mailx also saves any changes or deletions you've made.

If you type x at the mailx prompt and then press Return, the mailx program does not save any changes or deletions, nor does it move any messages you've already read into the mbox file.

Reading Messages

If you have mail, mailx notifies you each time you log in with the message

You have mail

or

You have new mail

To read your messages, type mailx at a command prompt and press Return. If no mail is waiting for you, you will see the message:

The mailx program displays information about itself (version number and date) and instructions for getting help (Type ? for help).

On the next line, mailx specifies the location of your mailbox, the number of messages received, and their status.

Next, mailx shows a numbered list of the messages in your mailbox. From left to right, the columns in each line specify:

Status: Indicates whether a message is new (N), unread (U), or read (no symbol). A “>” at the beginning of a line indicates the current message. Deleted messages are marked with an asterisk (*).

Number: Indicates the order in which message was received.

Sender: Indicates the name of the user (and usually machine) the message came from.

Time: Indicates the date and time the message was sent.

Size: Indicates the number of lines or the number of characters in message.

Subject: Indicates the sender-designated subject of the message.

When you have several messages in your mailbox, the displayed list might not show all of your mail. If this situation applies, type:

z – To display the next screenful of mail headers

h- – To display the previous screenful of mail headers

h – To redisplay the list of mail headers at any time

To view the current message in the mailbox list (marked with >), press Return. Press Return again to display the next message. To read any message in the list, type its number and press Return.

Deleting (and Undeleting) Messages

After you have read a message, you might decide to delete it rather than save it to your mbox file. A save to the mbox file is the default when you quit the mailx program.

To delete the last message you read, just type d at the mailx prompt. To delete a specific message from your mailbox, use the command form:

dnumber

For example, to delete the second message, use this command from within mailx:

& d 2

You can also delete several messages at a time. To delete messages 1 and 3, use the command:

& d 1 3

To delete a range of messages, for example 1 through 3, use the command:

& d 1-3

Before you quit mailx, you can undelete messages you've removed from your mailbox. Use the command form:

unumber

followed by Return. For example, to undelete the second message, use this command:

& u 2

To undo your last deletion, just type u at the mailx prompt immediately after the deletion. For example, if your last deletion command was d 2-5, type u to undelete messages 2, 3, 4, and 5.

Note that all deletions are made permanent when you quit mailx with the q command. That is, you can no longer undelete messages that have been deleted. You can, however, quit mailx with the x command and leave your mailbox intact. As mentioned previously, if you quit with x you leave read messages marked with a U, deleted messages undeleted, and so forth.

Printing Messages

You can print a hard copy of a message by piping a message to a printer command. To do so, use the following command form at a mailx prompt:

|numberlp

The | symbol is called a pipe. For example, to print a copy of message 2, type:

Sending Messages

To send mail with the mailx program, you need to know the login name(s) of the intended recipient(s) of your message. If an intended recipient is on a different machine, you also need to know that user's machine name. To determine this information, you can use the who, finger, or rusers commands.

Type the who command to list all the users who are currently logged in to the file server you are on. The displayed list contains users' login names, their terminal types, and the date and time they logged in. For example:

Type the finger command to display the same type of information as who with more detail. The information that appears depends on how your system administrator has set up this command. As an example, you might see something like the following:

The rusers command provides information on the users who are currently logged in to your local network. Refer to Chapter 9, Using the Network for instructions on the use of the rusers command.

When you have determined the necessary user information, complete the following steps to send a message.

Type the mailx command, followed by a user's address:

$ mailx user@machine

In this command, user is the intended recipient's login name and machine is the name of the intended recipient's machine.

If you've already started mailx, you can just type m at the mailx prompt, followed by the intended recipient's login and machine name:

& m user@machine

To send the same message to multiple recipients, separate each address with a space or a comma, for example:

$ mailx hank@fretful sally@dakota tex@twister

or

$ mailx hank@fretful,sally@dakota,tex@twister

When you press Return, the mailx program prompts you for a subject. Type a subject for your message and press Return again.

Type the body of your message. To create a new line, press Return.

A sentence that wraps on your screen is not considered a new line until you press Return.

Note –

Each line of text within your message can be up to 256 characters long. When you exceed this limitation, your screen freezes. If this situation occurs, press Ctrl-C to abort your message.

When you have completed your message, press Return to move the cursor to a new line. Then press Ctrl-D to send your message.

Undeliverable Messages

If you specify an incorrect user address when you send a message, the system responds with the message

user@machine...User unknown

The message is then returned to your mailbox. The next time you type the mailx command, the header states that you have returned mail, similar to the following example:

N 1 Mailer-Daemon Fri Jan 3 11:13 8/49 Returned mail: User unknown

When a message cannot be delivered, the file is also copied to a file in your home directory named dead.letter.

Canceling an Unsent Message

You can cancel a message at any time before it is sent by pressing Ctrl-C twice.

Adding Carbon and Blind Carbon Copies

Before sending a message, you can specify that “carbon copies” be sent to other than the main addressees. You can also send “blind carbons.” This specification ensures that recipients of your message can read the addresses for the carbon copies, but not the addresses for the blind carbons.

Many people send themselves carbons or blind carbons in order to retain a copy for their own record.

You can use three methods for sending carbon copies with a message:

Use a text editor to edit your .mailrc file (in your home directory) and insert the following line:

set askcc

The mailx program now displays the carbon copy prompt (Cc:) after the subject prompt. Type the addresses of the users you want to receive carbon copies. Separate multiple addresses with spaces.

When you have typed the body of your message, but before you press Ctrl-D, press Return to move to a new line and use the command form:

~caddress(es)

To use this method to send carbon copies to multiple recipients, separate the addresses with spaces. For example:

~c hank@fretful george@lonesome stormy@snoozer

You can also create a Cc: line with the ~h command, which displays the entire header of the message. ~h prompts you with To:, Subject:, Cc:, and Bcc: (blind carbon copy) lines, one line at a time. You can fill in blank lines. You can also retype filled lines. As with other tilde commands, always use the ~h command on a new line.

Inserting a Copy of a Message or File

You can insert a copy of any message in your mailbox into the message you're writing. Likewise, you can insert a copy of any text file.

Inserting a Message

Use the following command form to insert a message.

~mnumber

In this example, number is the number of the message to be inserted. For example, to send to another user a message that includes a copy of message number 3 from your mailbox list, complete the following steps.

On a new line, type the command ~m 3, and then press Return.

mailx displays the following message.

Interpolating: 3 (continue)

You do not see the text of message 3, but the recipient will. You can continue to compose your message after (continue), or you can send it as is.

To see the complete message, interpolation included, type the command ~p.

Inserting a File

You can also insert a copy of any text file into a message. As you are writing a message, use the following command form.

~rfilename

For example, to insert the file outline in the current message, type the following command.

& ~r outline

Replying to a Message

Reply to mail at a mailx prompt by typing the following command.

rnumber

If you omit the message number, mailx replies to the current message.

For example, to reply to the sender of message 2, type the following command.

& r 2

mailx automatically addresses your message and supplies an Re: Subject: line that echoes the original Subject: line. Send your reply as you would with any other message.

R is a variant of the reply command that sends your reply to all recipients of the original message as well as to its sender. Use this command only when absolutely necessary, to avoid generating “junk mail.”

Note –

You can insert a message into your reply as shown in the previous section. To insert a copy of the message to which you are replying, type the command ~m without a message number.

Saving and Retrieving Messages

In addition to sending and receiving messages, you might also save and retrieve them for later use. In mailx you can save messages by appending them to regular text files. You can also append messages to special files called folders. Both methods are discussed subsequently.

mailx makes a distinction between saving messages and copying them. Saving removes a message from the mailbox and appends it to a file or folder. Copying leaves a message in the mailbox and appends a copy to a file or folder.

Saving and Copying Messages in Files

To save a message into a file, use the following command form at the mailx prompt.

snumberfilename

where number is the number of the message to be saved and filename is the file where you want to save the message. For example, to save message 3 into a file called ~/notes/finance, you would type:

& s 3 ~/notes/finance

Remember that in a path name, the ~ represents your home directory.

You can also save several messages at once to the same file. For example, to save messages 3, 5, 6, 7, and 8 to ~/notes/finance, type the following command.

& s 3 5-8 ~/notes/finance

If the file you specify does not exist, mailx creates it. If the file does exist, mailx appends the message you are saving to the end of the file.

Saving a file removes it from your mailbox. mailx displays an asterisk (*) next to the header of any message than has been saved.

To leave the message in your mailbox while appending it to another file, use the copy command, as follows:

& c 3 ~/notes/finance

Saving and Copying Messages in Folders

You can avoid typing full path names to files if you save or copy messages to mail folders. Folders are special files that are stored in a folder directory.

The advantage of saving or copying messages to folders is that your messages are automatically stored in the same directory, where they are easily accessible without typing long path names.

Setting the Folder Directory

To use folders, you must first set up a folder directory. Follow these steps to set up a folder directory.

Create a directory by using the mkdir command.

For example, if you wanted your folder directory to be called Messages, you would first create the directory:

$ mkdir Messages

Edit the .mailrc file in your home directory (which contains mailx options) to set the folder directory path.

Edit the folder variable to include the full path name of your newly created folder directory. For example:

set folder=/home/austin/rose/Messages

Or use the C shell shortcut ~ to specify your home directory.

set folder=~/Messages

Now your folder directory is set to receive messages that are saved in folders. The change to the .mailrc file becomes effective the next time you start mailx.

Designating Folders

You use the same commands to save or copy messages into folders as into files, except that the folder name is preceded by a plus sign (+) instead of a path name. The + tells mailx that the folder is to be kept in the folder directory (Messages).

Sending a Message Directly to a File or Folder

You can send copies of your messages directly to one of your files or folders. To send a copy to a folder, simply type the folder name in either the Cc: or the Bcc: field. Sending a copy to a file is similar, but you must include the full path name.

Reading Messages in Files and Folders

To read messages that are saved in a file, use the command form:

mailx-ffilename

In the previous example, you would read the file ~/memos/finance by typing:

$ mailx -f ~/memos/finance

You can read messages that are saved in a folder with a similar command—just use the + instead of a path name. For example, to read the messages in the folder projects, you would type:

$ mailx -f +projects

This command starts mailx in the file or folder that you have designated. Only headers for the messages in the file or folder are displayed. Select a message to read by typing its number at the mailx prompt, and then press Return.

You can also work on mail folders within the mailx program. To see a list of your folders, type this command at a mailx prompt:

& folders

To switch from your mailbox to a folder, use the command form:

& folder +foldername

To return to your mailbox, type this command at a mailx prompt:

& %

To return to the previous folder, type:

& #

Using vi With mailx

You can use the vi text editor to compose messages while you run mailx. vi enables you to correct mistakes and add and delete text before you send your messages. If you are not already familiar with using vi, refer to Chapter 6, Using the vi Editor for instructions.

In the mailx program, you can use the standard vi commands for inserting, deleting, and changing text.

To write a message with vi:

Type the mailx command with an address at either the mailx prompt (&) or the command prompt ($).

After you quit vi, mailx displays the (continue): message. You can either add to the message (outside vi) or send the message by pressing Ctrl-D.

Mail Aliases

A mailalias is a selection of user names that are grouped under a single name.

Use mail aliases when you want to send messages to the same group of people over and over. For example, if you occassionally send mail to hank@fretful, george@lonesome, and sally@dakota, you could create a mail alias called amigos. Then, each time you sent mail to amigos, all three people would receive it.

Two locations where you can set up mail aliases are:

Your .mailrc file

The /etc/aliases file

Mail aliases that are set up in .mailrc behave differently from mail aliases set up in /etc/aliases. These differences are summarized in Table 7-1 at the end of this section.

Setting Up Mail Aliases in .mailrc

Note the following about setting up mail aliases in .mailrc:

Mail aliases in .mailrc are private. That is, only you can use them. For example, if you set up a mail alias called amigos in .mailrc and another user tries to send mail to amigos, he receives an unknown user error message.

When the mail is sent, .mailrc aliases are automatically expanded to show everyone on the mail alias. For example, if you send mail to amigos, your mail arrives as though you had typed everyone's names as recipients. The recipients do not know that you used a mail alias to send the mail.

.mailrc is located in your home directory. This file contains several settings that control the behavior of mailx and Mail Tool.

To add a mail alias to .mailrc, type:

$ vi ~/.mailrc

Note –

You can use any text editor to edit the .mailrc file. The previous example shows the method for using the vi editor to edit the file. If you are not already familiar with vi, refer to Chapter 6, Using the vi Editor for instructions.

Each mail alias is contained on one line of the file. That is, the line can visually “wrap around” to another line, but it cannot contain carriage returns. Each mail alias should contain the following, separated by spaces:

The word “alias”

The name of the mail alias (must be one word)

The recipients (logins and machine names) in the mail alias, separated by spaces

The following example shows two mail aliases. The first alias (amigos) contains three people. The second alias (softball) contains eight. Notice in softball how the names are visually wrapped around on the screen. This is fine, as long as no carriage returns are used.

Setting Up Mail Aliases in /etc/aliases

Mail aliases in /etc/aliases are public. This means that if you set up a mail alias called softball, anyone can send to softball@your-machinename and make use of the mail alias.

When the mail is sent, /etc/aliases mail aliases are not expanded. For example, if you send mail to softball@machinename, that's how the mail reads when it is received. The recipients know what the mail alias is, but not necessarily who else is on it.

The format of mail aliases that are created in /etc/aliases is somewhat different from those in .mailrc. Each /etc/aliases alias should use the following format:

The name of the mail alias, followed by a colon (:)

The recipients (logins and machine names), separated by commas. Note that the mail alias does not have to be on a single line.

To modify your /etc/aliases file, you must first become root. If root is password protected, you'll need to know the root password.

Type the following to become the root user on the system:

$ su
Password:
#

Notice that the command prompt changes when you become root.

The following example shows how the alias softball@texas is added to the default /etc/aliases file.

# vi /etc/aliases
##
#Aliases can have any mix of upper and lower case on the left-
#hand side,
#but the right-hand side should be proper case (usually lower)
#
# >>>>>>>>>>The program “newaliases” will need to be run after
# >> NOTE >>this file is updated for any changes to
# >>>>>>>>>>show through to sendmail.
#
#@(#)aliases 1.10 89/01/20 SMI
##
# Following alias is required by the mail protocol, RFC 822
# Set it to the address of a HUMAN who deals with this system's
mail problems.
Postmaster: root
# Alias for mailer daemon; returned messages from our MAILER-
DAEMON
# should be routed to our local Postmaster.
MAILER-DAEMON: postmaster
# Aliases to handle mail to programs or files, eg news or vacation
# decode: “|/usr/bin/uudecode”
nobody: /dev/null
# Sample aliases:
# Alias for distribution list, members specified here:
#staff:wnj,mosher,sam,ecc,mckusick,sklower,olson,rwh@ernie
# Alias for distribution list, members specified elsewhere:
#keyboards: :include:/usr/jfarrell/keyboards.list
# Alias for a person, so they can receive mail by several names:
#epa:eric
#######################
# Local aliases below #
#######################
softball@texas: earl@woofer
tex@twister elmer@farmhouse
jane@freeway hank@fretful jj@walker sally@dakota steve@hardway
:wq (to quitviand save the/etc/aliasesfile )
# exit (to exit root)
$

You can use any text editor to edit the /etc/aliases file. The previous example shows the method for using the vi editor to edit the file. If you are not already familiar with vi, refer to Chapter 6, Using the vi Editor for instructions.

Note that the pound signs (#) you see within the /etc/aliases file have been placed there to comment out the text and sample aliases. The pound signs prevent the system from processing this information as actual aliases.

Do not place pound signs in front of aliases you add to this file, unless you intentionally want to disable an alias.

To send mail to people on a /etc/aliases alias, address the mail by using the name of the alias and your machine name. Suppose that you sent the following:

$ mail softball@texas
Subject: Practice Today
Let's meet at the diamond
behind Building 4 after work tonight.
Goodness knows we can use the practice for Saturday's game! Be
there as early as you can.

The recipients would see the following:

To: softball@texas
Subject: Practice Today
Let's meet at the diamond behind Building 4 after work tonight.
Goodness knows we can use the practice for Saturday's game! Be
there as early as you can.

Notice that the To: line is not expanded.

Whenever you send mail by using a mail alias of this type, be sure to include the machine name of the machine on which it's located. If you set up a mail alias called riders on the machine freeway, then you should send your mail to riders@freeway.

Table 7-1 provides a summary comparison between mail aliases that are created in .mailrc and those that are created in /etc/aliases.

Table 7–1 Comparing Mail Aliases in .mailrc and /etc/aliases

.mailrc

/etc/aliases

Must be root to modify?

no

yes

Send message to:

alias

alias@machinename

Recipients list seen by recipients?

yes

no

Names separated by commas?

no

yes

Names all on one line?

yes

no

Others can use the mail alias?

no

yes

For more detailed information on mail aliases, type manaliases or manaddresses at the system prompt.

Tilde Commands

In the course of composing a message, you can use tilde commands to perform a variety of functions. Tilde commands usually consist of the tilde character (~) followed by a single character. The following table describes some of the more useful tilde characters. Some of these characters have already been introduced in this chapter.

Note –

If you want to include a literal tilde character in a message, type two tildes in succession. Only one tilde is displayed.

Table 7–2 Tilde Commands (mailx)

Command

Description

~!command

Escapes to a shell command.

~.

Simulates pressing Ctrl-D to mark end of file.

~?

Lists a summary of tilde commands.

~busername

Adds user name(s) to the blind carbon copies (Bcc:) list.

~cusername

Adds user name(s) to the carbon copies (Cc:) list.

~d

Reads the contents of the dead.letter file into current message.

~fnumber

Forwards the specified message. Valid only when you send a message while you read mail.

~h

Prompts for header lines: Subject, To, Cc, and Bcc.

~mnumber

Inserts text from the specified message into the current message. Valid only when you send a message while you read mail.

~p

Prints to the screen the message that you are typing.

~q

Simulates pressing Ctrl-C twice. If the body of the current message is not empty, the contents are saved to dead.letter.

~rfilename

Reads in the text
from the specified file.

~sstring

Changes the subject line to string.

~tname

Adds the specified name(s) to the To list.

~wfilename

Writes the current message without the header into the specified file.

~x

Exits mailx. Similar to ~q except message is not saved in the dead.letter file.

Getting Help: Other mailx Commands

mailx has two help commands that display lists of commands and functions. When in command mode, you can type ? at the mailx prompt (&) to see a list of commands used in that mode. Similarly, when in input mode (for example, when writing a message), you can use the equivalent command, ~? to view a list of tilde commands (also called “tilde escapes”).

The man pages contain extensive information on mailx in more technical form. To see this entry, type the command: