Found at: http://publish.ez.no/article/articleprint/38
|
Using Emacs for reading mail - Part 1
|
Emacs is, as you might know, a powerful texteditor with an advanced scripting language which allows for extending it to your needs. This has been used (and misused) on several occasions to enhance Emacs beyond the use of a simple texteditor. One of these extensions are the Gnus message reader. Gnus itself is also huge and powerful and that's were the trouble begins. In this article I'll give narrowed example on how to configure Emacs (or Gnus) to be used as a mail reader. For those of you who feel brave I suggest you go directly to the online manual or download it in other formats. For the rest of you, who like to spend the rest of this week doing other things than reading the manual, I suggest reading on.
Setup
The first thing you need to do is to download and install Gnus since the version included in your standard (X)Emacs distribution is probably old. Installation should be simple, for instance if you downloaded Gnus 5.8.7 you do:
tar xvfz gnus.tar.gz
cd gnus-5.8.7
./configure
make |
if you're using XEmacs you need to change the make into
This should byte-compile Gnus for your Emacs. XEmacs users should also download the glyph package to get some icons for their mail reader (Emacs will only use the startup image). Unpack the glyph archive and move it to the gnus main directory and rename it to etc.
For instance if you downloaded etc 0.27 and Gnus 5.8.7 you would write:
tar xvfz etc.tar.gz
mv etc-0.27 gnus-5.8.7/etc |
The next thing you need to decide is where you want gnus to be installed, my recommendation is /usr/share/emacs/site-lisp/gnus for Emacs users and /usr/share/xemacs/site-lisp/gnus for XEmacs users, if you use both you should install it to /usr/share/gnus.
The reason for the subdirectory gnus is that it makes it easier to upgrade to newer versions since gnus consists of several files. However if you feel strongly about it, you can simply install all files in /usr/share/emacs/site-lisp or /usr/share/xemacs/site-lisp, but not /usr/share.
For instance to install Gnus 5.8.7 for Emacs do:
mv gnus-5.8.7 /usr/share/emacs/site-lisp/gnus |
Now you need to tell Emacs where it can locate Gnus, this is done by editing your local Emacs config, located in your home directory. Edit ~/.emacs with your favourite editor and add these lines:
(setq load-path (cons "/usr/share/emacs/site-lisp/gnus/lisp" load-path)) |
If no problems occurred Gnus should be properly installed and ready to be used. So I suggest you fire up Emacs and test it. Once Emacs is up and running you should do:
|
|
Gnus logo
|
For those of you who are not used to Emacs this means: Press Meta(Sometimes Alt or left Windows key) and hold( For those of you without a Meta key press ESC and release), press x, enter gnus and press Return or Enter.
If you see some graphics or text resembling a Gnu you should be ready for the next step.
The last thing you should know of is the file .gnus in your home directory. It is used for configuring Gnus since it is read each time it is started, therefore I suggest all the Emacs-lisp code I write be put into this file, unless explicitly stated otherwise.
Adding newsgroups
Since Gnus is mainly used for reading News I'll start off by explaining how to read newsgroups. If you're not interested in newsgroups I still think you should read this, because it gives some basic information on using Gnus which I won't repeat.
The first you can do is to set your default news server. It is done by setting the gnus-select-method variable, we do this with:
(setq gnus-select-method '(nntp "news.online.no")) |
This sets the default selection method to use nntp (which is News) and use the server news.online.no for reading the news. Change the server to fit your needs. After you have done this simply start Gnus again by pressing R.
Gnus will then start a scan in the newsgroup and after a while (may take several minutes) you will have a couple of default groups selected for you, one of them being gnu.emacs.gnus which is the primary place for asking questions regarding Gnus.
Now you probably want to subscribe yourselves to some other newsgroups as well, to do this you first have to enter the server buffer as it is called. You do this by pressing ^, you will then be presented by a list looking something like:
{nnfolder:archive} (closed)
{nndoc:gnus-help} (opened)
{nntp:news.online.no} (opened) |
As you might see our News server news.online.no is here, any additional servers you add will also be present here. To start subscribing groups you move the cursor to the wanted server and press enter.
It should then start to download a group list from the server, again this might take some time. After it is finished you will be presented by a huge list of groups, each with a K in front of them. This means that it is not subscribed.
To subscribe a group you place the cursor over it and press u, the letter infront of them should then disappear, this means you have succesfully subscribed it. To unsubscribe you press u again.
A tip for finding groups is to use Emacs's search function, this is invoked with C-s (Hold Control and press s.
After you are done selecting groups you can quit the group list by pressing q and then quit the server buffer by pressing q again. This puts you back in the group list again with your newly subscribed groups, all your new groups will have a * in front of them which means that they have never been used before,
|
|
Gnus in action
|
in other words it does not know the number of messages in it.
To refresh your new and old groups you press g, Gnus will then scan all your subscribed groups for new messages. You might not always want to scan all groups but for the moment you have to, I'll explain a method for scanning only a selected set of groups later. After the groups have been scanned a number will be put in front of them, this is the number of unread messages.
To start reading messages in a group simply place the cursor over the group and press enter, a list will then be fetched and displayed. Pressing enter on a message will display it, pressing space either shows the next page in the message or jumps to the next message, pressing backspace shows the previous page in the message. To quit reading a group press q, pressing q is actually a common key for quitting operations in Gnus.
For a whole list of commands I suggest you look trough the menu items, Misc, Groups, Group and Topics.
Configuring POP3
A lot of you probably use pop3 for reading mail, I'll explain how you can do this with Emacs.
The first thing you need to do is to make Gnus read the local mail box since it is used for storing the mail from the remote pop3 server.
You do this by setting the gnus-secondary-select-methods variable, we set it to:
(setq gnus-secondary-select-methods
'((nnml "private"))) |
You can test this by sending a mail to yourself (replace myname with your local user name).
echo "testmail" | mail -s "testmail" myname |
and rereading the .gnus file by pressing r. Then press g to refresh the groups.
You should then get a group called nnml+private:mail.misc with one mail in it, this means you're ready for step two.
You now need to supply where gnus should fetch the mail, this is done by setting the mail-sources variable, for instance:
(setq mail-sources
'((file)
(pop :server "pop3.mail.server"
:user "pop3.user"
:password "secret"))) |
This will add the local file and a pop3 server as incoming mail. pop3.mail.server is the server where you fetch the mail, pop3.user is the user on the pop3 server and secret is the password for your user on the pop3 server. You can safely omit the :password if you don't want store the password in a text file, in this case you will be asked for a password on each Gnus session.
You can also supply more than one pop3 server if you wish, for instance:
(setq mail-sources
'((file)
(pop :server "pop3.mail.server"
:user "pop3.user"
:password "secret")
(pop :server "other.mail.server"
:user "other.user"))) |
The next thing you probably want to do is to filter your mails into various subgroups, luckily for you Gnus can do that too.
Filtering is controlled by the nnmial-split-methods variable, for instance to move all mail from a specific person to a junk group you would do:
(setq nnmail-split-methods
'(("junk" "^From:.*Bill Gates")
("other" ""))) |
The filter variable consists of a list of filter rules, each rule consists of a mail group ("junk" in the example) and a Regular Expression("^From:.*Bill Gates" in the example). For more information on regular expressions I suggest you read this article.
In this example we move all mail which come from some guy called Bill Gates to the group junk and all other mail to the group other.
Configuring IMAP
Not all of you use POP3 for mail reading mail, some of you, like me, use IMAP. I'll explain how this is done with Emacs.
There's one thing you need to be aware of, and that is that the IMAP handler in Gnus does not use a POP3 server for incoming mail, instead it relies on you having the mail box on the server running IMAP. This means that you have to use external programs for fetching the mail before Gnus handles it, one such program is fetchmail.
To read mail from an IMAP server you need to know the name of the server, your username and your password. Both the username and password will be asked by Gnus each time your restart after adding an IMAP server, however we don't want to answer them each time. Gnus will read the content of the file .authinfo in your home directory to find servers, usernames and their passwords.
For instance let's create this file with some content, we'll pretend we got our mail on a server called imap.mail.server, our username is imap.user and password is secret. The file will then look like this:
machine "imap.mail.server" login "imap.user" password "secret" |
Now change the server, user name and password to something that fits you.
I also suggest you change the permissions on the file to avoid any other users on the system to read it, you can do that by issuing the command
If you're very worried about security you can easily omit the password section in the file, in that case you will need to enter the password on each Gnus session.
The next thing you should do is tell the IMAP program to filter out certain files on the server, usually you have your mail in a file called mbox, sometimes you keep them in a directory called mail. From what I've understand there's no defined standard for this so you just use what fits you. For instance we might configure IMAP to only show the mbox and any files in the mail directory, this leads to:
(setq nnimap-list-pattern
'("mbox" "mail/*")) |
Enter this into your .gnus file and press r to re-read it.
Now we're ready to add our very first IMAP server, first make sure you're in the Gnus group buffer. Press B to browse a foreign buffer. You'll first be asked for a backend, enter mmimap and press enter. Then enter the server name as the address and press enter. If everything works out succesfully you should be left with a list looking something like this:
Each line represents a mail group and how many mails it has. To add a mail group to your group list you move the cursor over it and press u which subscribes it, press u once again to unsubscribe. Once you're happy with your choice you press q which returns you to the group listing with your new IMAP groups. To refresh their content press g.
Now for the last touch on IMAP groups, we want to split mails from the incoming mail box to various groups. All your incoming mail is usually put in /var/spool/mail/username where username is your login name on the IMAP server(The very same as used earlier), and we want to move this mail to our groups. We start by specifying were we get the mail:
(setq nnimap-split-inbox '("/var/spool/mail/imap.user")) |
If you want to you can specify more than one incoming mail box.
Next we define the splitting rules for the mail, for instance:
(setq nnimap-split-rule
'(("INBOX.junk" "^Subject: .*bugs")
("mbox" ".*"))) |
This moves all mail with bugs in the subject to a junk group and all the rest to the mbox. As stated in split rules for the POP3 mail the right hand filter portion uses Regular Expressions for matching text which are explained in this article.
Now you're ready for reading mail from both POP3 and IMAP and at the same time filtering out the bad mail.
And remember "No Gnus is bad news".