This is a guide to setting up access to email from a Gmail account (eg: @gmail.com) within Emacs (Doom Emacs) using mu
, mbsync
(isync
), and mu4e
(mu
for emacs
).
You may have to complete additional steps if you are not using doom emacs
(a), Gmail, or if your set-up differs from mine in other ways.
Prerequisites
Please be sure you’ve completed these steps before continuing:
Required Software
Install
mbsync
version >= 1.4.1.We will use
mbsync
to download our email. It is essential that the version is greater than or equal to 1.3.3 to ensure the OAuth2 token is not cut off (see line 1891 in drv_imap.c (a))git clone https://git.code.sf.net/p/isync/isync isync cd isync ./autogen.sh ./configure make sudo make install
Install
mu
.mu
will index the emails we’ve downloaded withmbsync
and provide them to themu4e
package.sudo apt install maildir-utils
Install
mu4e
.mu4e
is our email client within emacs.sudo apt install mu4e
Download
gmail-oauth2-tools
.This utility will generate our refersh tokens for us.
git clone https://github.com/google/gmail-oauth2-tools
Download Cyrus SASL Oauth2
mbsync
doesn’t support OAuth2, so we’ll install a plugin which enables it.# Reference: https://unix.stackexchange.com/a/632794/423626 # Clone the Cyrus SASL OAuth2 sources. git clone https://github.com/moriyoshi/cyrus-sasl-xoauth2.git # Configure and make. cd cyrus-sasl-xoauth2 ./autogen.sh ./configure # SASL2 libraries on Ubuntu are in /usr/lib/x86_64-linux-gnu/; modify the Makefile accordingly sed -i 's%pkglibdir = ${CYRUS_SASL_PREFIX}/lib/sasl2%pkglibdir = ${CYRUS_SASL_PREFIX}/lib/x86_64-linux-gnu/sasl2%' Makefile make sudo make install # Install saslpluginviewer sudo apt install sasl2-bin # Verify XOAUTH2 is known to SASL. saslpluginviewer | grep XOAUTH2
Create an OAuth2 Client ID
Please refer to: Create an OAuth2 Client ID
Create mbsyncrc
- Create a new file at
~/.mbsyncrc
(or symlink to that location, etc). - Base your config on mine, included below:
# Account Information IMAPAccount gmail Host imap.gmail.com User cashbweaver@gmail.com AuthMechs XOAUTH2 PassCmd "/path/to/gen_email_access_token.sh" SSLType IMAPS IMAPStore gmail-remote Account gmail MaildirStore gmail-local Path ~/.mail/cashweaver-gmail Inbox ~/.mail/cashweaver-gmail/inbox Channel gmail-inbox Far :gmail-remote: Near :gmail-local: Patterns "INBOX" Channel gmail-sent Far :gmail-remote:"[Gmail]/Sent Mail" Near :gmail-local:sent Remove Near Create near SyncState * Channel gmail-trash Far :gmail-remote:"[Gmail]/Trash" Near :gmail-local:trash Remove Near Create near SyncState * Channel gmail-drafts Far :gmail-remote:"[Gmail]/Drafts" Near :gmail-local:drafts Remove Near Create near SyncState * Group gmail Channel gmail-inbox Channel gmail-sent Channel gmail-trash Channel gmail-drafts
Emable IMAP in your Gmail Account
Follow the documentation (a) to enable IMAP.
Enable mu4e
within emacs
This step is written for those using doom emacs
(a). Those using other flavors of emacs
will need to enable mu4e
on their own.
Enable
mu4e
withindoom emacs
(a) by uncommenting(mu4e +gmail)
withininit.el
.Sync
doom emacs
.doom sync
Reload
doom emacs
by either killingemacs
and restarting, or by runningdoom/reload
(SPC : doom/reload
orSPC h r r
).
Sync and index your email
Follow the steps documented by doom emacs’ mu4e section (a).
Sync your email by running
mbsync --all
.This may take a while.
Index your email by running:
mu init --maildir ~/.mail --my-address <you>@gmail.com mu index
Configure emacs to use your email address.
As an example:
;; Each path is relative to `+mu4e-mu4e-mail-path', which is ~/.mail by default (set-email-account! "gmail.com" '((mu4e-sent-folder . "/gmail.com/Sent Mail") (mu4e-drafts-folder . "/gmail.com/Drafts") (mu4e-trash-folder . "/gmail.com/Trash") (mu4e-refile-folder . "/gmail.com/All Mail") (smtpmail-smtp-user . "<you>@gmail.com") (mu4e-compose-signature . "<Your signature here>"))