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.
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.
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.
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.
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.
To read your sample letter, give the mailx command again. Your screen will probably look something like this:
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:
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.
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:
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:
Status: indicates whether a letter is new (N), unread (U), or read (no symbol). A ">" at the beginning of a line indicates the current letter. Deleted letters are marked with an asterisk (*).
Number: indicates order in which letter was received.
Sender: indicates name of user (and usually machine) letter came from.
Time: indicates date and time letter was sent.
Size: indicates number of lines/number of characters in letter.
Subject: indicates sender-designated subject of letter.
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.
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.
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".
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 Networkfor instructions regarding the use of the rusers command.
When you have determined the necessary user information, complete the following steps to send a letter.
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 |
When you press Return, the mailx program prompts you for a subject. Type a subject for your letter and press Return again.
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.
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.
When you have completed your letter, press Return to move the cursor to a new line. Then press Ctrl-D to send your letter.
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.
You can cancel a letter at any time before it is sent by pressing Ctrl-C twice.
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:
Use a text editor to edit your .mailrc file (in your home directory) and insert the following line:
set askcc |
The mailx program will now display the carbon copy prompt (Cc:) after the subject prompt. Enter the addresses of the users you want to receive carbon copies. Separate multiple addresses with spaces.
When you've finished typing the body of your letter, but before you press Ctrl-D, press Return to move to a new line and use the command form:
~c address(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 |
A Cc: line is also created by the ~h command, which displays the entire header of the letter. ~h prompts you with To:, Subject:, Cc:, and Bcc: (blind carbon copy) lines, one line at a time. Blank lines can be filled in; filled lines can be retyped. As with other tilde commands, always use the ~h command on a new line.
~c, ~h, and other tilde commands are described in "7.9 Tilde Commands".
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.
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:
On a new line give the command ~m 3 and then press Return.
mailx displays the message, Interpolating: 3 (continue)
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.
To see the complete letter, interpolation included, type the command ~p.
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 |
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."
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.
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.
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 |
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.
To use folders, you must first set up a folder directory. This is a two-step process:
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 |
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.)
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 |
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.
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:
& # |
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 Editorfor instructions.
In the mailx program, you can use the standard vi commands for inserting, deleting, and changing text.
To write a letter with vi:
Give the mailx command with an address at either the mailx prompt (&) or the command prompt ($).
Type in the subject at the Subject: line. Press Return.
Start vi by giving the command ~v on a new line. The vi screen will appear, representing an empty file in your /tmp directory.
Use vi commands to input and edit the body of your letter.
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.
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:
Your .mailrc file
The /etc/aliases file
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.
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 tried to send mail to amigos, he would receive 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 sent mail to amigos, your mail arrives as though you had typed everyone's names as recipients. It is not apparent to the recipients that you used a mail alias to send the mail.
.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 |
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 Editorfor 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 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 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? |
Note the following about 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 sent mail to softball@machinename, that's how the mail will read when it is received. The recipients will know what the mail alias is, but not necessarily who else is on it.
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:
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.
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 Editorfor 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.
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.
If you want to include a literal tilde character in a letter, type two tildes in succession. Only one tilde will be displayed.
Command |
Function |
---|---|
Escapes to a shell command |
|
Simulates pressing Ctrl-D to mark end of file |
|
Lists a summary of tilde commands |
|
Adds user name(s) to the blind carbon copies (Bcc:) list |
|
Adds user name(s) to the carbon copies (Cc:) list |
|
Reads the contents of the dead.letter file into current letter. |
|
|
Forwards the specified letter. Valid only when sending a message while reading mail. |
Prompts for header lines: Subject, To, Cc, and Bcc. |
|
|
Inserts text from the specified letter into the current letter. Valid only when sending a message while reading mail. |
Prints the message being entered to the screen. |
|
Simulates pressing Ctrl-C twice. If the body of the current message is not empty, the contents are saved to dead.letter. |
|
Reads in the text from the specified file. |
|
Changes the subject line to string. |
|
Adds the specified name(s) to the To list. |
|
Writes the current letter without the header into the specified file. |
|
Exits mailx. Similar to ~q except message is not saved in the dead.letter file. |
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.