- Azure app setup
- config.json configuration
- mbsync configuration
- Emacs SMTP configuration
- Imap notify support
You can follow the instructions here: azure register app or follow the TLDR below
- Login to https://portal.azure.com/#home
- Click on Azure active directory
- Click on App registration > New Registration
- Enter the app’s name
- Enter redirect URI as http://localhost:5000/getToken. The port number and redirect path (“/getToken”) can be configured
- Click on the newly created App, select “Certificates and secrets” and create a “New client secret”. Make sure the copy the secret from the
value
field now! - Click on API permissions > Add a permission.
- Click Microsoft graph > Delegated permission
- Add the “IMAP.AccessAsUser.All” permission
- Add the “User.ReadBasic.All” permission
- Add the “SMTP.Send” permission
That is it. Now follow setting up config.json
below
{
"tenant_id": "TENANT_ID",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET",
"redirect_host": "localhost",
"redirect_port": "5000",
"redirect_path": "/getToken/",
"scopes": ["https://outlook.office.com/IMAP.AccessAsUser.All", "https://outlook.office.com/SMTP.Send"]
}
The TENANT_ID and CLIENT_ID are available on the overview page of the app. CLIENT_SECRET is the one created in step 6. If you entered a different port and redirect path in step 5, modify accordingly.
First install the xoauth2 sasl plugin. Then change the following lines in you mbsync configuration:
... PassCmd oauth2ms AuthMechs XOAUTH2 ...
The following code defines a new method to the authentication mechanisms of smtpmail emacs.
;;; Call the oauth2ms program to fetch the authentication token
(defun fetch-access-token ()
(with-temp-buffer
(call-process "oauth2ms" nil t nil "--encode-xoauth2")
(buffer-string)))
;;; Add new authentication method for xoauth2
(cl-defmethod smtpmail-try-auth-method
(process (_mech (eql xoauth2)) user password)
(let* ((access-token (fetch-access-token)))
(smtpmail-command-or-throw
process
(concat "AUTH XOAUTH2 " access-token)
235)))
;;; Register the method
(with-eval-after-load 'smtpmail
(add-to-list 'smtpmail-auth-supported 'xoauth2))
(setq message-send-mail-function 'smtpmail-send-it
smtpmail-default-smtp-server "smtp.example.com"
smtpmail-smtp-server "smtp.example.com"
smtpmail-stream-type 'starttls
smtpmail-smtp-service 587)
The Imap notify extension allows you to open an active connection to the imap server to listen for events (like new mail) instead of polling frequently. When an event is received, you can trigger certain actions like updating your mail box using mbsync. The imapnotify tool can be configured to listen for events. Sample configuration:
var child_process = require('child_process');
function getStdout(cmd) {
var stdout = child_process.execSync(cmd);
return stdout.toString().trim();
}
exports.host = "outlook.office365.com";
exports.port = 993;
exports.tls = true;
exports.username = "EMAIL";
exports.xoauth2 = getStdout("oauth2ms --encode-xoauth2");
exports.onNewMail = "emacsclient -e '(mu4e-update-mail-and-index 1)'";
exports.boxes = [ "INBOX"];
Once configured, the tool can be started using:
$ imapnotify -c <path_to_config.js>