Email with Emacs

Last updated: 2023-09-18

Comments on Mastodon

All posts: Index

I've used email for a long time. I have all my email since 2008, I want to have a local copy of all my email as more often than not, I go back in time to search for something from my email archives. I think that email is the best digital communication tool we humans have today.

I've used Thunderbird for a long time. In recent years I've been using Mutt, but as html-based email is more and more popular (Unfortunately so, stop using it, now!), using Mutt gets annoying. I'm using Emacs all the time , writing code, writing web pages and whatnot. So why not use it for reading and writing email too?

Installing mu4e, mbsync and msmtp

This article assumes you have Emacs installed already and that you're familiar with it. I use Fedora Linux, so some commands and package names might be different, for example on Ubuntu system.

To install mu4e: sudo dnf install maildir-utils. This will install mu1 and mu4e which is a Emacs front-end for mu.

To install msmtp: sudo dnf install msmtp. This will install msmtp2, which we're using for sending emails from mu4e.

To install mbsyn: sudo dnf install isync. This will install mbsync3, which we're using to synchronize with our imap-server.

How to configure mu4e and Emacs?

I will share my mu4e and Emacs configuration and I encourage you to try mu4e for email. Especially, if you use Emacs for other things too.

Here's mu4e related part from my ~/.emacs.

            
;; tell Emacs where to find mu4e
(add-to-list 'load-path "/usr/share/emacs/site-lisp/mu4e")
(require 'mu4e)

(setq
 mu4e-headers-skip-duplicates  t
 mu4e-view-show-images t
 mu4e-view-show-addresses t
 mu4e-compose-format-flowed nil
 mu4e-date-format "%y/%m/%d"
 mu4e-headers-date-format "%Y/%m/%d"
 mu4e-change-filenames-when-moving t
 mu4e-attachments-dir "~/Downloads"

 ;; top-level Maildir (mbsync puts email in here)
 mu4e-maildir       "~/Maildir"

 mu4e-refile-folder "/Archive"
 mu4e-sent-folder   "/Sent Items"
 mu4e-drafts-folder "/Drafts"
 mu4e-trash-folder  "/Trash")

 ;; Re-index every 5 minutes.
(setq mu4e-update-interval 300)
(setq mu4e-user-mail-address-list '("niko@byteptr.com"))

(setq user-mail-address "niko@byteptr.com")

;; this setting allows to re-sync and re-index mail
;; by pressing U
(setq mu4e-get-mail-command  "mbsync -a")

;; use msmtp for sending emails
(setq message-send-mail-function 'message-send-mail-with-sendmail
      sendmail-program "msmtp")

;; Start mu4e, you could just run M-x mu4e
(global-set-key [f9] 'mu4e)

;; By default mu4e shows only 500 results for every search, this is usually
;; more than enough. However sometimes I want them all.
(global-set-key [f8] 'mu4e-headers-toggle-full-search)

        

This is pretty much all it's needed. I also use custom mu4e bookmarks, but I don't include them here as they contain other people email addresses.

How to configure msmtp

msmtp is used to send email. It's easy to configure, you just have to know the details for the smtp-server you're going to use. As I use Fastmail, these are related to that. Here's my ~/.msmtprc:


account default
host smtp.fastmail.com
from niko@byteptr.com

auth on
user ""
password ""

tls on
tls_starttls off

        

Obviously I'm not going to tell my user and password here, but you get the idea. If you type your password directly to the ~/.msmtprc, you need to set the permissions so that only you can read the file. For example: chmod 600 ~/.msmtprc. It's probably a better idea to use passwordeval setting instead of password. See here.

How to configure mbsync

We use mbsync to synchronize ~/Maildir with the IMAP-server. Configuring mbsync is simple, at least with Fastmail. I've heard that Gmail uses some non-standard IMAP stuff which needs more complicated configuration, if you use Gmail, search for the web and you will find examples. Here's my ~/.mbsyncrc:

            
IMAPAccount fastmail
Host imap.fastmail.com
Port 993
User ""
# For simplicity, this is how to read the password from another file.
# For better security you should use GPG https://gnupg.org/
PassCmd "cat ~/.mbsync-fastmail"
SSLType IMAPS
SSLVersions TLSv1.2

IMAPStore fastmail-remote
Account fastmail

# This section describes the local storage
MaildirStore fastmail-local
Path ~/Maildir/
Inbox ~/Maildir/INBOX
# The SubFolders option allows to represent all
# IMAP subfolders as local subfolders
SubFolders Verbatim

# This section a "channel", a connection between remote and local
Channel fastmail
Far :fastmail-remote:
Near :fastmail-local:
Patterns *
Expunge None
CopyArrivalDate yes
Sync All
Create Near
SyncState *

        

Next you want to run mbsync -a to fetch all the email to ~/Maildir.

Start using mu4e

Now you need to run mu init --maildir=~/Maildir --my-address="your@ownemailhere.com" and mu index to let mu to initialize and index your email for the first time. After those commands you can start using Emacs and mu4e.

Fire up your Emacs and do M-x mu4e RET. mu4e has a good documentation and it's relatively simple to use. When you need help, C-h m will display help in mu4e. There's also quite a lot of documentation around the web.


Copyright © Niko Rosvall