Using emacs and notmuch as a mail client

This tutorial aims to help you setup notmuch for multiple IMAP accounts and use emacs as a frontend.
( At the time writing this tutorial emacs version was 24.3 and notmuch version was 0.15.2)

Soon after writing this tutorial I experimented with mu and mu4e, the configuration is similar and the experience much better (IMO).

The stack

We are going to use the following stack to fetch, index and finally read our emails.

offlineimap –> notmuch –> emacs

Fetching from the IMAP servers

First we have to setup offlineimap to fetch our emails to a local directory.
A configuration (.offlineimaprc) example follows:

[general]
accounts = Gmail, Other
ui = quiet

[Account Gmail]
localrepository = localG
remoterepository = remoteG
autorefresh = 5
holdconnectionopen = true
keepalive = 60

[Account Other]
localrepository = localI
remoterepository = remoteI
autorefresh = 5
holdconnectionopen = true
keepalive = 60

[Repository localG]
type = Maildir
localfolders = ~/.mail/G

[Repository localI]
type = Maildir
localfolders = ~/.mail/I

[Repository remoteG]
type = IMAP
remotehost = imap.googlemail.com
remoteuser = yourusername
remotepass = yourpassword
ssl = yes
folderfilter = lambda foldername: foldername not in ['Trash', 'Spam', 'Junk', 'All Mail', '[Gmail]/All Mail', '[Gmail]/Spam', '[Gmail]/Trash', '[Gmail]/Junk']
cert_fingerprint = a_hex_code(will explain later)

[Repository remoteI]
type = IMAP
remotehost = mailhost.other.com
remoteuser = yourusername
remotepass = yourpassword
ssl = yes
folderfilter = lambda foldername: foldername not in ['Trash', 'Spam', 'Junk', 'Junk E-mail']
cert_fingerprint = a_hex_code(will explain later)

Next I will shortly explain the configuration.

[general]
accounts Here we declare the IMAP accounts, in this case I want two accounts
ui Here we specify the UI. Since we are going to run it in the background we would like it to be quiet. Quiet will suppress anything but errors.

[Account Name] Here we define account Name.
localrepository is where we want to locally store the emails for this account (which we define later).
remoterepository is where we want to fetch our email from (which we also define later).
autorefresh is the interval (in minutes) between synchronizations with the server.
holdconnectionopen is a hint to keep the connection with the IMAP server open. This way we can skip the overhead of a new connection at every synchronization.
keepalive is the interval (in seconds) between messages to the server to keep the connection open.

[Repository Name] Here we setup the local and remote repositories mentioned earlier.
type Defines the type of the repository. (Note: notmuch does not support mbox).
localfolders Defines the local folder to store the fetched mails.
remotehost Defines the IMAP server.
remoteuser Defines the username for the IMAP server.
remotepass Defines the password for the IMAP server.
ssl Defines whether the IMAP server supports SSL connections (yes/no).<br\> folderfilter Defines a python function that is called before each folder fetch. The folder is fetched only if the function returns true.
cert_fingerprint This is the IMAP server’s fingerprint in hex. This is required only if ssl is enabled. The first time leave this field out and run offlineimap. It will print a message and give you the value you should fill.

That’s it. Now fetch your email invoking offlineimap -u basic (to enable printing, so you can see what’s going on the first time). After this add offlineimap & at your initialization script or in a cronjob to be executed @reboot.

Initial index

Now that we have fetched all our email it is time to setup notmuch. That’s really simple. Run notmuch and answer its questions (run notmuch config to reconfigure it in case you did something wrong the first time). After you are finished with the configuration run notmuch new.<\p>

To initially tag some emails refer to this.

Send emails through SMTP

To achieve this we are using msmpt. Here you can find a configuration (.msmtprc) example.

Emacs setup (.emacs)

First add the melpa repository if you haven’t done it already.

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "http//melpa.milkbox.net/packages") t)
(package-initialize)

Now install the package with:

M-x package-refresh-contents
M-x package-install RET notmuch RET

And finally add these to your configuration file:

(require 'notmuch) ; loads notmuch package
(setq message-kill-buffer-on-exit t) ; kill buffer after sending mail)
(setq mail-specify-envelope-from t) ; Settings to work with msmtp
(setq message-sendmail-envelope-from header)
(setq mail-envelope-from header)
(setq notmuch-fcc-dirs "G/[Gmail]Sent Mail") ; stores sent mail to the specified directory
(setq message-directory "G/[Gmail]Drafts") ; stores postponed messages to the specified directory

For more customization and tricks have a look here.

Previous
Next