Assembled by Márton Balázs, IT rep for Maths
last updated: [2025-04-12]
This is a tested example of reading emails via IMAP and sending via SMTP to O365 using Oauth2 authentication. It is using isync for getting email locally, NeoMutt for reading from file and sending via msmtp. Hopefully the below will make sense with other clients as well. Notice that IMAP and SMTP access is turned off by default at the UoB, you can request this with IT Services.
libsasl2-dev
package, otherwise cyrus-sasl-xoauth2 will complain.
autogen.sh
and configure
, but before make
and make install
, find the line
pkglibdir = ${CYRUS_SASL_PREFIX}/lib/sasl2in
Makefile
, and change it to
pkglibdir = ${CYRUS_SASL_PREFIX}/lib/x86_64-linux-gnu/sasl2(source of wisdom).
isync
installed (see below) then reinstall it so that it notices the new sasl plugin.
~/.config/oauth2ms/config.json
:
{ "tenant_id": "<see your ITS' guides>", "client_id": "<see your ITS' guides>", "client_secret": "", "redirect_host": "localhost", "redirect_port": "5000", "redirect_path": "/appredirecturi/", "scopes": ["https://outlook.office.com/IMAP.AccessAsUser.All", " https://outlook.office.com/SMTP.Send"] }where
tenant_id
and client_id
are to be provided by your IT Services. Click here for the UoB ones. I suspect that redirect_port
is irrelevant but cannot be left empty.oauth2ms
it will guide you through getting a token, see the details on their GitHub page. The token is stored in ~/.local/share/oauth2ms/credentials.bin
, make sure the disk or this part of your filesystem is encrypted. Alternatively, the token can be encrypted with the -e
option.oauth2ms
regenerate it.~/.mbsyncrc
:
IMAPAccount uob PipelineDepth 1 # Address to connect to Host outlook.office365.com User ab12345@bristol.ac.uk PassCmd oauth2ms AuthMechs XOAUTH2 SSLType IMAPS CertificateFile /etc/ssl/certs/ca-certificates.crt Timeout 180of course write in your UoB userid.
~/.msmtprc
:
defaults protocol smtp tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt account uob host smtp.office365.com port 587 auth xoauth2 from x.yzuv@bristol.ac.uk user ab12345@bristol.ac.uk passwordeval oauth2msof course write in your public email address and UoB userid.
~/.mutt/muttrc
(or the sub-config you link in there):
set realname = 'xab yzuv' set from = x.yzuv@bristol.ac.uk unset smtp_url unset smtp_pass set sendmail = "/usr/bin/msmtp -a uob"again, set your real name and public email address.