Query-driven selection
Define Gmail queries and attachment filters per rule. Keep it narrow and precise.
Gmail + NAS automation
A fast, focused Python tool to fetch matching emails, store attachments in structured folders, and optionally delete processed messages.
Define Gmail queries and attachment filters per rule. Keep it narrow and precise.
Attachments are saved by sender and date, making retrieval predictable.
Scan PDF or text attachments for required phrases before saving.
Automatically convert saved PDFs to txt, png, or jpeg in a separate output folder.
Retries, logging, and optional delete-after-save keep runs clean.
pip install -r requirements.txt
python -m build
pip install .
export-gmail-attachments-to-nas \
--username YOUR_NAS_USER \
--password YOUR_NAS_PASSWORD \
--credentials /path/to/credentials.json \
--criteria /path/to/criteria.json
Create a criteria.json file to define your email processing rules. Each entry in criteria is evaluated independently.
{
"smb_server": "homenas.home.lan",
"criteria": [
{
"enabled": true,
"query": "{subject:Invoice subject:Receipt} filename:.pdf",
"smb_folder": "\\documents\\invoices",
"filters": [".pdf"],
"attachment_content_filter": ["Invoice", "Total"],
"delete_after_save": false,
"convert": {
"to": "txt",
"output_folder": "\\documents\\invoices-txt",
"extension_filter": [".pdf"],
"filename_filter": "invoice.*"
}
}
]
}
| Field | Required | Description |
|---|---|---|
enabled | ❌ | Set to false to skip this rule. Defaults to true. |
query | ✅ | Gmail search query (supports all Gmail search operators). |
smb_folder | ✅ | Destination folder on the SMB share. |
filters | ❌ | Optional list of file extensions to save (e.g. [".pdf"]). If omitted or empty, no attachments are saved for this rule. |
attachment_content_filter | ❌ | Save only if the attachment contains at least one of these phrases (case-sensitive match). |
delete_after_save | ❌ | Delete the source email after a successful save. Defaults to false. |
convert | ❌ | Optional block to convert attachments. See Convert section. |
Each rule can include a convert block to automatically convert saved attachments to another format
and store the result in a separate output folder. Omitting it leaves the rule's behaviour unchanged.
txt — full text extractionpng — one image per pagejpeg / jpg — one image per page"convert": {
"to": "txt",
"output_folder": "\\documents\\converted",
"extension_filter": [".pdf"],
"filename_filter": "invoice.*"
}
| Field | Required | Description |
|---|---|---|
to | ✅ | Target format: txt, png, jpeg, or jpg. |
output_folder | ✅ | SMB folder path where converted files are saved. |
extension_filter | ❌ | Restrict conversion to attachments with these extensions (e.g. [".pdf"]). |
filename_filter | ❌ | Restrict conversion to filenames matching this regex (case-insensitive). |
This tool requires Google OAuth credentials to access Gmail. Follow these steps to create them:
https://mail.google.com/credentials.json in your project directorytoken.json automaticallyStore NAS credentials in env vars or pass them at runtime.
Gmail tokens are stored locally in token.json. Treat it as sensitive.
Use minimal required Gmail scope. Delete requires mail.google.com.
Limit SMB share permissions to a dedicated account when possible.
Thanks to all the amazing people who have contributed to this project!