How to Use Email within Emacs

A photograph of a bundle of mail envelopes spread on a surface.

Emacs is a powerful application. It allows you to integrate various parts of your daily workflow into a single cohesive utility. You can also use it to manage your emails. Here we show you how you can integrate your email inbox in Emacs and how to properly link Emacs to Gmail in order to send and receive email directly from it.

Tip: Download our Emacs keyboard shortcuts cheatsheet to increase your productivity.

Setting Up Email Into Emacs

You can easily add an email inbox in Emacs with the help of Offlineimap and Notmuch. They allow you to save your email in an Org buffer to either annotate it or save it as a to-do item in a calendar. You can also write an article in an Org buffer and send it as an email.

Installing Offlineimap

Offlineimap is a helper program that fetches your email from a remote server and saves it on your disk.

Run the following command to install the program in your system:

sudo apt install offlineimap

You can also use pacman to install Offlineimap in Arch Linux:

sudo pacman -S offlineimap

In RHEL and Fedora, you can use dnf:

sudo dnf install offlineimap

Setting up Offlineimap

Create the user config file for your Offlineimap install using your favorite text editor:

nano /home/$USER/.offlineimaprc

Paste the following block of code inside your new config file:

[general]
accounts = gmailaccount
 
[Account gmailaccount]
localrepository = localgmail
remoterepository = remotegmail
 
[Repository localgmail]
type = GmailMaildir
localfolders = /home/$USER/mail/youremail@gmail.com
 
[Repository remotegmail]
type = Gmail
maxconnections = 1
remotehost = imap.gmail.com
remoteuser = youremail@gmail.com
remotepass = your_password_goes_here
ssl = yes
sslcacertfile = /your/ca/cert/path/here

Replace the email address inside the “localfolders” and “remoteuser” variables to your Gmail account.

A terminal showing the Offlineimap information for email in Emacs.

Check whether your system has a proper system certificate file:

ls /etc/ssl/certs/

Copy the absolute path of your certificate file and paste it inside the “sslcacertfile” variable.

A terminal showing a sample certificate path for Offlineimap.

Good to know: Learn more SSL and how it works by creating self-signed certificates using OpenSSL.

Obtaining a Gmail App Password for Offlineimap

Open your Gmail account, then click your user icon on the page’s upper right corner.

A screenshot highlighting the user icon on the Gmail webpage.

Click “Manage your Google Account.”

A screenshot highlighting the "Manage your Google Account" button.

Click the “Security” category on the page’s left sidebar, then ensure that 2-Step Verification is currently active for your account.

Select the “2-Step Verification” menu item.

A screenshot highlighting the 2-Step Verification button inside the Security page.

Scroll to the bottom of the verification page, then click the “App Passwords” button.

A screenshot showing the App Password button inside the Verification prompt.

Provide a name for your new App Password, then click “Create.”

A screenshot showing a sample App Password prompt.

Copy the text inside the blue box to a separate notepad, then click “Done.”

Go back to your .offlineimaprc file and replace the value of your “remotepass” variable with your App Password.

A terminal highlighting the remotepass variable for Offlineimap.

FYI: Learn how you can send fully encrypted emails using Gmail.

Installing Notmuch

Notmuch is a simple application that tags and displays mail. It does this by maintaining a database of all your emails and tags them. Notmuch organizes and displays these emails in Emacs using those tags.

Install the notmuch utility to your system:

sudo apt install notmuch

You can also install notmuch using pacman in Arch Linux:

sudo pacman -S notmuch

In RHEL and Fedora, you can install it using dnf:

sudo dnf install notmuch

Open your Emacs client, then press Ctrl + X, then F.

Navigate to your client’s “.emacs” file inside your home directory.

Paste the following line of code inside the file:

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))
(package-initialize)

To install the notmuch package, press Alt + X and type package-install. Emacs will ask for the name of the package that you want to install. Type “notmuch.”

A screenshot showing the MELPA repository information for email in Emacs.

Once installed, this package will serve as your front end for notmuch.

Setting up Notmuch

First, ensure that offlineimap is working properly and has fetched all the available mail from your Gmail account:

offlineimap

Once done, run notmuch in the command line. This will start a configuration script that will ask about your specific email setup.

Provide the full name on your Gmail account that you want to link, then press Enter.

A terminal showing the notmuch welcome prompt for email in Emacs.

Provide the complete Gmail address of the account that you want to link, then press Enter twice.

Press Enter twice again to accept the default mail directory for notmuch as well as the default tags for your mailboxes.

Initialize notmuch’s database by running the following command:

notmuch new

This will read your configuration file and create a database based on sensible defaults. From there, you can now use notmuch’s tagging commands to tag both your incoming and existing email.

Viewing Your Email in Emacs

At this point, you can now view your emails inside Emacs. Pressing Alt + X and typing notmuch will load the notmuch front-end in Emacs.

Click the “inbox” link to load all the messages from your Gmail account.

A screenshot showing the notmuch email frontend in Emacs.

Press Ctrl + N to scroll down through all the messages with an “inbox” tag.

A screenshot showing a notmuch email list in Emacs.

Aside from that, you can also trigger a “tree-style” view by pressing Shift + Z while viewing a specific tag. This is useful when you are subscribed to a mailing list since it displays the emails in easy-to-follow threads.

Sending Emails in Emacs

To enable email support, open your .emacs file and paste the following block of code inside it:

(setq user-mail-address "your-email@gmail.com"
      user-full-name "Ramces Red")
(setq mail-user-agent 'message-user-agent)
(setq message-send-mail-function 'smtpmail-send-it
      smtpmail-stream-type 'starttls
      smtpmail-smtp-server "smtp.gmail.com"
      smtpmail-smtp-service 587)

You need to allow Emacs to send email through your mail account. Do this by creating a “.authinfo” file inside your home directory:

touch /home/$USER/.authinfo

One important thing to note is that this file will contain sensitive information, such as your username and password. Therefore, you need to secure its access permissions so that only you can read and write to it:

chmod 600 /home/$USER/.authinfo

From there, paste the following line of code inside your new authinfo file:

machine smtp.gmail.com login username@gmail.com port 587 password #REPLACE-ME#

Replace the email address with your Gmail account’s complete address and the “#REPLACE-ME#” value with your account’s App Password.

A screenshot showing a sample credentials for email in Emacs.

Reload Emacs to apply your new settings.

Preparing Your First Email in Emacs

To send an email from Emacs, press either Ctrl + X, or M while inside the notmuch buffer. This will create a message buffer with an empty heading where you can type your message.

A screenshot showing a sample email using SMTP sendit in Emacs.

Once done, press Ctrl + C, Ctrl + C to send your first email from Emacs.

Using email in Emacs is just one of the few things that you can do with this powerful text editor. For example, you can also use Emacs as a music player with EMMS as well as a fully fledged RSS reader using Elfeed.

Meanwhile, if you are looking for small tweaks that can quickly improve your Emacs experience check out these five useful packages for this extensible text editor.

Image credit: Joanna Kosinka via Unsplash. All alterations and screenshots by Ramces Red.

Is this post useful?
Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.