A tool to synchronize emails from Office 365 to an IMAP server using OAuth2 authentication and ImapSync.
- Python 3.x
- imapsync
- Azure Application with IMAP permissions
- Clone this repository
- Install Python dependencies:
pip install -r requirements.txt
Create a .env
file with the following parameters or pass them as command line arguments:
O365_IMAP_HOST='outlook.office365.com'
O365_IMAP_USER='[email protected]'
CLIENT_ID='your-azure-client-id'
TENANT_ID='your-azure-tenant-id'
CLIENT_SECRET='your-azure-client-secret'
IMAP_HOST='target.imap.server'
IMAP_USER='[email protected]'
IMAP_PASSWORD='target-password'
Run the synchronization script:
./oauth2_imapsync.sh
Or with command line arguments:
./oauth2_imapsync.sh \
--o365-host outlook.office365.com \
--o365-user [email protected] \
--client-id your-client-id \
--tenant-id your-tenant-id \
--client-secret your-client-secret \
--imap-host target.server \
--imap-user [email protected] \
--imap-password "password"
The script will:
- Obtain an OAuth2 token from Office 365
- Store the token in a file
- Synchronize emails using imapsync
- Preserve folder structure and avoid duplicates
- OAuth2 authentication for Office 365
- Automatic token management
- Cross-duplicate detection
- Folder mapping
- No email deletion on source (--noexpunge)
oauth2_imapsync.sh
: Main synchronization scriptget_imap_oauth2_office365.py
: OAuth2 token generator.env
: Configuration filerequirements.txt
: Python dependencies