Emacs Mail Configuration with Mu4e and Outlook 365
This is how I have mail configured for Emacs here at my employer, who is using Outlook with Office 365.
System Packages
I am using Nix’s Home-Manager to maintain my system packages, including Emacs. The relevant parts of my configuration are these:
let
# Inspiration: https://github.com/NixOS/nixpkgs/issues/108480#issuecomment-1115108802
isync-oauth2 =
with pkgs;
buildEnv {
name = "isync-oauth2";
paths = [ isync ];
pathsToLink = [ "/bin" ];
nativeBuildInputs = [ makeWrapper ];
postBuild = ''
wrapProgram "$out/bin/mbsync" \
--prefix SASL_PATH : "${cyrus_sasl}/lib/sasl2:${cyrus-sasl-xoauth2}/lib/sasl2"
'';
};
in
{
programs.emacs = {
enable = true;
extraPackages =
epkgs: with epkgs; [
mu4e
];
};
home.packages =
with pkgs;
[
mu
msmtp
pizauth
] ++ [isync-oauth2];
}
I am pretty sure that having mu listed along with mu4e is redundant but I couldn’t get Emacs to find the mu executable otherwise.
The wonky stuff there with isync is due to it not normally building with Cyrus SASL and we need XOAUTH2 as a plugin in order for the next steps to work.
Pizauth Configuration
account "EMPLOYER" {
auth_uri = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
token_uri = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
client_id = "9e5f94bc-e8a4-4e73-b8be-63364c29d753"; // Fill in with your Client ID
scopes = [
"https://outlook.office365.com/IMAP.AccessAsUser.All",
"https://outlook.office365.com/SMTP.Send",
"offline_access"
];
}
You will want to fill in EMPLOYER with something more
specific.
The client_id you see there is Thunderbird’s, from https://github.com/thunderbird/thunderbird-desktop/blob/main/mailnews/base/src/OAuth2Providers.sys.mjs#L319; if it stops working, search for that line in whatever is current for Thunderbird.
You’ll need to arrange for pizauth server to be
executed; on Linux, services.pizauth.enable = true will
suffice but you’ll just have to run it manually on a Mac for some
reason.
You should be able to authenticate by using
pizauth show EMPLOYER; this will output a URL and after you
authenticate, your OpenID key or whatever is going to be displayed. The
nice thing about pizauth is that it doesn’t store this anywhere on disk,
just in memory, so you don’t have to configure GPG or anything like you
would with oama.
Mbsync Configuration
IMAPAccount EMPLOYER
Host outlook.office365.com
Port 993
User YOUR_EMAIL@WHEREVER
PassCmd "pizauth show EMPLOYER"
AuthMechs XOAUTH2
TLSType IMAPS
IMAPStore EMPLOYER-remote
Account EMPLOYER
MaildirStore EMPLOYER-local
Subfolders Verbatim
Path ~/Maildir/
Inbox ~/Maildir/Inbox
Channel EMPLOYER
Far :EMPLOYER-remote:
Near :EMPLOYER-local:
Patterns *
Create Both
Expunge Both
SyncState *
You’re going to want to update all the lines I have capitalized:
EMPLOYER and YOUR_EMAIL@WHEREVER. Pay
attention to the User line, and the Path and Inbox lines.
The first sync is a doozy but it should work if you just run
mbsync -a, you can also run mbsync --dry-run
if you want to see if it will work first.
If you get an error about XOAUTH2 not being supported, make sure you are using the patched version mentioned above. If you’re not using NixOS or home-manager, see this Stack Overflow question for advice.
Run mu
Once you have your mail downloaded, run
mu init -m $HOME/Maildir --my-address YOUR_EMAIL@WHEREVER.
This crashed for me with a BUS ERROR on a certain mail
message. I wound up going to some effort to isolate the message but it
looked like it amounted to an encoding problem on a message I don’t care
about, so I just gave up on it.
Msmtp Configuration
account default
auth xoauth2
host smtp.office365.com
port 587
tls on
tls_starttls on
protocol smtp
from YOUR_EMAIL@WHEREVER
user YOUR_EMAIL@WHEREVER
passwordeval pizauth show EMPLOYER
If you call this account something other than default mu4e will have some trouble sending.
Make sure you fix your from/user lines to your email address.
Mu4e Configuration
This is my configuration but feel free to change it however you like:
(use-package mu4e
:ensure t
:config
(setq sendmail-program (executable-find "msmtp")
send-mail-function 'sendmail-send-it
message-send-mail-function 'sendmail-send-it
user-mail-address "YOUR_EMAIL@WHEREVER"
mu4e-get-mail-command (concat (executable-find "mbsync") " -a")
mu4e-update-interval 300
mu4e-user-email-address-list '("YOUR_EMAIL@WHEREVER")
mu4e-refile-folder "/Archive"
mu4e-sent-folder "/Sent Items"
mu4e-drafts-folder "/Drafts"
mu4e-trash-folder "/Deleted Items")
(add-to-list 'mu4e-bookmarks
'(:name "Inbox" :key ?i :query "maildir:/Inbox")))
And that’s it!