Oct 2007 - Oct 2008

Table of Contents

1 Why do mail in Emacs?

Yes, you can read mail in Emacs. In fact, there are more than five mail clients to choose from, ranging from simple message readers to complex mail clients that integrate with many other modules. In this chapter, you'll learn how to set up and use Gnus, a feature-rich mail client that comes with Emacs 22.

Why Emacs when there are already plenty of desktop and web-based mail clients to choose from? I keep coming back to Emacs and Gnus because all the other e-mail clients I've tried seem limited in comparison. Here are some of the things I love about Emacs:

You can enjoy all of those features without giving up the convenience of your existing web-based or desktop e-mail client. If your mail server supports the Internet Mail Access Protocol (IMAP), you can synchronize your mail in Gnus with your mail in your regular e-mail client. For example, I use Emacs to read my Google Mail, and the messages I read offline are marked as read when I synchronize with the web copy. See "Project XXX: Set up Gnus with Synchronized IMAP" for more details.

Ready to start? First, make sure that your system can send mail.

2 Project XXX: Send mail in Emacs

First, let's find out if you already have a mail transfer agent (MTA) set up and ready to go. Type M-x mail to create a mail buffer. Fill in the details and e-mail yourself (at some Internet address) a note, typing C-c C-c (mail-send-and-exit) to send the message. Wait a few minutes, then check your mail using another mail client. If the message showed up, great! Skip this section. If the message didn't show up, read on.

If you're the administrator of a Linux or Unix-based system, you'll probably want to set up a proper mail transfer agent that can be used by other programs. That's outside the scope of this book, but you can probably find information in your distribution's administration guide. If you're in Microsoft Windows, you will probably want to set up a small mail transfer agent such as msmtp (http://msmtp.sourceforge.net), as the Emacs built-in modules for securely sending mail require extra programs that are not available for Microsoft Windows. Read the documentation for your mail transfer agent, configure it, check it by sending a message outside Emacs, then try sending a message from Emacs.

If you can't set up a mail transfer agent, you can use Emacs' built-in mailer. Emacs comes with a mail-sending library called smtpmail.el, which takes your messages and sends them to a mail server using the Simple Mail Transfer Protocol (SMTP). The mail server will then take care of passing on the messages through the Internet. Your Internet service provider will typically provide an SMTP server, and may block you from directly connecting to other SMTP servers. (This is to cut down on spam.) Some web-based mail services provide SMTP servers. Read their help files for more information.

Once you have the SMTP server name and other needed connection details such as port, username or password, you can set up smtpmail.el for sending mail. For example, if your provider lets you send mail through its SMTP server smtp.yourisp.com, add something like this to your ~/.emacs:

 (setq smtpmail-smtp-server "smtp.yourisp.com") 
 (setq send-mail-function 'smtpmail-send-it)
 (setq message-send-mail-function 'smtpmail-send-it)
 (require 'smtpmail)

For SMTP servers outside your Internet service provider, you may need to connect to a non-standard port and specify authentication details. This authentication is handled by the smtpmail-auth-credentials variable. SMTP servers that require authentication typically also require a secure connection through either Transport Layer Security (TLS) or Secure Sockets Layer (SSL). For security, you'll need either gnutls-cli (part of the GNU TLS package, which you can get from http://www.gnu.org/software/gnutls/) or starttls (get it from the archives of ftp://ftp.opaopa.org/pub/elisp/). Both programs work on Linux, but not Microsoft Windows. After you set either program up, change your smtpmail-related code in ~/.emacs to the following:

 (setq smtpmail-smtp-server "smtp.example.com")   (1)
 (setq smtpmail-smtp-service 587)                 (2)
 (setq smtpmail-auth-credentials                  (3)
       (list (smtpmail-smtp-server 
              smtpmail-smtp-service
              "yourusername"  
              "yourpassword")) 

Set your SMTP server(1) and port(2). SMTP servers usually use port 25, but some offer alternative ports so that you can use them even if port 25 is blocked by a firewall. (Make sure your own computer lets Emacs connect to this port.) Authentication information is stored in smtp-auth-credentials(3), which reuses the SMTP server and port and adds your username and password. If you change your server and port, you will need to set smtpmail-auth-credentials again.

You may also want to define a signature file that contains your contact information, a pithy saying, or anything that you would like to attach to the bottom of each message you send. Emacs includes the contents of ~/.signature (or the file indicated by mail-signature-file) in messages you create. Go ahead and set this to something like

 Firstname Lastname
 Long live Emacs!

When you compose a new message, your signature will be included.

3 Project XXX: Receive your mail in Gnus

There are several ways to get your mail into Gnus and several ways to store your mail once it's there. If you're on Linux or a UNIX-like system, then you can retrieve your mail outside Emacs and read it inside Emacs. If you're on Microsoft Windows, you will probably need to connect directly to your mail servers in order to retrieve or read your mail.

4 Project XXX: Work with mail in Gnus

Gnus is probably the most unusual mail client you will ever come across. That's because Gnus isn't really a mail client - it's a newsreader that geeks built to help them handle the large volumes of Usenet news. As a result, Gnus has many features that can help you with e-mail overload–and it also has some idiosyncrasies you'll need to watch out for and work around.

After you set up Emacs to send and receive mail (see Project XXX: Send mail in Emacs and Project XXX: Receive mail in Gnus), type M-x gnus to get to the main Gnus screen. First, Gnus checks your mail or connects to the server you specified in ~/.gnus. After that, Gnus displays a list of groups, which are similar to mail folders. Gnus also indicates the number of unread messages in each group. Press RET (gnus-group-select-group) on a group to see the messages in the group.

You will then see the Gnus summary view for that group. Here are some handy keyboard shortcuts:

DescriptionShortcutCommand
Displays the selected message or scrolls down the displayed messageRETgnus-summary-scroll-up
Show the next page of the selected message or go to the next unread messageSPCgnus-summary-next-page
Show the previous page of the selected message<backspace>gnus-summary-prev-page
Reply to the author of the message, quoting the original messageRgnus-summary-reply-with-original
Reply to the author of the message, but don't include the original messagergnus-summary-reply
Reply to all the people in the To:, From:, and Cc: fields, quoting the original messageFgnus-summary-followup-with-original
Reply to all the people in the To:, From:, and Cc: fields, but don't include the original messagefgnus-summary-followup
Compose a messagemgnus-summary-mail-other-window
Delete the current messageB DELgnus-summary-delete-article
Move the current message to a different groupB mgnus-summary-move-article
Exit the current group and go back to the group selection screenqgnus-summary-exit

When you compose a message or a reply, type C-c C-c (message-send-and-exit) from the message buffer in order to send it.

Gnus makes it easy to delete, reply to, or move more than one message at a time by using process marks. To select multiple messages in the summary buffer, press # (gnus-summary-mark-as-processable). You can also specify the number of messages to mark by using the C-u prefix. For example, to mark the next five messages, type C-u 5 # (gnus-summary-mark-as-processable). You can mark all the visible messages with M P a (gnus-uu-mark-all). To unmark a message, type M-# (gnus-summary-unmark-as-processable). To unmark all messages, type M P U (gnus-summary-unmark-all-processable). The shortcuts for deleting, replying to, and moving messages all work with process marks.

When you exit and re-enter a group, all the messages you've already read will be hidden from view. Don't panic. You can view old messages by typing / o (gnus-summary-insert-old-articles) and pressing RET to accept the default of all messages, or specifying the number of old messages you want to retrieve. To keep a message visible, press ! (gnus-summary-tick-article-forward) to flag it as important. To remove the flag, press M-u (gnus-summary-clear-mark-backward).

5 Project XXX: Read HTML mail

Thanks to spammers, phishers, and all sorts of nefarious malcontents on the Internet, it is generally not a good idea to automatically render HTML mail in Gnus. To disable HTML mail entirely, add the following line to your ~/.gnus:

 (setq mm-automatic-display (remove "text/html" mm-automatic-display))

If you must read the occasional HTML message, however, you can manually display it by adding the following to your ~/.gnus:

 (setq mm-text-html-renderer nil)
 (defun wicked/gnus-article-show-html ()
  "Show the current message as HTML mail."
  (interactive)
  (let ((mm-automatic-display (cons "text/html" mm-automatic-display)))
    (gnus-summary-show-article)))
 (define-key gnus-article-mode-map "WH" 'wicked/gnus-article-show-html)

You can then use W H (wicked/gnus-article-show-html) to show the current message in an external browser. To configure this to open a different web browser, use M-x customize to change the value of browse-url-browser-function. For more details, read "Project XXX: Set your default web browser".

6 Project XXX: Search mail

There are several ways to find messages in Emacs. From the summary buffer, you can use / o (gnus-summary-insert-old-articles) to display all or some old messages. You can then scan through the headers in the summary buffer by using C-s (isearch-forward), or you can limit the displayed messages with these commands:

DescriptionShortcutCommand
Messages from a given author/ agnus-summary-limit-to-author
Messages whose subjects matching a given regular expression/ /gnus-summary-limit-to-subject
Messages that match a given extra header/ xgnus-summary-limit-to-extra-headers
Messages at least N days old/ tgnus-summary-limit-to-age

Limits work on the messages that are currently displayed, so you can apply multiple limits. If you make a mistake, use / w (gnus-summary-pop-limit) to remove the previous limit. You can repeat / w (gnus-summary-pop-limit) until satisfied. To remove all the limits, type C-u / w (gnus-summary-popl-limit).

If you specify a prefix, the limit's meaning is reversed. For example, C-u / a (gnus-summary-limit-to-author) will remove the messages from the matching author or authors.

You can use Gnus to search the currently-displayed messages by using M-s (gnus-summary-search-article-forward) and M-r gnus-summary-search-article-backward.

If you want to search a lot of mail, you'll find NNIR handy. NNIR is a front-end to mail search engines which can index your mail and return search results quickly. If you want to use NNIR with a local or remote IMAP server, you will need to use nnir.el and imap.el. If you download your mail using fetchmail or connect to a POP3 server and use an nnml backend, you can use NNIR with a search engine such as swish-e to search your ~/Mail directory efficiently.

7 Project XXX: Organize mail into Groups

People handle large volumes of mail in different ways. Keeping everything in one mailbox can quickly become unmanageable because messages you need to read get lost among messages you don't need to read.

You can move mail manually by selecting them in the summary buffer and typing B m (gnus-summary-move-article). Then type the name of the group to which you would like to move the message. The group will be created if it doesn't exist.

To move multiple messages, mark them with # (gnus-summary-mark-as-processable) and then type B m (gnus-summary-move-article). To unmark a message, type M-# (gnus-summary-unmark-as-processable). To unmark all messages, type M P U (gnus-summary-unmark-all-processable).

8 Project XXX: Filter spam

Ah, spam, the bane of our Internet lives. There is no completely reliable way to automatically filter spam. Spam messages that slip through the filters and perfectly legitimate messages that get labelled spam are all part of the occupational hazards of using the Internet.

The fastest way to filter spam is to use an external spam-filtering program such as Spamassassin or Bogofilter, so your spam can be filtered in the background and you don't have to spend time in Emacs filtering it yourself. In an ideal world, this would be done on the mail server so that you don't even need to download unwanted messages. If your inbox isn't full of ads for medicine or stocks, your mail server is probably doing a decent job of filtering the mail for you.

As spam filtering isn't an exact science, you'll want to find out how you can check your spam folder for misclassified mail. If you download your mail through POP, find out if there's a webmail interface that will allow you to check if any real mail has slipped into the junk mail pile. If you're on IMAP, your mail server might automatically file spam messages in a different group. Here's how to add the spam group to your list of groups:

  1. Type M-x gnus to bring up the group buffer.
  2. Type ^ (gnus-group-enter-server-mode).
  3. Choose the nnimap: entry for your mail server and press RET (gnus-server-read-server).
  4. Find the spam or junk mail group if it exists.
  5. Type u (gnus-browse-unsubscribe-current-group) to toggle the subscription. Subscribed groups will appear in your M-x gnus screen if they contain at least one unread message.

Another alternative is to have all the mail (spam and non-spam) delivered to your inbox, and then let Gnus be in charge of filing it into your spam and non-spam groups. If other people manage your mail server, ask them if you can have your mail processed by the spam filter but still delivered to your inbox. If you're administering your own mail server, set up a spam filtering system such as SpamAssassin or BogoFilter, then read the documentation of your spam filtering system to find out how to process the mail.

Spam filtering systems typically add a header such as "X-Spam-Status" or "X-Bogosity" to messages in order to indicate which messages are spam or even how spammy they are. To check if your mail server tags your messages as spam, open one of your messages in Gnus and type C-u g (gnus-summary-show-article) to view the complete headers and message. If you find a spam-related header such as X-Spam-Status, you can use it to split your mail. Add the following to your ~/.gnus:

 (setq spam-use-regex-headers t) ;; (1)
 (setq spam-regex-headers-spam "^X-Spam-Status: Yes")   ;; (2)
 (require 'spam) ;; (3)
 (spam-initialize) ;; (4)

This configures spam.el to detect spam based on message headers(1). Set spam-regex-headers-spam to a regular expression matching the header your mail server uses to indicate spam.(2) This configuration should be done before the spam.el library is loaded(3) and initialized(4), because spam.el uses the spam-use-* variables to determine which parts of the spam library to load.

In order to take advantage of this, you'll also need to add a rule that splits spam messages into a different group. If you haven't set up mail splitting yet, read qthe instructions on setting up fancy mail splitting in "Project XXX: Organize mail into groups". Add (: spam-split) to either nnmail-split-fancy or nnimap-split-fancy, depending on your configuration. For example, your ~/.gnus may look like this:

 (setq nnmail-split-fancy
       '(
         ;; ... other split rules go here ...
         (: spam-split)
       ;; ... other split rules go here ...
         "mail.misc")) ; default mailbox

9 Project XXX: Score mail

One of the things you'll probably love about Gnus is the way it can remember what you find interesting and what you find boring. When you're dealing with a high-volume mailing list or newsgroup, or even with a large inbox, it helps to have interesting messages bubble up to the top and boring messages sink to the bottom. Gnus can do that with a combination of manual and adaptive scoring.

Gnus can calculate message scores based on headers, keywords, arbitrary Lisp expressions, or combinations of all three. These rules can be permanent or temporary. Gnus can then perform different actions based on those message scores, such as sorting the message list by score, marking messages with scores higher or lower than a particular threshold, or even automatically deleting messages with a low score.

You can define these scoring rules manually or have Gnus generate them from your reading behavior. For example, Gnus could raise the score of message threads you've read or replied to, while lowering the score of message threads you marked as read without actually reading.

For a full discussion of scoring, read the "Scoring" section of the Gnus info manual. In this project, you'll learn how to create simple manual and adaptive scoring rules.

10 Project XXX: Set up posting styles

You might want to use your work address and signature when responding to mail sent to your work address, your personal address and signature when responding to regular mail, and a nospam address otherwise. This is handled by gnus-posting-styles, which you can set in ~/.gnus. gnus-posting-style is a list of rules, and all matching rules will be applied in order.

The easiest way to use gnus-posting-styles is to set up groups (see Project XXX: Organize Mail into Groups) and then set your posting styles based on the groups. For example, if all your work mail is in INBOX.work and all your personal mail is in INBOX.personal, you can use the following code in your ~/.gnus:

 (setq gnus-posting-styles
       '((".*"    ; Matches all groups of messages
          (address "[email protected]")
          (signature "J. Random Hacker\nhttp://example.com\nRemove nospam from e-mail address before replying."))
         ("INBOX.work"
          (address "[email protected]")
          (signature "J. Random Hacker\nGrand Poobah, Example Inc.\nhttp://example.com"))
         ("INBOX.personal"
          (address "[email protected]")
          (signature "J. Random Hacker\nhttp://example.com"))))

Even if you don't split your mail into different groups, you can still change posting styles depending on the message. For example, this uses the To: header:

 (add-to-list 'gnus-extra-headers 'To)
 (setq gnus-posting-styles
       '((".*"    ; Matches all groups of messages
          (address "[email protected]")
          (signature "J. Random Hacker\nhttp://example.com\nRemove nospam from e-mail address before replying."))
         ((header "to" "[email protected]")
          (address "[email protected]")
          (signature "J. Random Hacker\nGrand Poobah, Example Inc.\nhttp://example.com"))
         ((header "to" "[email protected]")
          (address "[email protected]")
          (signature "J. Random Hacker\nhttp://example.com"))))

To learn more about what you can change, read the Posting Styles page of the Gnus info manual.

Author: Sacha Chua <[email protected]>

Date: 2008-08-04 12:32:09 EDT

HTML generated by org-mode 6.06b in emacs 23