A Python script to archive emails from any IMAP-compatible email server to local storage. Works with Gmail, Outlook, Yahoo, iCloud, and any email provider supporting IMAP access.
- IMAP SSL Connection: Secure connection to IMAP servers
- Date-based Archiving: Archive emails older than a specified number of days
- Folder Selection: Archive all folders or specific folders only
- Dry Run Mode: Preview what would be archived without making changes
- Safe Deletion: Optionally delete archived emails from server (with confirmation)
- Progress Statistics: Detailed reporting of processed, archived, and deleted emails
The script supports configuration via environment variables in a .env file. Command line arguments override .env values.
| Variable | Description | Required |
|---|---|---|
EMAIL_ADDRESS |
Email address | Yes |
EMAIL_PASSWORD |
Email password (prompts if not provided) | Yes |
IMAP_SERVER |
IMAP server hostname | Yes |
IMAP_PORT |
IMAP port | No (default: 993) |
USE_SSL |
Use SSL connection | No (default: true) |
DAYS_OLD |
Archive emails older than N days | No (default: 365) |
ARCHIVE_DIR |
Directory to save archived emails | No (default: ./EmailArchive) |
FOLDER |
Specific folder to archive (optional) | No |
DELETE_AFTER_ARCHIVE |
Delete from server after archiving | No (default: false) |
.env file to version control. It's already added to .gitignore.
- Python 3.6+
python-dotenvfor environment variable support (see Installation)
- Install the required dependency:
pip install -r requirements.txt- Configure your environment variables by copying
.env.exampleto.env:
cp .env.example .env- Edit
.envwith your email provider settings:
# Email Configuration
EMAIL_ADDRESS=your_email@example.com
EMAIL_PASSWORD=your_password
# IMAP Server Configuration - Common settings:
# Gmail: imap.gmail.com
# Outlook/365: outlook.office365.com
# Yahoo: imap.mail.yahoo.com
# iCloud: imap.mail.me.com
IMAP_SERVER=imap.gmail.com
IMAP_PORT=993
USE_SSL=true
# Archiving Options
DAYS_OLD=365
ARCHIVE_DIR=./EmailArchive# Dry run - see what would be archived (emails older than 1 year)
python archive_emails.py --dry-run
# Archive emails older than 6 months and delete from server
python archive_emails.py --days-old 180 --delete --archive-dir ~/EmailArchive
# Archive specific folder only
python archive_emails.py --folder "INBOX" --days-old 90| Option | Description | Required |
|---|---|---|
--email |
Email address | Yes (or set in .env) |
--password |
Email password (prompts if not provided) | Yes (or set in .env) |
--imap-server |
IMAP server hostname | Yes (or set in .env) |
--imap-port |
IMAP port | No (default: 993) |
--no-ssl |
Disable SSL (not recommended) | No |
--days-old |
Archive emails older than N days | No (default: 365) |
--folder |
Specific folder to archive | No |
--archive-dir |
Directory to save archived emails | No (default: ./EmailArchive) |
--delete |
Delete emails from server after archiving | No |
--dry-run |
Preview without making changes | No |
--list-folders |
List available folders and exit | No |
List all folders in the mailbox:
python archive_emails.py --list-foldersDry run for emails older than 1 year:
python archive_emails.py --days-old 365 --dry-runArchive emails older than 6 months to a custom directory:
python archive_emails.py --days-old 180 --archive-dir ~/Backups/EmailsArchive and delete from server (
python archive_emails.py --days-old 365 --deleteArchive specific folder only:
python archive_emails.py --folder "Sent" --days-old 90Archived emails are saved as .eml files in the following structure:
EmailArchive/
├── INBOX/
│ ├── 2023-01-15_Meeting_Notes_12345.eml
│ ├── 2023-02-20_Project_Update_12346.eml
│ └── ...
├── Sent/
│ ├── 2023-03-10_Re_Proposal_12347.eml
│ └── ...
└── ...
Files are named using the pattern: YYYY-MM-DD_Subject_MessageID.eml
- Dry Run Mode: Always test with
--dry-runfirst to see what would be archived - Password Prompt: Password is securely prompted if not provided via command line
- SSL by Default: All connections use SSL/TLS encryption by default
- System Folder Skip: Automatically skips
[Gmail],Trash,Spam,Junkfolders - Verify Before Delete: Emails are only deleted from server after confirming the local file was written correctly (size verification)
- Partial File Cleanup: Corrupt/partial files are automatically removed if verification fails
- Expunge on Delete: Properly expunges deleted messages when using
--delete
- IMAP Server:
imap.gmail.com - Port:
993 - SSL: Yes
- Notes: If you have 2-Factor Authentication enabled, you must use an App Password instead of your regular password.
- IMAP Server:
outlook.office365.com - Port:
993 - SSL: Yes
- Notes: If you have MFA enabled, use an App Password.
- IMAP Server:
imap.mail.yahoo.com - Port:
993 - SSL: Yes
- IMAP Server:
imap.mail.me.com - Port:
993 - SSL: Yes
- Notes: Generate an app-specific password from Apple ID settings.
- IMAP Server: Your server's hostname (e.g.,
mail.yourdomain.com) - Port: Usually
993for SSL, sometimes143for non-SSL - SSL: Depends on your server configuration
MIT License - See project repository for details.