This script will migrate multiple user and shared mailboxes from O365 to a new mail server using IMAP. It will automatically authenticate the user or mailbox.
Steps:
-
Install all the requirements.
-
Register a new app in the O365 Entra Admin Center.
-
Grant Microsoft Graph API permissions (ensure you select "Application permissions," not "Delegated permissions") and provide admin consent for the following
permissions:Mail.Read, Mail.ReadWrite, User.Read.All
-
Go to "Certificates & Secrets" → "Client Secrets," and create a new client secret.
-
Save the following information:
Application (client) ID, Directory (tenant) ID, Client secret value
-
Add all of this information to the config file. Also, select in the config file if you want to migrate attachments and choose the email format: 'html' or 'plain' (note that some mail providers do not support HTML format emails).
-
Populate your CSV file with user data and IMAP server details as shown in the example. Run the code, and it will migrate all the emails.