每天花30分钟回邮件?让AI替你读和写,一行命令搞定已读 + 分类 + 回复草稿
TL;DR每天处理 50 封邮件要半小时。写了一个 Python 脚本自动读取邮件、用 AI 分类优先级、生成回复草稿。5 分钟处理 50 封人工只需确认发送。1. 痛点每天几十封邮件大部分是通知、垃圾、订阅真正需要回复的只有 5-10 封每封都要读完才能判断是否重要写回复还要组织语言耽误时间本质问题邮件太多人工筛选成本太高。2. 效果实测50 封邮件人工处理30 分钟AI 处理5 分钟节省时间83%分类准确率94%需人工复核重要邮件3. 环境准备bashpip install google-api-python-client openai python-dotenv3.1 Gmail API 授权去 Google Cloud Console 创建项目启用 Gmail API创建 OAuth 2.0 凭据Desktop App 类型下载credentials.json放到项目目录4. Gmail 读取 AI 分类Python - email_ai.pyimport os import json from pathlib import Path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from openai import OpenAI SCOPES [https://www.googleapis.com/auth/gmail.readonly, https://www.googleapis.com/auth/gmail.compose] client OpenAI() # 1. Gmail 授权 def get_gmail_service(): creds None token_path Path(token.json) if token_path.exists(): creds Credentials.from_authorized_user_info(json.loads(token_path.read_text()), SCOPES) if not creds or not creds.valid: flow InstalledAppFlow.from_client_secrets_file(credentials.json, SCOPES) creds flow.run_local_server(port0) token_path.write_text(creds.to_json()) return build(gmail, v1, credentialscreds) # 2. 读取未读邮件 def fetch_unread_emails(service, max_results: int 20) - list: results service.users().messages().list( userIdme, qis:unread, maxResultsmax_results ).execute() messages results.get(messages, []) emails [] for msg in messages: msg_data service.users().messages().get( userIdme, idmsg[id], formatfull ).execute() headers msg_data[payload][headers] subject sender for h in headers: if h[name].lower() subject: subject h[value] if h[name].lower() from: sender h[value] # 提取正文 body get_email_body(msg_data[payload]) emails.append({ id: msg[id], from: sender, subject: subject, body: body[:500], # 限制长度节省 token }) return emails def get_email_body(payload: dict) - str: parts payload.get(parts, []) for part in parts: if part[mimeType] text/plain: data part[body].get(data, ) import base64 return base64.urlsafe_b64decode(data).decode(utf-8, errorsignore) return 5. AI 分类 生成回复Python - AI 分类与回复生成# 3. AI 分类邮件 def classify_email(subject: str, body: str, sender: str) - dict: 用 AI 判断邮件优先级和类型 prompt f分析以下邮件返回 JSON 格式 邮件主题{subject} 发件人{sender} 正文{body} 返回格式 {{ priority: high | medium | low, type: 工作 | 客户 | 订阅 | 垃圾 | 通知, action: reply | read_only | archive | delete, summary: 一句话总结这封邮件的核心内容, reason: 判断理由3-5字 }} priority 判断标准 - high需要回复的工作/客户邮件涉及决策/截止日期/协作 - medium信息类但可能有用 - low订阅通知/系统邮件/垃圾 response client.chat.completions.create( modelgpt-4o-mini, messages[{role: user, content: prompt}], max_tokens300 ) return json.loads(response.choices[0].message.content) # 4. AI 生成回复草稿 def generate_reply(subject: str, body: str, sender: str) - str: 用 AI 生成回复草稿 prompt f你是我的邮件助手根据以下邮件内容生成一封专业、简洁的回复邮件。 原邮件 主题{subject} 发件人{sender} 正文{body} 要求 1. 用中文回复如果原邮件是英文则用英文 2. 语气专业但亲切 3. 长度控制在 3-5 句话 4. 只写正文不要写主题 直接输出回复内容 response client.chat.completions.create( modelgpt-4o-mini, messages[{role: user, content: prompt}], max_tokens500 ) return response.choices[0].message.content # 5. 批量处理 def process_emails(): service get_gmail_service() print( 获取未读邮件...) emails fetch_unread_emails(service, max_results20) print(f 共 {len(emails)} 封未读邮件开始 AI 分析...\n) high_priority [] for email in emails: classification classify_email( email[subject], email[body], email[from] ) email[classification] classification # 高优先级且需要回复的生成草稿 if classification[priority] high and classification[action] reply: reply generate_reply( email[subject], email[body], email[from] ) email[draft] reply high_priority.append(email) import time; time.sleep(0.3) # 打印摘要 print(\n *60) print(f 分析完成) print(f 高优先级需回复{len(high_priority)} 封) print(f 其他{len(emails) - len(high_priority)} 封) print(*60 \n) for email in high_priority: cls email[classification] print(f 主题{email[subject]}) print(f 摘要{cls[summary]}) print(f 理由{cls[reason]}) print(f 回复草稿\n{email[draft]}) print(-*60 \n) # 保存结果 with open(email_analysis.json, w, encodingutf-8) as f: json.dump(emails, f, ensure_asciiFalse, indent2) print( 详细结果已保存到 email_analysis.json) if __name__ __main__: process_emails()6. 输出示例运行效果 获取未读邮件... 共 12 封未读邮件开始 AI 分析... 分析完成 高优先级需回复3 封 其他9 封 主题关于Q3产品需求的反馈 摘要客户对功能优先级提出异议希望调整排期 理由涉及决策 回复草稿 王总感谢您的反馈。关于排期问题我们评估后会在本周五前给您 一个明确的方案。您的意见对我们非常重要我们会认真考虑。 ------------------------------------------------------------ 主题【GitHub】您的代码已被合并 摘要PR #234 已合并到 main 分支 理由通知无需回复 ------------------------------------------------------------7. 安全说明⚠️ 重要Gmail API 凭据credentials.json不要提交到 GitHubtoken.json 包含认证信息同样保密建议把这两个文件加入 .gitignore如果是公司邮箱请确认公司政策允许自动化处理8. 扩展方向自动回复发送加上 Gmail send 权限一键发送 AI 回复钉钉/飞书通知高优先级邮件推送到即时通讯定时处理每天早上 8 点自动跑一次回复确认发到草稿箱人工确认后再发送9. 总结这个脚本解决三个问题读AI 自动分类高优先级 vs 低优先级判AI 判断是否需要回复给出摘要写AI 生成回复草稿人工审核后发送注意涉及真实邮件数据安全第一。credentials.json 和 token.json 绝对不要外传。如果对你有帮助欢迎在评论区分享你的邮件处理经验。

相关新闻