This script connects to a Gmail account via IMAP, lets you select a mailbox folder, identifies all emails containing attachments, and downloads those attachments. Users can specify a directory to save the attachments and choose from various sorting methods, such as by file extension, size, type, sender, or date. The script supports session recovery to handle interruptions and avoid redownloading previously processed emails, with a persistent manifest file to prevent overwriting attachments across sessions.
- IMAP Connection: Connects securely to Gmail via IMAP to access emails.
- Folder Selection: Lists all available Gmail folders and lets you choose which one to process.
- Attachment Download: Downloads all attachments found in the selected folder.
- Unnamed Attachment Handling: Automatically generates a filename for attachments that have none.
- Sorting Methods: Attachments can be sorted by:
- File Extension
- File Size
- File MIME Type
- Date Year -> Month -> Day
- Sender Domain -> sender
- Session Recovery: Supports resuming the download process to avoid redownloading attachments.
- Manifest Persistence: Saves a
file_manifest.jsonfile to track downloaded files across sessions, preventing overwrites on resume. - Windows Long Path Support: Automatically applies the
\\?\extended-length path prefix for file paths exceeding 260 characters on Windows. - Environment Variable Support: Optionally set your password via the
EMAIL_PASSWORDenvironment variable.
- Python 3.6+
- Required Libraries:
email(Standard Library)hashlib(Standard Library)getpass(Standard Library)imaplib(Standard Library)os(Standard Library)re(Standard Library)json(Standard Library)logging(Standard Library)collections(Standard Library)pathlib(Standard Library)enum(Standard Library)organize(Included Custom Module)
-
Make sure you have IMAP enabled in your GMail settings.
-
If you are using 2 step verification you may need an APP Password.
-
Reference information for GMail IMAP extension can be found here.
-
Clone the Repository
git clone https://github.com/mjseeley/Gmail-Attachment-Downloader.git cd Gmail-Attachment-Downloader -
Verify Python Version
Make sure you have Python 3.6 or later installed:
python --version
-
Run the Script
python gmail_downer.py
-
User Prompts
- The script will prompt for your Gmail credentials (or use the
EMAIL_PASSWORDenvironment variable). - You will be shown a numbered list of available Gmail folders and asked to select one.
- You will be asked to specify a directory where attachments should be saved.
- You can select a sorting method to organize the attachments.
- The script will prompt for your Gmail credentials (or use the
-
Resuming Sessions
- If interrupted, the script can resume from where it left off by recovering from saved state files (
resume.txt,processed_ids.txt, andfile_manifest.json). - The manifest file tracks file counters and content hashes so attachments are never overwritten when resuming.
- If interrupted, the script can resume from where it left off by recovering from saved state files (
You can set your password as an environment variable to avoid entering it each time:
# Linux/macOS
export EMAIL_PASSWORD="your-app-password"
# Windows (PowerShell)
$env:EMAIL_PASSWORD="your-app-password"
# Windows (Command Prompt)
set EMAIL_PASSWORD=your-app-password- Extension: Organizes files by their extension (e.g.,
.pdf,.jpg). - Size: Organizes files by their size into categories:
- Tiny: < 10 KB
- Small: 10 KB - 100 KB
- Medium: 100 KB - 1 MB
- Large: 1 MB - 10 MB
- Huge: > 10 MB
- MIMEType: Organizes files by general type (e.g., image, text, video).
- Date: Organizes files into folders based on the email date (Year/Month/Day).
- Sender: Organizes files based on the sender's domain and email address.
The script uses getpass to securely input your Gmail password. You will need to set up an App Password instead of using your regular main Gmail password.
- Login failed: Ensure IMAP is enabled in your Gmail settings and you are using an App Password if 2FA is enabled.
- FileNotFoundError: Make sure the destination directory path is valid and you have write permissions.
- Connection errors: Check your internet connection and firewall settings. Gmail IMAP uses port 993.
- Empty folders created: This can occur when sorting by date if attachments were already saved elsewhere and matched by hash (duplicate detection).
Feel free to open issues or submit pull requests with improvements or bug fixes.
This project is licensed under the MIT License.