Gmail Enhanced
Advanced Gmail integration with powerful automation features.
Features
1. Advanced Search
- - Complex query building (AND, OR, NOT)
- Date range filters
- Attachment filters
- Label filters
- Search within attachments
2. Label Management
- - Create, rename, delete labels
- Nested labels (folders)
- Color customization
- Label statistics
3. Attachment Handling
- - Download attachments
- Upload and send attachments
- Filter attachments by type/size
- Extract attachments to cloud storage
4. Email Processing
- - Auto-categorize emails
- Extract data from emails
- Auto-reply rules
- Email templates
- Bounce detection
5. Email Parsing
- - Extract structured data
- Parse invoices, receipts
- Extract contact info
- HTML parsing
Prerequisites
- 1. Enable Gmail API in Google Cloud Console
- Create OAuth 2.0 credentials
- Download credentials.json
- Generate tokens.json (run once with authentication)
Configuration
CODEBLOCK0
Or place credentials in default locations:
Usage
Send Email
CODEBLOCK1
Advanced Search
CODEBLOCK2
Label Management
CODEBLOCK3
Attachment Handling
CODEBLOCK4
Auto-categorization
CODEBLOCK5
API Reference
Core Methods
| Method | Description |
|---|
| INLINECODE2 | Send email |
| INLINECODE3 |
Search emails |
|
get_message(msg_id, format) | Get email details |
|
delete_message(msg_id) | Move to trash |
|
archive_message(msg_id) | Archive email |
Label Methods
| Method | Description |
|---|
| INLINECODE7 | Create label |
| INLINECODE8 |
Rename label |
|
delete_label(name) | Delete label |
|
get_labels() | List all labels |
|
get_label_stats(label) | Get label statistics |
Attachment Methods
| Method | Description |
|---|
| INLINECODE12 | Download attachment |
| INLINECODE13 |
Search and download |
|
get_attachment_info(msg_id) | List attachments |
Automation Methods
| Method | Description |
|---|
| INLINECODE15 | Create processing rule |
| INLINECODE16 |
Run all rules |
|
create_template(name, subject, body) | Create email template |
|
send_template(template_name, to, variables) | Send using template |
Parsing Methods
| Method | Description |
|---|
| INLINECODE19 | Extract structured data |
| INLINECODE20 |
Parse invoice data |
|
extract_contacts(msg_id) | Extract email addresses |
Email Query Syntax
CODEBLOCK6
Error Handling
Common errors:
- -
invalid_credentials: Re-authenticate - INLINECODE23 : Wait and retry
- INLINECODE24 : Message ID invalid
- INLINECODE25 : Check scopes
Scopes Required
CODEBLOCK7
Links
Gmail增强版
具备强大自动化功能的高级Gmail集成。
功能特性
1. 高级搜索
- - 复杂查询构建(AND、OR、NOT)
- 日期范围筛选
- 附件筛选
- 标签筛选
- 附件内容搜索
2. 标签管理
- - 创建、重命名、删除标签
- 嵌套标签(文件夹)
- 颜色自定义
- 标签统计
3. 附件处理
- - 下载附件
- 上传并发送附件
- 按类型/大小筛选附件
- 提取附件至云存储
4. 邮件处理
- - 自动分类邮件
- 从邮件中提取数据
- 自动回复规则
- 邮件模板
- 退信检测
5. 邮件解析
- - 提取结构化数据
- 解析发票、收据
- 提取联系人信息
- HTML解析
前置条件
- 1. 在Google Cloud Console中启用Gmail API
- 创建OAuth 2.0凭据
- 下载credentials.json文件
- 生成tokens.json文件(通过身份验证运行一次)
配置
bash
export GMAILCREDENTIALSPATH=/path/to/credentials.json
export GMAILTOKENPATH=/path/to/tokens.json
或将凭据放置在默认位置:
- - ~/.credentials/gmail-credentials.json
- ~/.credentials/gmail-token.json
使用方法
发送邮件
python
from gmail_enhanced import GmailClient
gmail = GmailClient()
简单邮件
gmail.send(
to=recipient@example.com,
subject=你好,
body=邮件内容
)
带附件
gmail.send(
to=recipient@example.com,
subject=报告,
body=请查收附件中的报告,
attachments=[report.pdf]
)
高级搜索
python
复杂查询
results = gmail.search(
query=from:boss@company.com,
label=INBOX,
after=2024/01/01,
has_attachments=True
)
使用OR搜索
results = gmail.search_or([
subject:紧急,
label:重要
])
标签管理
python
创建标签
label = gmail.create_label(项目/工作/Q1, color=#4A90E2)
获取标签统计
stats = gmail.get
labelstats(INBOX)
应用标签
gmail.add
labels([标签1, 标签2], messageids)
附件处理
python
从搜索结果下载附件
attachments = gmail.search_attachments(
query=subject:发票,
save_dir=./下载
)
上传附件
gmail.send(
to=recipient@example.com,
subject=文件,
attachments=[/path/to/file.pdf]
)
自动分类
python
创建规则
gmail.add_rule(
name=分类发票,
query=subject:发票 has:attachment,
add_labels=[已处理/发票]
)
运行规则
gmail.process_rules()
API参考
核心方法
| 方法 | 描述 |
|---|
| send(to, subject, body, attachments, cc, bcc) | 发送邮件 |
| search(query, max_results, label) |
搜索邮件 |
| get
message(msgid, format) | 获取邮件详情 |
| delete
message(msgid) | 移至回收站 |
| archive
message(msgid) | 归档邮件 |
标签方法
| 方法 | 描述 |
|---|
| createlabel(name, color) | 创建标签 |
| renamelabel(oldname, newname) |
重命名标签 |
| delete_label(name) | 删除标签 |
| get_labels() | 列出所有标签 |
| get
labelstats(label) | 获取标签统计 |
附件方法
| 方法 | 描述 |
|---|
| downloadattachment(msgid, attachmentid, savepath) | 下载附件 |
| searchattachments(query, savedir) |
搜索并下载 |
| get
attachmentinfo(msg_id) | 列出附件 |
自动化方法
| 方法 | 描述 |
|---|
| addrule(name, query, actions) | 创建处理规则 |
| processrules() |
运行所有规则 |
| create_template(name, subject, body) | 创建邮件模板 |
| send
template(templatename, to, variables) | 使用模板发送 |
解析方法
| 方法 | 描述 |
|---|
| parseemail(msgid) | 提取结构化数据 |
| extractinvoice(msgid) |
解析发票数据 |
| extract
contacts(msgid) | 提取邮箱地址 |
邮件查询语法
基本:
from:user@example.com
to:user@example.com
subject:关键词
精确短语
筛选:
after:2024/01/01
before:2024/12/31
older_than:7d
newer_than:2h
标记:
has:attachment
has:drive
is:unread
is:starred
is:important
标签:
label:INBOX
label:工作
组合:
from:老板 AND subject:紧急
(from:张三 OR from:李四) AND is:unread
错误处理
常见错误:
- - invalidcredentials:重新进行身份验证
- ratelimit:等待后重试
- notfound:消息ID无效
- permissiondenied:检查权限范围
所需权限范围
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/gmail.labels
https://www.googleapis.com/auth/gmail.modify
链接