OpenWindows Advanced User's Guide

Chapter 7 Using Mail

SunOS provides a program called mailx for sending and receiving electronic mail (email). mailx provides facilities 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 may prefer to use the window-based mail, you may 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're in the OpenWindows 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 this may 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 will affect your "in tray," thus confusing Mail Tool.


7.1 mailx Basics

In this section you will learn just enough about mailx to get by. In later sections you will learn about features and functions that will greatly 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've read your mail, mailx automatically places these letters in a storage file called mbox, which is also located in your home directory.

7.1.1 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 will display the message:

No mail for username$

where username is your login name.

7.1.2 Sending Yourself a Sample Letter

To see at a glance how mailx works, you can begin by sending yourself a sample letter. 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 may be able to use just your login on a local network--consult your system administrator when in doubt.

$ mailx rose@texas

The program will respond with a Subject: line:

$ mailx rose@texas
Subject:

If you like, type in a word or two here about the content of the letter you're sending yourself and press Return. Now type the body of the letter; use short lines and press Return at the end of each line. (Note that you can only make corrections as you go by backspacing and retyping lines before you press Return.)

Your sample letter 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 letter, press Return to complete the last line of the letter and then press Ctrl-D. After your letter has been sent, the system returns a command prompt.

7.1.3 Reading Your Sample Letter

To read your sample letter, give the mailx command again. Your screen will probably look something like this:

$ mailx
Mail version 4.0 Thu Jan 16 12:59:09 PST 1992  Type ? for help.
"/var/mail/rose": 2 messages 1 new
 U  2 hal@uncertain   Fri Feb 14 12:01   14/318 financial status
>N  1 rose@texas      Mon Feb 17 08:12   21/453 to someone who
&

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 letter you sent yourself. The "N" at the beginning of the line means that it's a "new" letter. A "U" (unread) means the letter was new, but was not read before quitting the mailx program previously. (The information in this screen is discussed in greater detail in "7.2 Reading Letters", in this chapter.)

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

To read a letter, type its number at the mailx prompt, the ampersand (&), as follows:

$ mailx
Mail version 4.0 Thu Jan 16 12:59:09 PST 1992  Type ? for help.
"/var/mail/rose": 1 message 1 new
>N  1 rose@texas   Fri Jul 14 12:01 21/453 to someone who
& 1

To: rose@texas
From: rose@texas
Subject: to someone who really cares

Dear Rose,

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

&

7.1.4 Quitting mailx

When you're finished using mailx, you can quit the program using one of two commands: q (quit) or x (exit).

If you type q at the mailx prompt and then press Return,

& q

you will then see a message similar to the following:

Saved one message in home_directory/mbox.

where home_directory is the path name to your home directory.

When you use q to quit mailx after reading messages, mailx moves the letters 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,

& x

the mailx program does not save any changes or deletions, nor does it move any letters you've already read into the mbox file.

7.2 Reading Letters

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 letters, type mailx at a command prompt and press Return. If there's no mail waiting for you, you will see the message:

No mail for username

Otherwise, you will see a list similar to the following:

$ mailx
Mail version 4.0 Thu Jan 16 12:59:09 PST 1992  Type ? for help.
"/var/mail/rose": 4 messages 1 new 2 unread
    1 rose@texas        Fri Feb 14 12:01 21/453 to someone who
 U  2 hank@fretful      Fri Feb 14 18:31 19/353 so lonely I
 U  3 farmer@freeway    Sat Feb 15 10:22 24/557 looks like my
>N  4 hoover@woofer     Sun Feb 16 23:59 14/280 big old furry

&

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 letters received, and their status.

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

When you have a large number of letters in your mailbox, the displayed list may not show all of your mail. If this is the case, type:

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

7.3 Deleting (and Undeleting) Letters

When you are finished reading a letter, you may decide you want to delete it rather than have it saved to your mbox file (the default when you quit the mailx program).

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

d number

For example, to delete the second letter, give this command from within mailx:

& d 2

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

& d 1 3

To delete a range of letters, for example 1 through 3, give the command:

& d 1-3

Before quitting mail, you can undelete letters you've removed from your mailbox. Use the command form:

u number

followed by Return. For example, to undelete the second letter, give this command:

& u 2

If you want 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, typing u will undelete messages 2, 3, 4, and 5.

Note that all deletions are made permanent when you quit mailx with the q command; that is, deleted letters can no longer be undeleted. You can, however, quit mailx with the x command, leaving your mailbox intact -- as mentioned previously, quitting with x will leave read letters marked with a U, deleted letters undeleted, and so forth.

7.4 Printing Letters

You can print a hard copy of a letter by piping a letter to a printer command. To do this, use the command form:

|number lp

at a mailx prompt. (The | symbol is called a pipe.) For example, to print a copy of letter 2, type:

& |2 lp

and press Return. If a letter number is not specified, mailx pipes the current letter to the printer. For more information on piping, see "2.2.5 Redirecting and Piping Command Output" in Chapter 2, Basic SunOS Commands.

7.5 Sending Letters

To send mail with the mailx program, you need to know the login name(s) of the intended recipient(s) of your letter. 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.

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

$ who
    elmer      tty15        Feb 20 10:22
    susan      tty04        Feb 20 10:37
    stormy     tty07        Feb 20 11:49
    hankw      tty06        Feb 20 12:02

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

$ finger
    Login     Name           TTY      Idle     When
    elmer     Elmer Brown    tty15     43      Thu 10:22
    susan     Susan Lake     tty04             Thu 10:37
    stormy    Stormy Ball    tty07     12      Thu 11:49
    hankw     Hank Wilson    tty06     22      Thu 12:02

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

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

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

    $ mailx user@machine
    

    where 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 letter 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
      

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

  3. Type the body of your letter. When you want 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 letter can be up to 256 characters long. When you exceed this limitation, your screen will freeze. If this occurs, press Ctrl-C to abort your letter.


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

7.5.1 Undeliverable Letters

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

user@machine...User unknown

and the letter is 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 letter cannot be delivered, the file is also copied to a file in your home directory named dead.letter.

7.5.2 Canceling an Unsent Letter

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

7.5.3 Adding Carbon and Blind Carbon Copies

Before sending a letter, you can specify that "carbon copies" be sent to other than the main addressees. You can also send "blind carbons." (Recipients of your letter 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.

There are three methods for sending carbon copies with a letter:

~c hank@fretful george@lonesome stormy@snoozer

7.5.4 Inserting a Copy of a Letter or File

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

7.5.4.1 Inserting a Letter

The command form to insert a letter is

~m number

where number is the number of the letter to be inserted. For example, to send a letter to another user that includes a copy of letter number 3 from your mailbox list, you would do the following:

  1. On a new line give the command ~m 3 and then press Return.

  2. mailx displays the message, Interpolating: 3 (continue)

  3. You won't see the text of message 3, but the recipient will. You can go on with your letter after (continue), or you can send it as is.

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

7.5.4.2 Inserting a File

You can also insert a copy of any text file into a letter. Use the command form:

~r filename

as you're writing a letter. For example, to insert the file outline in the current letter, type:

~r outline

7.5.5 Replying to a Letter

Reply to mail by giving the command

r number

at a mailx prompt. (If you omit the letter number, mailx replies to the current letter.) For example, to reply to the sender of letter 2, give the command:

& r 2

mailx automatically addresses your letter and supplies an Re: Subject: line that echoes the original Subject: line. Send your reply like any other letter.

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


Note -

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


7.6 Saving and Retrieving Letters

In addition to sending and receiving letters, you may also want to save and retrieve them for later use. In mailx you can save letters by appending them to regular text files; you can also append letters to special files called folders. Both methods are discussed below.

mailx makes a distinction between saving letters and copying them; saving removes a letter from the mailbox and appends it to a file or folder; copying leaves a letter in the mailbox and appends a copy to a file or folder.

7.6.1 Saving and Copying Letters in Files

To save a letter into a file, the command form at the mailx prompt is:

s number filename

where number is the number of the letter to be saved and filename is the file where you want to save the letter. For example, to save letter 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 letters at once to the same file. For example, to save letters 3, 5, 6, 7, and 8 to ~/notes/finance, you would type:

& 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 letter 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 letter than has been saved.

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

& c 3 ~/notes/finance

7.6.2 Saving and Copying Letters in Folders

You can dispense with typing full pathnames to files if you save or copy letters to mail folders. Folders are special files that are stored in a folder directory.

The advantages to saving or copying letters to folders is that your letters are automatically kept together in the same directory, where they are easily accessible without typing long pathnames.

7.6.2.1 Setting the Folder Directory

To use folders, you must first set up a folder directory. This is a two-step process:

  1. First, make the directory using the mkdir command.

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

    $ mkdir Letters
    

  2. Second, use a text editor to edit the .mailrc file in your home directory (which contains mailx options) to set the folder directory path.

    Here you need to edit the folder variable to include the full path name of your newly created folder directory. For example:

    set folder=/home/austin/rose/Letters
    

    or, using the C shell shortcut ~ to specify your home directory.

    set folder=~/Letters
    

Now your folder directory is set to receive letters saved in folders. (The change to the .mailrc file will take effect the next time you start mailx.)

7.6.2.2 Designating Folders

You use the same commands to save or copy letters 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 (Letters).

For example, to save letter 3 to a folder called projects, type:

& s 3 +projects

mailx interprets this command as meaning "save letter 3 into ~/Letters/projects." (If the folder doesn't already exist, mailx will create it.)

Copy the letter into a folder by typing:

& c 3 +projects

7.6.2.3 Sending a Letter Directly to a File or Folder

You can send copies of your letters 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.

7.6.3 Reading Letters in Files and Folders

To read letters saved in a file, use the command form:

mailx -f filename

Using the example above, you would read the file ~/memos/finance by typing:

$ mailx -f ~/memos/finance

You can read letters saved in a folder with a similar command--just use the + instead of a pathname. For example, to read the letters in the folder projects, you would type:

$ mailx -f +projects

This command starts mailx in the file or folder designated. Only headers for the letters in the file or folder are displayed. Select a letter to read by typing its number at the mailx prompt and pressing Return.

You can also work on mail folders within the mailx program. To see a list of your folders, type this 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 at a mail prompt:

& %

To return to the previous folder, type:

& #

7.7 Using vi with mailx

You can use the vi text editor to compose letters while running mailx. This gives you the capability of correcting mistakes and adding and deleting text before you send your letters. 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 letter with vi:

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

  2. Type in the subject at the Subject: line. Press Return.

  3. Start vi by giving the command ~v on a new line. The vi screen will appear, representing an empty file in your /tmp directory.

  4. Use vi commands to input and edit the body of your letter.

  5. When done, quit vi with the command :wq or ZZ.

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

7.8 Mail Aliases

A mail alias is a selection of user names grouped under a single name.

Use mail aliases when you want to send letters to the same group of people over and over. For example, if you wanted to send mail from time to time 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.

There are two locations where you can set up mail aliases:

Mail aliases 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.

7.8.1 Setting Up Mail Aliases in .mailrc

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

.mailrc is located in your home directory. This file contains a number of 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 example above 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, it can visually "wrap around" to another line, but it cannot contain carriage returns. Each mail alias should contain the following, separated by spaces:

The example below shows two mail aliases. The first (amigos) contains three people. The second (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.

alias amigos hank@fretful george@lonesome sally@dakota
alias softball earl@woofer tex@twister elmer@farmhouse
jane@freeway hank@fretful jj@walker sally@dakota steve@hardway

To send mail to people on a .mailrc alias, just address it to the mail alias name. Do not include your machine name. For example, if you sent the following:

$ mail amigos
Subject: Let's eat

Hey Compadres. How about
getting together for lunch on Friday?
Anyone interested?

the recipients would see the following (note the expanded To: line):

To: hank@fretful george@lonesome sally@dakota
Subject: Let's eat

Hey Compadres. How about getting together for lunch on Friday?
Anyone interested?

7.8.2 Setting Up Mail Aliases in /etc/aliases

Note the following about setting up mail aliases in /etc/aliases:

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

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 quit viand save the  /etc/aliasesfile )
# exit      (to exit root)
$

You can use any text editor to edit the /etc/aliases file. The example above 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 using the name of the alias and your machine name. For example, if 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 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 created in .mailrc and those 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 man aliases or man addresses at the system prompt.

7.9 Tilde Commands

In the course of composing a letter, 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 have already been introduced in this chapter.


Note -

If you want to include a literal tilde character in a letter, type two tildes in succession. Only one tilde will be displayed.


Table 7-2 Tilde Commands (mailx)

Command 

Function 

~!command

Escapes to a shell command 

~.

Simulates pressing Ctrl-D to mark end of file 

~?

Lists a summary of tilde commands 

~b username

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

~c username

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

~d

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

~f number

 

Forwards the specified letter. Valid only when sending a message while reading mail. 

~h

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

~m number

 

Inserts text from the specified letter into the current letter. Valid only when sending a message while reading mail. 

~p

Prints the message being entered to the screen. 

~q

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

~r filename

Reads in the text from the specified file. 

~s string

Changes the subject line to string.

~t name

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

~w filename

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

~x

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

7.10 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. Likewise, when in input mode (for example, when writing a letter), you can give 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, give the command:

$ man mailx

or refer to the man Pages(1): User Commands.