Accessing Outlook Mail Outside the Proprietary Bloatware

Some month ago, my uni moved the mail accounts to Microsoft Outlook. Whenever I used the web-based, proprietary and bloatware interface of MS Outlook it was a pain. It's super slow, unintuitive and of course, evil proprietary software. After some considerable effort, I managed to set up my Outlook account in Evolution mail (GNOME's mail app). Finally free of the horrible Outlook web interface! Motivated by this success, and after some more considerable effort, I also managed to download Outlook mail using isync and sending mail with msmtp.

In this post, I summarize the steps I've followed to access Outlook from various free software solutions, so you can transition (hopefully) with less friction.

Alternatively, if you want to use Mozilla's Thunderbird, check this guide.

Evolution Mail

  1. Install Evolution and it's EWS plugin, in Debian based distros: apt install evolution evolution-ews.
  2. Once installed, open Evolution running evolution in terminal or via an application launcher of your preference.
  3. You should see a setup wizard similar to this:

mail_1.png

  1. Click Next and set your name and the mail you want to configure. Uncheck the checkbox from the bottom of the window (Look up mail server details…). Click the Next button again.

mail_2.png

  1. In Server Type, select Exchange Web Services. Set the username of your outlook account (possibly your mail address). Below the username, in Host URL, set the following url: https://outlook.office365.com/EWS/Exchange.asmx. Finally, in the Authentication, section, set the drop list value to Oauth2 (Office 365) and click Next.

mail_3.png

  1. For the following configuration windows, you can click Next accepting the default values. Now, when Evolution tries to sync mail for the first time, you will be prompted with a window asking for your Outlook password to give some permissions to Evolution.

isync and msmtp

With isync you will be able to download mail to your computer, and msmtp allows to send mail via terminal or other interfaces such as mutt, neomutt and others (including Emacs of course).

isync

  1. Download isync (the executable of isync is called mbsync), and gpg (this might be already installed in your system).
  2. If you are in a Debian based distro, install libsasl2-modules-kdexoauth2, else, if you are in an Arch based distro install cyrus-sasl-xoauth2-git (AUR). This allows to use XOAUTH2 (required by outlook) with isync.
  3. Run gpg --gen-key, to generate a GPG key, that is needed for the next steps.
  4. Download mutt_oauth.py from mutt's repo, and give it permissions to be executed with chmod u+x mutt_oauth2.py.
  5. Edit mutt_oauth2.py:
    • Replace YOUR_GPG_IDENTITY with the mail that you entered in the gpg key generation step.
    • Set client_id to 9e5f94bc-e8a4-4e73-b8be-63364c29d753.
  6. Run mutt_oauth2.py your@mail.com.token --authorize, that will save the resulting token into your@mail.com.token file (customize this path). When you run the script, it will load a webpage in your web browser, where you will be asked for your Outlook password. Once it verifies your password, it will redirect to a blank page. Look at the URL of this blank page, there is a section of the url under a key named code (it looks like a hash code), copy this code to the terminal where you have mutt_oauth2.py and hit enter.
  7. Add the following contents to your ~/.mbsyncrc:

    IMAPAccount outlook
    # Address to connect to
    Host outlook.office365.com
    User your@mail.com
    AuthMechs XOAUTH2
    PassCmd "mutt_oauth2.py your@mail.com.token" # be sure to set the correct paths here
    # Use SSL
    SSLType IMAPS
    CertificateFile /etc/ssl/certs/ca-certificates.crt  # this path can be different in some distros
    
    IMAPStore outlook-remote
    Account outlook
    
    MaildirStore outlook-local
    SubFolders Verbatim
    # The trailing "/" is important
    Path ~/.mail/outlook/
    Inbox ~/.mail/outlook/Inbox
    
    Channel outlook
    Far :outlook-remote:
    Near :outlook-local:
    Patterns *
    # Automatically create missing mailboxes, both locally and on the server
    Create Both
    # Sync the movement of messages between folders and deletions, add after making sure the sync works
    Expunge Both
    # Save the synchronization state files in the relevant directory
    SyncState *
    

Now, If you execute the command mbsync outlook, it should download your mails to the ~/.mail/outlook directory!

msmtp

  1. Install msmtp with the package manager of your distro, or compile it yourself if you are that kind of person.
  2. Create the file ~/.msmtprc with the following contents:

    account your@mail.com
    from your@mail.com
    user your@mail.com
    auth xoauth2
    passwordeval "mutt_oauth2.py your@mail.com.token" # be careful with the paths in this command
    host smtp.office365.com
    port 587
    tls on
    tls_starttls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt # this path can change depending on the distro
    
    account default : your@mail.com
    

Test that everything is ok with this command: echo -e "\nSending regards from Terminal." | msmtp -a default some-friend@mail.com.

Once you have a isync and msmtp working, you can use great tools as mutt or neomutt to read and send email very efficiently from terminal. But, if you like to live inside Emacs (or you prefer GUI over TUI), the next section of the blog explains how to use Emacs to read/write mail from the confort of your perfectly configured development environment.

Emacs and mu4e

NOTE: This instructions assume that you have a working configuration of isync and msmtp (see the section above).

Although many mail packages exist for Emacs (being the most famous ones Gnus, notmuch and mu4e), in this tutorial I only consider mu4e, that is the one that I prefer.

The first step is to install mu4e. Note that, unlike other packages, mu4e has to be installed using the package manager of your distro (or from source).

Once you have mu4e installed, add the following code to your Emacs configuration:

(use-package mu4e
  :ensure nil ; mu4e is installed with the distro's pkg manager
  :config
  (setq mu4e-maildir "~/.mail/outlook"

        mu4e-sent-folder "/Sent Items"
        mu4e-drafts-folder "/Drafts"
        mu4e-trash-folder "/Deleted Items"

        ;; set to t to avoid mail sync issues when using mbsync
        mu4e-change-filenames-when-moving t
        mu4e-update-interval (* 10 60) ; update every 10mins
        mu4e-get-mail-command "mbsync -a"

        mu4e-maildir-shortcuts '(("/Inbox" . ?i)
                                 ("/Deleted Items" . ?t)
                                 ("/Drafts" . ?d)
                                 ("/Sent Items" . ?s)))

Now, type M-x mu4e and enjoy!