If you want to backup the emails from your Microsoft Office 365 mailbox you can utilize the execellent OfflineIMAP program. In this article I will outline how you can configure and use OfflineIMAP for this specific use case.

Install OfflineIMAP

If you are running a recent Linux distribution OfflineIMAP is likely to be included in the standard software repositories of your distribution. Suppose you are running Kubuntu. Then you can just use the command sudo apt install offlineimap to install OfflineIMAP:

# show info about used linux distribution
user@pc:~$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=21.04
DISTRIB_CODENAME=hirsute
DISTRIB_DESCRIPTION="Ubuntu 21.04"
user@pc:~$ 

# install offlineimap
user@pc:~$ sudo apt install offlineimap
[sudo] password for user: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
offlineimap is already the newest version (7.3.3+dfsg1-1+0.0~git20210218.76c7a72+dfsg-1).
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
user@pc:~$ 

Installation instructions for other operating systems can be found over at the website of OfflineIMAP.

Configure OfflineIMAP

You can tell OfflineIMAP which mailbox you want to backup by editing the file .offlineimaprc in your home folder:

# file ~/.offlineimaprc

# documentation:
#   https://github.com/OfflineIMAP/offlineimap/blob/master/offlineimap.conf
# offlineimap command docs:
#   http://www.offlineimap.org/doc/offlineimap.html
# overview of docs relate to offlineimap:
#   https://www.offlineimap.org/documentation.html

[general]
accounts = outlook365

[Account outlook365]
localrepository = outlook365-local
remoterepository = outlook365-remote
maxsyncaccounts = 1

[Repository outlook365-local]
type = Maildir
localfolders = ~/offlineimap-targets/outlook365

[Repository outlook365-remote]
type = IMAP
remoteuser = bill@mycompany.com       # set your username / email here
remotepass = mysecretpassword         # set your password here
remotehost = outlook.office365.com
remoteport = 993
ssl = yes
realdelete = no
sync_deletes = no
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
# only backup the folders listed here
folderfilter = lambda folder: folder in ['INBOX', 'Drafts', 'Sent Items' ]

By default OfflineIMAP will backup all IMAP folders. When using Outlook 365 there are IMAP folders which contain data other than emails. Those IMAP folders are hidden to the user but OfflineIMAP will try to synchronize them if not configured otherwise. This might lead to problems. Therefore it is advised to provide an explicit list of IMAP folders you want OfflineIMAP to synchronize.

By the way. If you want to synchronize the emails from the Sent Items folder into a folder with another name (e.g. sent) this is also possible. See here and here for further details.

Run OfflineIMAP

After you have created the configuration file ~/.offlineimaprc you can start the backup process by executing the offlineimap command. Your emails will then be backuped into the folder ~/offlineimap-targets/outlook365.

One of the nice things about OfflineIMAP is that you can run OfflineIMAP again any time later and OfflineIMAP will only fetch the emails which you have received since the last backup run and update the backup accordingly.

OfflineIMAP and other email providers

OfflineIMAP can also be used for other email providers of course. It uses IMAP (Internet Message Access Protocol) to access the mails stored on the servers of your mail provider. IMAP is the defacto standard for such use cases.

A note about Netcup (advertisement)

Netcup offers inexpensive, yet powerfull web hosting packages, KVM-based root servers or dedicated servers for example. Using a code generated by my Netcup coupon code generator you can even save more money (5$ on your first purchase, 30% off any KVM-based root server, ...).