Наши партнеры








Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31. Email

The primary means of sending plain-text messages (or binaries in attachment files) between users across computer networks and systems on the Internet is called electronic mail, or email (and more often than not these days, just "mail").

The number of email applications (called mail user agents, or MUAs) available for Linux is large, and you could spend endless hours exploring the details of all of them. Instead of guiding you toward this route, this chapter attempts to do three things: give a brief intro to using the default mail agent; give an overview of other well-supported mail agents, with pointers on where to go for more info; and show how you can use other tools on the system to manipulate your email.

The mail tool is the default mail agent on Debian and most other Linux systems. It comes without many bells and whistles that are standard with most MUAs, and any user who sends and receives email more than occasionally will certainly want to learn a more advanced system (see section Picking the Right Mail Application).

However, mail is available on almost all Unix-based systems, and it works in a pinch--by learning to use it you can always send and receive email on any Linux- or Unix-based system you encounter.

NOTE: On some Unix-based systems, the name of the tool is mailx instead of mail.

31.1 Sending Mail  Sending email.
31.2 Receiving Mail  Receiving email.
31.3 Managing Mail  Managing your email.
31.4 Mail Attachments  Dealing with mail attachments.
31.5 Making an Email Signature  Signatures in email.
31.6 Picking the Right Mail Application  Different mail applications to try.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.1 Sending Mail

To send an email message with mail, give the email addresses to which you are sending as arguments, and then type the message proper in the lines that follow; type C-d on a line by itself to signify the end of the message body, and to send the message.

  • To send an email message to lisa@example.com, type:

     
    $ mail lisa@example.com RET
    Subject: Hello RET
    Hi there, long time no talk! I'm just learning how to use RET 
    Linux and thought I'd show you how easy it is to send email! RET
    C-d
    Cc: RET
    

The text you type on the `Subject:' line is displayed as the subject of your email message, and the lines of text you type after that is the body text of the message. Type C-d on a line alone to end the message. Then, mail prompts for `Cc:' addresses; a "carbon copy" of the email message is sent to any addresses you give here, if any (just type RET for none, and separate multiple addresses with commas).

When you type, mail just reads the standard input like any other command-line tool, so there's little direct editing capability in this basic email service--use C-u to erase the current line, and C-c C-c (that is, C-c pressed twice) to cancel your input and abort the message altogether.

That's it! No bells, no whistles--but no time-wasting excess, either.

31.1.1 Mailing a User on the Same System  Sending mail to a user on your system.
31.1.2 Mailing a File or the Output of a Command  Emailing a file.
31.1.3 Mailing the Contents of a URL  Emailing the contents of a URL.
31.1.4 Special Mail Composition Keystrokes  Special mail composition keystrokes.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.1.1 Mailing a User on the Same System

To send an email message to another user on the same system, give their username on the system instead of an email address (technically, you are giving the email address, since email addresses take the form of username@hostname; when hostname is omitted, the localhost is assumed).

  • To send an email message to user mrs on your local system, type:

     
    $ mail mrs RET
    Subject: are you going to the party tonight? RET
    C-d
    Cc: RET
    Null message body; hope that's ok
    $
    

This command sends an email message to the user mrs on the local system. The email message itself is empty, but the subject is a short note asking whether user mrs will be attending a party.

NOTE: Besides being good for sending mail to users that you might share your system with, mail is useful for sending yourself mail, as a way to give yourself a reminder at your terminal (see section Sending Yourself Email Reminders).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.1.2 Mailing a File or the Output of a Command

The mail tool is also useful for mailing the contents of a text file or the text output of a command. To do this, give the email addresses you want to send to as arguments to mail, and use the standard input redirection operators to redirect the text to use as the message body (see section Redirecting Input and Output).

  • To mail the contents of the text file `trades' to the email address terrapin@example.com, type:

     
    $ mail terrapin@example.com < trades RET
    


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.1.3 Mailing the Contents of a URL

A variation on the previous recipe is to use mail and shell redirection to send the output of some command to some address via email. You can, for example, send the contents of a URL as an annotated text file by redirecting the output of the lynx Web browser (see section Reading Text from the Web).

  • To mail the text of the URL http://etext.org/ as annotated text to the email address droneon@example.com, type:

     
    $ mail droneon@example.com < lynx -dump -number_links
    http://etext.org/ RET 
    


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.1.4 Special Mail Composition Keystrokes

The following table lists the special keystrokes that work when composing a mail message, and describes their functions.

KEYSTROKE DESCRIPTION
C-c C-c Abort the current message and exit mail.
. RET or C-d On a blank line, either of these commands sends the message and then exits mail.
C-u Erase the current line and move the cursor to the beginning of the line.

There are also a few special commands that you may use while composing the body of the message. They're known as "tilde escapes" because you specify them by typing a tilde character (`~').

The following table lists some of these commands and describes their functions.

COMMAND DESCRIPTION
~!command Run command in a shell.
~baddress Send a blind carbon copy to the usernames or email addresses given.
~d Copy the file `dead.letter' from your home directory into the message.
~e Edit the message in the default text editor program. (When you exit the text editor, you are returned to mail.)
~fnumber Insert copies of the specified received messages into the message body. Messages are specified by number or a range (for example, `2-4' inserts messages two through four inclusive); if no number is given, the current received message is inserted.
~F Same as `~f', but reads in the messages with full headers.
~rfile Insert a copy of the file file into the message.
~wfile Write a copy of the body text into the file file.

These commands should each be typed on a line by itself.

  • To insert a copy of the current mail message into the body of the message you are writing, and then open the message in the default text editor, type:

     
    ~f RET
    ~e RET
    


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.2 Receiving Mail

On Linux-based systems, the INBOX is a text file on the system where your incoming mail is written to. Its location is always given by $MAIL, a special shell variable (see section Changing the Shell Prompt).

  • To output the location of your INBOX, type:

     
    $ echo $MAIL RET
    

Usually, the INBOX location is in the `/var/spool/mail' directory, and has the same name as your username--so if your username is mrs, your INBOX is likely `/var/spool/mail/mrs'.

You shouldn't directly edit this file, because doing so can inadvertently cause you to lose incoming mail.

To see if you have any mail waiting in your INBOX, type mail. If you don't have any mail, mail will indicate this and exit; if you do have mail waiting, mail outputs a list of message headers, one line per message, each containing the status of the message (`N' for new messages, blank for previously read messages), the message number, the name of the sender, the date and time the message was received, and the number of lines and characters in the message.

  • To see if you have mail, type:
 
$ mail RET
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/m": 3 messages 3 new
>N  1 mrs           Mon Sep 6 17:29  13/345 "Re: A modest proposal"
 N  2 Ray           Tue Sep 7 04:20  15/694 "Latest news"
 N  3 lisa@example  Tue Sep 7 09:35  19/869 "Re: Hello"
& 

In this example, the user has three messages waiting--one from mrs, one from Ray, and one from lisa@example.com.

The mail prompt is an ampersand (`&') character; from there, you can read, delete, reply to, and save messages.

When you type RET at the `&' prompt, mail outputs the next unread message to the screen. You can also type a number to output that message.

  • To read the next unread message in mail, type:
     
    & RET
    

  • To read message number three in mail, type:

     
    & 3 RET
    

There are two ways to exit mail: type q to exit mail and apply the deletion commands you have given, if any, to your INBOX; type x to exit mail and revert the state of your INBOX to how it was before you ran mail.

  • To exit mail and revert your INBOX to its state before you started mail, type:

     
    & x RET
    

31.2.1 Deleting Mail  Deleting email.
31.2.2 Options Available while Reading Mail  Command options while reading mail.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.2.1 Deleting Mail

To delete a message in mail, type d at the `&' prompt after reading the message in question. You can also specify a message or a range of messages to delete as an option to d.

  • To delete the message you just read, type:
     
    & d RET
    

  • To delete message 3, type:
     
    & d3 RET
    

  • To delete messages 10 through 14, type:

     
    & d10-14 RET
    


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.2.2 Options Available while Reading Mail

The following table summarizes the most common mail commands for reading mail; these commands work at the `&' prompt.

COMMAND DESCRIPTION
? Output a help menu containing a list of mail options and their meanings.
d Delete a message. Give the number or range of the message(s) to delete as an argument.
h Output a list of headers of mail messages. You can specify a range or the number of the message to start with.
q Exit mail and apply the changes you have made in this mail session to your INBOX.
r Reply to the message you last read; you can also give a message number as an argument to reply to that message number.
u Undelete a message you have deleted in the current mail session. Give the number or range of the message(s) to be undeleted as an argument.
x Exit mail and revert the INBOX to its state before this mail session.
s file Save the message you last read to the file in your home directory specified by file (if the file does not exist, mail will ask you whether or not it should create it).

NOTE: By default, only you (and, as always, the superuser) have access to read your INBOX. While there are tools available (such as mail, and the other MUAs) to read this file in special ways, you can also view this file like any other text file (see section Viewing Text).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.3 Managing Mail

A mail folder is simply a text file whose contents consist of saved mail messages; any tool that works on text can be used on a mail folder.

The following subsections describe some of the common ways to manage and otherwise modify your saved mail.

31.3.1 Viewing a Mail Folder  Viewing a folder of email.
31.3.2 Setting Notification for New Mail  Announcing when the mailman has arrived.
31.3.3 Counting How Many Messages You Have  Counting the mail you have.
31.3.4 Seeing Who Your Mail Is From  Finding out who your mail is from.
31.3.5 Verifying an Email Address  Finding out if an email address is correct.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.3.1 Viewing a Mail Folder

@sf{Debian}: `elm-me+'
@sf{WWW}: http://www.instinct.org/elm/

You can view your mail folders in less or edit them in a text editor, although the folder will appear as one long scroll containing all of the messages the folder contains.

You can also view them in elm (see section Picking the Right Mail Application) or open them with mail, and they will appear in the normal way as your INBOX would appear with these tools.

To view a mail folder with elm, give the name of the folder as an argument to the `-f' option.

  • To view the mail folder `~/email/mrs' in elm, type:

     
    $ elm -f ~/email/mrs RET
    

If you save your mail messages in a lot of separate folders, you can view a sorted list of all messages from all files by using cat in conjunction with elm. Concatenate all the folders into one with cat and then view that file in elm as you would view any folder.

  • To view the contents of all of the email folders in your `~/email' directory, type:

     
    $ cat ~/email/* > allmessages RET
    $ elm -f allmessages RET
    

These commands write a new file, `allmessages', in the current directory, containing the contents of all email folders in `~/email'; then, that file is viewed in elm.

NOTE: To view a list showing who all the messages in a folder are from, use frm; see Seeing Who Your Mail Is From.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.3.2 Setting Notification for New Mail

@sf{Debian}: `biff'
@sf{WWW}: ftp://ftp.uk.linux.org/pub/linux/Networking/
@sf{WWW}: http://www.splode.com/~friedman/software/packages/index.html

The biff tool notifies you when new mail arrives, by printing the header and first few lines of a mail message.

To turn biff on, use `y' as an option. To turn biff off, so that you stop being notified when new mail arrives, use `n' as an option. biff options don't take a hyphen.

  • To turn biff on, type:

     
    $ biff y RET
    

Some people put the above line in their `.bashrc' file so that biff is always set on in all of their shells (see section Changing the Shell Prompt).

Typing biff alone with no options will tell you whether biff is set to `y' or `n'.

  • To see what biff is set to, type:

     
    $ biff RET
    

A companion tool, xbiff, works only in the X Window System (you can use the regular biff in X, too). When you start it, xbiff draws a window containing a mailbox that looks like this:(38)

email-xbiff-01

When you have mail, xbiff rings the system bell, the window icon reverses color, and the mailbox flag goes up:

email-xbiff-02

NOTE: The original version of biff was named after a dog. In the early 1980s at a UC Berkeley computer lab, a girl would bring her dog, Biff, with her when she went to use the computers. Biff was known for barking at the mailman when he came in to deliver the day's mail. He was also very popular with all of the BSD UNIX hackers at Berkeley, and when one of them wrote a mail notification tool, he thought of Biff--hence the name. (Biff, the dog, died in August 1993.)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.3.3 Counting How Many Messages You Have

@sf{Debian}: `elm-me+'
@sf{WWW}: ftp://ftp.uu.net/networking/mail/elm

Use messages to count the number of mail messages in a folder or file. Give the name of a mail folder as an argument; with no arguments, it counts the mail you have waiting in your INBOX.

  • To see how many email messages you have waiting, type:
     
    $ messages RET
    

  • To count the number of email messages in the mail folder `~/email/saved', type:

     
    $ messages ~/email/saved RET
    


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.3.4 Seeing Who Your Mail Is From

@sf{Debian}: `elm-me+'
@sf{WWW}: ftp://ftp.uu.net/networking/mail/elm

Use frm to output a list of sender names and subjects for your mail. Give the name of a mail folder as an option; with no options, frm reads your INBOX.

  • To output a list showing sender names and subjects of your incoming mail, type:
     
    $ frm RET
    

  • To output a list showing sender names and subjects of the mail in the file `~/email/saved', type:

     
    $ frm ~/email/saved RET
    

NOTE: An alternate tool, from, works in similar fashion, but it does not output subject lines; instead, it outputs the names of senders and the time that messages were received.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.3.5 Verifying an Email Address

@sf{Debian}: `vrfy'
@sf{WWW}: ftp://ftp.nikhef.nl/pub/network/

Use vrfy to determine whether or not a given email address works. This is useful when you are unsure whether or not you have the right email address for someone. If the address works, vrfy outputs a message indicating that the recipient exists; if the address is not valid, vrfy outputs a message saying that the user is unknown.

  • To verify that the email address user@example.edu is valid, type:

     
    $ vrfy user@example.edu RET
    

Use the `-f' option to specify a text file containing email addresses; vrfy attempts to verify all email addresses contained in the file.

  • To verify all of the email addresses contained in the file `net-legends-faq', type:

     
    $ vrfy -f net-legends-faq RET
    

NOTE: vrfy relies on the remote system to get this information; in these days of the heavily corporatized Internet, an increasing number of sites no longer supply this kind of information to the general public. However, it's still useful enough to be worth mentioning.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.4 Mail Attachments

@sf{Debian}: `metamail'
@sf{WWW}: http://bmrc.berkeley.edu/~trey/emacs/metamail.html

MIME ("Multipurpose Internet Mail Extensions") is an Internet standard for encoding and attaching files to mail messages. It's used when sending image, audio, or other non-plain-text data via email.

Normally, you read and send MIME mail with your MUA. The following recipes, which show ways to send and receive MIME mail on the command line, are useful for when you just use the mail tool to read and send occasional mail with an attachment, but the built-in methods for manipulating MIME mail in any reasonable MUA will invariably be easier and more convenient than the techniques described here (see section Picking the Right Mail Application).

31.4.1 Reading a Mail Attachment  Reading mail attachments.
31.4.2 Sending a Mail Attachment  Sending mail attachments.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.4.1 Reading a Mail Attachment

To read a mail attachment, write the message to a file and then run metamail with the file name as an argument. metamail lists each attachment and prompts you about whether it should display the attachment, write it to a file, or skip it.

    To read a mail attachment, type:
     
    $ mail RET
    Mail version 8.1 6/6/93.  Type ? for help.
    "/var/spool/mail/m": 1 messages 1 new
    >N  1 Photo Dept.   Mon Feb 12 14:37  231/10980 "New Images"
    & w1 image.mail RET
    "image.mail" [New file]
    & x RET
    $ metamail image.mail RET
    

In this example, the mail tool was used to open the INBOX and write the message to a file called `image.mail'; then, metamail was run with the file name as an argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.4.2 Sending a Mail Attachment

To send a file as an email attachment, use metasend. It prompts for the values to use in the `To:', `Subject:', and `CC:' header fields, plus the following values for each MIME attachment: its `Content-type:' field, which describes the kind of data the attachment contains; the file name; and the type of encoding to use, if any (usually one is recommended).

  • To mail the JPEG file `dream.jpeg' in the current directory to the address dali@example.org, type:
 
$ metasend RET
To: dali@example.org RET
Subject: The image you requested RET
CC: RET
Content-type: image/jpeg RET
Name of file containing image/gif data: dream.jpeg RET
Do you want to encode this data for sending through the mail?
  1 -- No, it is already in 7 bit ASCII
  2 -- Yes, encode in base64 (most efficient)
  3 -- Yes, encode in quoted-printable (less efficient, more readable)
  4 -- Yes, encode it using uuencode (not standard, being phased out)
2 RET
Do you want to include another file too (y/n) [n] ? n RET
Delivering mail, please wait...  Mail delivery apparently succeeded.
$

The following table lists values to use in the MIME `Content-type:' field for various kinds of files.

VALUE FILE TYPE
application/gzip File compressed with gzip.
application/zip File compressed with zip.
application/postscript PostScript file.
image/jpeg JPEG image file.
image/png PNG image file.
audio/basic Audio file.
audio/mpeg3 MP3 audio file.
audio/wav WAV audio file.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.5 Making an Email Signature

@sf{Debian}: `sigrot'
@sf{WWW}: ftp://metalab.unc.edu/pub/Linux/system/mail/misc/

A signature file (often called a "dot sig," and written as `.sig') is a text file containing text that you want to appear at the end of email messages and other online postings.

Sometimes, people put their name, email address, and a small quote, or a piece of ASCII art (such as text written in a figlet font---see section Horizonal Text Fonts); once the World Wide Web became popular, many people started including the URL of their home page in their `.sig'.

The use of signatures goes in and out of vogue with the years; you can decide whether or not you want to use one, but whatever you do, be sure to keep your `.sig' at most four lines in length--to use any more is considered very bad form. A first line consisting only of `-- ' is sometimes used; many applications recognize this text as the beginning of a `.sig' when processing messages.

You create your signature file in a text editor, just like any other text file. Name the file `.signature' or `.sig', and keep it in your home directory.

If you want to use more than one signature, use sigrot to "rotate" your various signatures--every time you run sigrot, it selects one of the signature files you keep in your `.sigrot' directory and writes it to `.signature'. To change your `.signature' every time you log in, you would run sigrot in your `.bash_login' file (see section The Shell).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

31.6 Picking the Right Mail Application

The following table lists some of the more popular MUAs that are available for Linux, describing their special features, and listing their Debian package name and URL (when available).

APPLICATION DESCRIPTION
balsa A graphical email client that works in X with GNOME installed; its interface is inspired somewhat by the proprietary Eudora. {@sf{Debian}}: `balsa' {@sf{WWW}}: http://www.balsa.net/
elm A menu-driven MUA, elm was popular in the early 1990s among experienced users--it has some interesting features, including ways to send mails in batch mode to many addresses at once, and a tool to send telephone messages as email messages. Interest in elm has waned somewhat over the years, and most novices are advised to try mutt instead. {@sf{Debian}}: `elm me+' {@sf{WWW}}: http://www.instinct.org/elm/
gnus The gnus newsreader for Emacs(39) can also be used to read and send mail. It has many features and should appeal to Emacs lovers--but a warning: it can be daunting to learn! {@sf{Debian}}: `gnus' {@sf{WWW}}: http://www.gnus.org/
mew mew is an Emacs mail and news facility developed in Japan. It shows promise as a fairly new MUA and has many features for handling mail in complex ways. {@sf{Debian}}: `mew' {@sf{WWW}}: http://www.mew.org/
mh-e MH-E is an Emacs front end to nmh, below. It's very powerful, yet it remains easy to use. {@sf{Debian}}: `emacsen-common' {@sf{WWW}}: http://www.emacs.org/
mozilla Netscape Inc.'s open source Web browser, mozilla, has its familiar and self-explanatory email interface that works in the X Window System. {@sf{Debian}}: `mozilla' {@sf{WWW}}: http://www.mozilla.org/
mutt The MUA currently in favor among many vi users is mutt; it is one of the most popular MUAs for Linux. {@sf{Debian}}: `mutt' {@sf{WWW}}: http://www.mutt.org/
nmh The Rand "Mail Handling" system, mh, is not one application but a collection of small tools for manipulating mail folders. It should appeal to those who excel at building complex commands from combinations of simple tools and operators. nmh is the new "Mail Handling" system, containing rewrites and improved versions of the mh tools. Most Linux systems will install this over the old mh. {@sf{Debian}}: `nmh' {@sf{WWW}}: http://www.mhost.com/nmh/
vm VM ("View Mail") is a facility for reading and sending mail in Emacs. Older than gnus and mew, it is very configurable. {@sf{Debian}}: `vm' {@sf{WWW}}: http://www.wonderworks.com/vm/
wl Wanderlust is a MUA for Emacs designed to facilitate reading your mail on multiple computers. {@sf{Debian}}: `wl' {@sf{WWW}}: http://www.gohome.org/wl/


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]