This script connects to a Gmail account via IMAP, 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 also supports session recovery to handle interruptions and avoid redownloading previously processed emails.
- IMAP Connection: Connects securely to Gmail via IMAP to access emails.
- Attachment Download: Downloads all attachments found in the mailbox.
- 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.
- Python 3.6+
- Required Libraries:
email
(Standard Library)hashlib
(Standard Library)getpass
(Standard Library)imaplib
(Standard Library)os
(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.
Make sure you have Python 3.6 or later installed. You can verify your Python version with:
python --version
-
Clone the Repository
git clone https://github.com/mjseeley/Gmail-Attachment-Downloader.git cd Gmail-Attachment-Downloader
-
Run the Script
python gmail_downer.py
-
User Prompts
- The script will prompt for your Gmail credentials.
- You will be asked to specify a directory where attachments should be saved.
- You can select a sorting method to organize the attachments.
-
Resuming Sessions
- If interrupted, the script can resume from where it left off by recovering from saved state files (
resume.txt
andprocessed_ids.txt
).
- If interrupted, the script can resume from where it left off by recovering from saved state files (
- Extension: Organizes files by their extension (e.g.,
.pdf
,.jpg
). - Size: Organizes files by their size (e.g., tiny, small, large).
- MIMEType: Organizes files by general type (e.g., image, text, video).
- Date: Organizes files into folders based on the email date.
- Sender: Organizes files based on the sender's 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.
To run the script and download all attachments from your Gmail account, use:
python gmail_downer.py
You'll be prompted to enter your Gmail credentials, specify a directory for saving attachments, and choose a sorting method.
Feel free to open issues or submit pull requests with improvements or bug fixes.
This project is licensed under the MIT License.