SaveGmail is a powerful Python script designed to backup emails and attachments from Gmail. It provides a complete solution for archiving and organizing your important emails.
- Download emails with complete metadata
- Extract and save email attachments
- Secure OAuth 2.0 authentication
- Automatic file organization
- Precise timezone handling
- Gmail label management
- Intuitive command-line interface
- Python 3.6 or higher
- Google Cloud Platform project with Gmail API enabled
- OAuth 2.0 credentials file (
credentials.json) - Gmail account with required labels
-
Clone the repository :
git clone https://github.com/C0sm0cats/GMAIL.git
-
Install dependencies :
playwrightfor browser automationgoogle-authandgoogle-api-python-clientfor Gmail APIpytzandtzlocalfor timezone handlingpython-dateutilfor date processingemail.parser.BytesParserfor email parsing (included in Python standard library)
-
Google Cloud Platform Setup
- Create a project on Google Cloud Console
- Enable Gmail API
- Create OAuth 2.0 credentials
- Download
credentials.jsonto the project directory
-
Gmail Labels Setup Create these labels in your Gmail account :
HasAttachment(for emails with attachments to process)HasAttachment/SavedAsPDF(where processed emails will be moved)
-
Download Directory Setup Modify the
DOWNLOAD_PATHvariable in the script if needed (default :~/Downloads/) -
Run the Script
python savegmail.py
-
Authentication
- Follow terminal instructions
- Authorize the app to access your Gmail account
- The script handles token refresh automatically
- Automatic extraction of all attachment types
- Filename cleaning for compatibility
- Organization in subfolders by sender and date
- Preserved HTML email formatting
- Email to PDF conversion (using Playwright)
- Complete metadata included (From, To, Date, Subject, etc.)
- Detailed operation logging
- Robust connection error handling
- Resume capability for interrupted downloads
You can customize the script's behavior by modifying these variables :
SCOPES: Gmail API permissionsDOWNLOAD_PATH: Download destination directory- Logging parameters
-
Authentication Errors
- Verify
credentials.jsonexists - Check Google Cloud Console permissions
- Verify
-
Download Issues
- Check destination directory permissions
- Ensure sufficient disk space
Contributions are welcome ! Feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Built with ❤️ by C0sm0cats