Importing Legacy Emails From O365

20 Jun 2024 - sj, tags: archiving, insights, news, product

Microsoft 365 (formerly office365) is a popular email service provider. It’s often a requirement to archive the legacy emails already in o365.

The latest release of piler enterprise features a tool that downloads all emails from o365 using the GraphQL API that you can import.


Create a new application with a secret in Azure portal

Grant the following application type permissions:

  • Mail.Read
  • User.ReadBasic.All

Install the msal python3 package

pip install msal


Create, and set the values for client_id, client_secret and tenant_id. Feel free to customise the list of SKIP_FOLDERS. These are IMAP folders that won’t be processed.

DEBUG = False
SKIP_FOLDERS = ['Deleted Items', 'Drafts', 'Junk Email']
client_id = ".........."
client_secret = "......"
tenant_id = "......."


You can download emails from a single mailbox --email

Or you can download all users’ emails by omitting the –email argument.

The tool creates a directory called ‘email’ in the current directory. Each mailbox gets its own directory, eg. 8f7cd945-c816-491b-8c8c-577bd664189a

The tool preserves the IMAP folder structure, eg.

ls -la emails/8f7cd945-c816-491b-8c8c-577bd664189a

total 12
drwxr-xr-x 2 piler piler 4096 May 30 09:29  Inbox/
drwxr-xr-x 3 piler piler 4096 May 30 09:29  folder2/
drwxr-xr-x 2 piler piler 4096 May 30 09:29 'test folder 1'/

When you have downloaded all emails, run pilerimport to import them. The command below processes all emails in the “emails” directory, and removes the processed emails:

pilerimport -d emails -r


Contact Us