Sam Bateman

Getting Started With GNUs

Emacs is quite the beast. It really isn’t an editor so much as a operating system, and a pretty good one. It brings the promise of lisp machines onto every modern operating system and makes it quite easy to build a fantastic experience across systems.

Obviously, one of the more integral functions of such an operating system is email, and here, Emacs is a embarassment of riches. There are many avalible email clients in Emacs. Having tried Wunderlust and a few others, I found they didn’t have much advantages over using a normal client other than org-mode integration. However, there is one that stands out from the rest: GNUs.

Not only a competitent email client, GNUs provides a fantastic way to get through large RSS Feeds and mailing lists quickly, clearly designed by people who are ingesting a lot of information on the regular. It does this by providing a clean interface with almost no required user interation while providing some of the nice feature of a GUI. The simple nature of GNUs interface lets you quickly get through some of the more intense RSS feeds and mailing list. For example, now that I use GNUs, I simply skim all of the submissions to Hacker News right from the fire hse and it still takes less time than reading the first couple pages used to. But thats only one of the many improvements I have seen to productivity.

One of the benifits I didn’t anticipate is that when one uses w3m to render your emails, every image-only marketing email doesn’t take any of your attention and you only get the gist from every email of a similar, text friendly nature. This leaves only the important mail left to actually read and sort through, meaning you spend less time looking at worthless marketing information and more time doing something else!

Now here comes the hard part: GNUs configuration is documented in a very Emacs-y way. Many Emacs users much more versed than I will chuckle at that statement. What I mean is that the Emacs community is very intelligent but isn’t super good at collessing information into one place and ensuring that information stays alive. This means some of the better resources are now just 404 responses and what is left is wiki articles with a ton of broken links. When I started learning Emacs, I was afraid it was because the community was dying. But what I have found since is that Emacs has never been doing better than now, but that most people either use a prepackaged set of extensions and configurations or are very tinkery and build whatever they can’t have out of the box. This means that most of the documentation is right in the code and there aren’t a lot of clear examples outside of random .emacs files on github that say “warning I don’t know if this actually works.”

However, after sifting through a ton of crap, I have built a fairly effective GNUs configuration for my usage. With that said, email is different for everyone and I don’t promise to have the one-true-config for GNUs that will be everything for everyone.

The first thing I have learned, is that nnimap in GNUs is terrible. From what I have gathered myself, I think its a pretty bad idea to try and use it. It makes everything very slow to start and isn’t what elisp is good for. But, there are tools that work with GNUs that are very good that for talking to IMAP servers. The one I decided on is mbsync (used to be known as isync). mbsync provides good support for IMAP and gmail specfic IMAP. It isn’t perfect, but it is the fastest maildir compatible IMAP client and is also pretty reliable (something that I didn’t fully appreciate as a nessesary feature during my short stint with the bug ridden offlineimap).

Getting mbsync is non-obvious but simple on debian. One just installs isync and this installs mbsync and a legacy wrapper for it to act as isync. From there, you will want to download your email. This isn’t instantanious but only took me 45 minutes or so with 50,000 emails but YMMV. Your primary hold up will likely be that Googles IMAP servers get upset if you make too many requests too quickly and will stop giving you data for a period of time. This doesn’t happen regularly, but did happen to me multiple times when setting up my accounts. A good starting .mbsyncrc (living in your $HOME directory) is as follows:

# Not gmail account
IMAPAccount personal
Host imap.not-google.com
user <USERNAME>
pass <PASSWORD>
PipelineDepth 50

IMAPStore personal-remote
Account personal

MaildirStore personal-local
Path ~/.mail/personal
Inbox ~/.mail/personal/Inbox
SubFolders Verbatim

Channel personal
Master :personal-remote:
Slave :personal-local:
Patterns "INBOX" "Other" "Drafts"
Create Both
SyncState *

# Gmail Account
IMAPAccount gmail
Host imap.gmail.com
user <USERNAME>
pass <PASSWORD>
SSLType IMAPS
CertificateFile ~/.cert/imap.gmail.com.pem
AuthMechs LOGIN
PipelineDepth 50

IMAPStore gmail-remote
Account gmail

MaildirStore gmail-local
Path ~/.mail/gmail
Inbox ~/.mail/gmail/Inbox
SubFolders Verbatim

Channel gmail
Master :gmail-remote:
Slave :gmail-local:
Patterns *
Create Both
SyncState *

# More accounts ....

However, if you choose to use this as a long-term solution I recommend getting mbsync to get your password out of a .gpg (or a custom commandline password manager, which, btw, is probably better for ease of use), which it supports pretty simply. Just replace the pass line for each email with:

PassCmd "gpg2 -q --for-your-eyes-only --no-tty -d ~/.mailpass.gpg"

Be sure to direct it to the proper file for each password. I haven’t figured out yet how to put them all into a single .authinfo.gpg yet though, so if you find that out, please email me!

From here, you should just be able to run:

mbsync -a

This should download all your mail from each account. You probably want to set up a recurring task with systemd to run this every 5 minutes or so. There is a great Arch Wiki page here describing it

From here, we have our mail (or atleast most of it) local on our machine. Now we can start getting into the nitty gritty of reading it from GNUs. Like all great *nix utilities, it requires its own config file in your $HOME directory called .gnus.el.

Because of the simplicity of how we set up our maildirs, the configuration isn’t too bad. To get GNUs reading our email, all we need in our .gnus.el file is the following:

(setq user-mail-address    "example@not-google.com"
      user-full-name       "Your Name")

(setq gnus-select-method '(nnnil ""))
(setq gnus-secondary-select-methods
      '((nntp "news.gwene.org")
        (nnmaildir "personal"
		   (directory "~/.mail/personal/"))
	(nnmaildir "gmail"
		   (directory "~/.mail/gmail/"))
	))

(setq send-mail-function 'smtpmail-send-it)
(setq smtpmail-smtp-server "smtp.not-google.com")
(setq smtpmail-smtp-service 587)


(setq mm-text-html-renderer 'gnus-w3m)
(setq gnus-inhibit-images nil)

You can see that all we are doing is pointing GNUs to those folders and telling them they are maildirs. There is a certain beauty in the fact that there is a expert tool doing its job and all your mail client has to do is focus on providing email for you.

You can add as many emails as you would like by the way to this set up, I personally have added 6 and they all work fine.

Now that we have this file, we can run M-x gnus in Emacs and after a short loading period, it should come up with your email boxes as you have configured them!

Now that that is all in order, you should be able to read your email in a simplistic fashion in gnus with org-mode links. Go to the first mailbox with unread mail, hit enter twice and then read that email and hit n to see the next email. If you reach the end of a mailbox, just hit n again and you will be taken to the next mailbox.

This is awesome and fast, which is everything we wanted from email in emacs. What alludes to the power of GNUs is that this is the most basic getting started configuration, we can do better.

One thing that I am personally stumped with is setting up is a mythical notmuch search functionality. Many have spoke of it on mailing-lists but the primary source config is a 404 these days unfortunately. With that said, I have found this gist which has fantastic information but requires that All Mail be downloaded to work with Gmail which I have yet to figure out. Again, if you find anything, feel free to email me!

From here, you probably want to add some RSS feeds. I like http://gwene.org/ for this task. We already set it up as as newsgroup provider when we setup our email, so all you need to do is open emacs (which lands you at what is called the Groups page) and from the Groups page, press ^. You should be given a list of newsgroups/RSS feeds provided by Gwene. To check if a RSS feed is provided by them, go to their website and try and add it. If it is already added, they will give you the name of the newsgroup, which you can just search the group list server buffer (the one we opened with ^) for it and hit u when your cursor is on that line to subscribe to it. In addition, you can type U to bring up a prompt so you don’t have to search the buffer for said group.

From here, you should have a pretty slick, org-compatible setup that gets you through your email! If you have any questions or ideas, just email me at sam[at]bateman[dot]io!

#gnus #emacs #email