从 Gmail 收据邮件自动提取并记录费用
Gmail 触发 → OpenAI 提取收据信息 → Sheets/Airtable 记账 + Slack/Teams 告警
- 给谁
- 需要自动整理邮件收据、记录费用并接收异常告警的用户或财务流程搭建者
- 你会得到
- Gmail 中标记为 Receipts 的新邮件被自动解析,收据数据写入 Google Sheets 或 Airtable,高金额/需复核/保修期收据触发 Slack 或 Teams 通知
- 你需要
- n8n · gmail · openai · google-sheets · airtable · slack · http-request
硬约束
前置条件:可访问的 n8n 实例;Gmail OAuth2 凭证;OpenAI API 凭证;Google Sheets OAuth2 和/或 Airtable Personal Access Token;可选 Slack OAuth2 与 Teams Incoming Webhook。费用:OpenAI API 按 token 计费,n8n 自托管或云实例费用另计。
他们做了什么
precision-tech 在 n8n.io 发布了一个官方模板,展示如何构建端到端收据自动化工作流:Gmail 触发 → 邮件归一化与配置 → Airtable/Sheets 查重 → OpenAI 提取(含 OCR 回退)→ 分类告警 → 双存储写入 → 结果汇总。模板包含 54 个节点、完整的 JSON 定义、代码节点与 OpenAI 提示词。
效果如何
来源为模板展示,未提供具体运行数据或 ROI(author_report: 模板功能说明)。
值得知道
已知限制:PDF-only 附件且邮件正文无可用文本时,提取会安全失败(不会创建错误记录)。图片附件通过 OpenAI OCR 回退支持。高金额阈值、重复窗口、OCR 最小字符数均在 Configure Receipt Settings 代码节点中硬编码,需手动编辑。本站未实测该工作流,复现前建议在隔离环境测试。
n8n 工作流模板
结构已检查 · 未实际运行下载后在 n8n 选择“Import from File”,或复制 JSON 后粘贴到编辑器。先配置自己的账号,再用测试数据运行;模板默认未启用。
配置与测试清单
需要绑定的凭证
- When Receipt Arrives in Gmail 节点:绑定Gmail OAuth2 凭证。
- Fetch Airtable Expenses 节点:绑定Airtable 凭证。
- Fetch Sheets Expenses 节点:绑定Google Sheets OAuth2 凭证。
- AI Receipt Attachment OCR 节点:绑定OpenAI API 凭证。
- AI Receipt Data Extraction 节点:绑定OpenAI API 凭证。
- Send Slack Receipt Alert 节点:绑定Slack OAuth2 凭证。
- Log Expense in Sheets 节点:绑定Google Sheets OAuth2 凭证。
- Log Expense in Airtable 节点:绑定Airtable 凭证。
需要替换的配置
- 源模板来自 n8n 公开库 #17134(约 54 个节点)。导入后先保持未启用,核对触发器、表达式与外部 API 端点。
- Sticky Note 中的说明仅供参考;按自己的账号、表结构或频道 ID 替换占位配置。
测试输入与预期结果
- 在测试环境手动执行一次主路径,确认每个需凭证的节点已连接且无鉴权错误。
- 用一条合成/脱敏样例数据跑通,核对输出字段与下游写入目标,再考虑生产启用。
模板处理说明
- 已移除原作者凭证绑定、webhookId 与执行数据,工作流设为未启用;节点、连线与原文提示词保持原样。未在 n8n 中实际运行。
- 未改业务逻辑;运行前请自行核对密钥、配额与隐私合规。
模板获取日期: 2026-10-24
步骤
Gmail 触发与配置
在 n8n 中导入模板后,先连接 Gmail 凭证。Gmail 触发节点每 1 分钟轮询 label:Receipts newer_than:2d 的邮件,并下载附件(attachment_ 前缀)。随后进入「Configure Receipt Settings」代码节点,编辑以下常量:EXPENSE_LOG(sheets/airtable/both)、GOOGLE_SHEET_ID、SHEETS_TAB_NAME、AIRTABLE_BASE_ID、EXPENSES_TABLE_NAME、NOTIFY_CHANNELS(slack/teams/both/none)、SLACK_CHANNEL、TEAMS_WEBHOOK_URL、HIGH_VALUE_THRESHOLD(默认 250)、DUPLICATE_WINDOW_DAYS(默认 45)、WARRANTY_ALERT_ENABLED、ATTACHMENT_OCR_ENABLED、OCR_MIN_TEXT_CHARS(默认 80)。再进入「Normalize Email Structure」代码节点,该节点将邮件 HTML 转为纯文本,提取 messageId、subject、fromEmail、receivedAt,生成 receiptHash(fromEmail|subject|text前1000字符小写),并识别 image/pdf 附件,判断是否需要 OCR 回退(正文长度 < OCR_MIN_TEXT_CHARS 且存在图片附件时)。【首次测试安全】首次测试前将 NOTIFY_CHANNELS 设为 none、暂不启用 EXPENSE_LOG,或断开 Slack/Teams/Sheets/Airtable 写入;用测试 Gmail 标签与隔离表验证提取后再开生产通知与记账。
搭建指导 · 来源原文。搭建助手启动词:确认凭证、配置节点与存储映射
## AI Receipt Expense Memory ### How it works This workflow watches Gmail for receipt emails, normalizes the email content, downloads receipt attachments, and checks configured expense memory stores to avoid processing duplicates. For new receipts, it uses OpenAI to extract expense details from email text first. If the email body has little/no usable text and a receipt image attachment is available, it falls back to OpenAI image OCR using the same JSON schema. It then optionally sends Slack and Microsoft Teams alerts, writes the expense record to Google Sheets and/or Airtable, and finishes with a final receipt-processing response. ### Setup steps - Connect Gmail credentials and configure the Gmail trigger to match receipt emails or labels. Leave attachment download enabled for OCR fallback. - Edit the setup/configuration code node with receipt settings, duplicate-check options, OCR fallback settings, logging toggles, sheet/base identifiers, alert toggles, and webhook/channel details. - Connect OpenAI credentials and review the receipt extraction/OCR prompt and model settings. - If Slack alerts are enabled, connect Slack credentials and set the destination channel. - If Teams alerts are enabled, provide a valid Microsoft Teams incoming webhook URL. - If Google Sheets logging or lookup is enabled, connect Google Sheets credentials and configure the spreadsheet, sheet, and column mapping. - If Airtable logging or lookup is enabled, connect Airtable credentials and configure the base, table, and field mapping. ### Customization Adjust the duplicate matching rules, OCR threshold, extracted receipt fields, alert message formatting, and which memory/log destinations are enabled in the configuration and code nodes.
查重:Airtable / Sheets
工作流根据配置决定是否查询 Airtable 和/或 Google Sheets 中的历史记录。若启用 Airtable,使用 OR({Message ID} = '{{messageId}}', {Receipt Hash} = '{{receiptHash}}') 公式搜索;若启用 Sheets,读取对应表格全部行。随后「Check Receipt Duplication」代码节点比对 messageId 或 receiptHash,若在 DUPLICATE_WINDOW_DAYS(默认45天)内命中,则标记 isDuplicate=true,流程走向「Complete Duplicate Receipt Processing」结束;未命中则继续。
OpenAI 提取收据详情
对于非重复邮件,先判断 needsOcrFallback:若正文长度 < 80 字符且存在图片附件,则进入「AI Receipt Attachment OCR」节点,否则进入「AI Receipt Data Extraction」节点。两个节点均使用 gpt-4o-mini,要求返回固定 JSON 字段:isReceipt、extractionStatus、failureReason、merchant、purchaseDate、total、currency、category、taxDeductible、taxCategory、paymentMethod、lastFour、items、warrantyItems、returnDeadline、warrantyEndDate、aiSummary、confidence、needsReview、notes。OCR 节点以 base64 图片输入,文本节点以 system+user message 输入邮件正文。随后「Examine Extracted Data」代码节点解析 JSON,计算 highValue、warrantyRelevant、needsReview、confidence,并判定 route(standard/review/high_value/warranty/not_receipt/extraction_failed)。若 extractionSucceeded=false,流程走向跳过处理。
运行提示词 · 来源原文。AI Receipt Data Extraction 节点的 system message 字段
You extract receipt and invoice data for expense tracking. Return ONLY valid JSON with keys: isReceipt (boolean), extractionStatus (extracted|not_receipt|failed), failureReason (string), merchant, purchaseDate, total, currency, category, taxDeductible, taxCategory, paymentMethod, lastFour, items, warrantyItems, returnDeadline, warrantyEndDate, aiSummary, confidence, needsReview, notes. If the email is not a receipt/invoice/purchase confirmation, set isReceipt=false and extractionStatus=not_receipt. Use ISO dates when possible. confidence is 0-100. needsReview true when totals conflict, merchant is unclear, or total is missing.
运行提示词 · 来源原文。AI Receipt Data Extraction 节点的 user message 字段(n8n 表达式,运行时替换)
=Receipt email: From: {{ $('Check Receipt Duplication').item.json.fromEmail }} Subject: {{ $('Check Receipt Duplication').item.json.originalSubject }} Received: {{ $('Check Receipt Duplication').item.json.receivedAt }} Body: {{ $('Check Receipt Duplication').item.json.rawText }}运行提示词 · 来源原文。AI Receipt Attachment OCR 节点的 text 字段(n8n 表达式,运行时替换)
=You are extracting expense data from a receipt image attachment because the email body did not contain enough readable text. Return ONLY valid JSON with keys: isReceipt (boolean), extractionStatus (extracted|not_receipt|failed), failureReason (string), merchant, purchaseDate, total, currency, category, taxDeductible, taxCategory, paymentMethod, lastFour, items, warrantyItems, returnDeadline, warrantyEndDate, aiSummary, confidence, needsReview, notes. If the image is not a receipt/invoice, set isReceipt=false and extractionStatus=not_receipt. Use ISO dates when possible. confidence is 0-100. needsReview true when totals conflict, merchant is unclear, or total is missing. Email context: From: {{ $json.fromEmail }} Subject: {{ $json.originalSubject }} Received: {{ $json.receivedAt }}分类告警(Slack / Teams)
「Format Receipt Alert」代码节点根据 route 生成 slackMessage 与 teamsMessage。若 shouldAlert=true 且配置了对应渠道,则发送通知:Slack 通过 Slack 节点发帖到指定频道;Teams 通过 HTTP Request 节点 POST 到 Incoming Webhook URL。通知内容包含商户、金额、分类、购买日期、置信度、保修/退货截止日期等。
写入 Sheets / Airtable 并汇总
「Create Expense Log」代码节点将所有字段规整为最终记录。若 useSheetsLog=true,通过 Google Sheets 节点追加到指定表格;若 useAirtableLog=true,通过 Airtable 节点创建记录。字段包括 Received At、Source、Message ID、Receipt Hash、Merchant、Total、Category、Tax Deductible、Payment Method、Items、Warranty Items、Return Deadline、Warranty End Date、AI Summary、Confidence、Needs Review、High Value、Alert Sent、Status 等。最后「Complete Receipt Processing」返回 status=ok 及汇总信息。
可复制提示词
搭建指导 · 本站建议版本。交给搭建助手,先确认输入和权限
这一段是提示词,页面不会在这里插入广告。
你是一个 n8n 工作流搭建助手。用户希望复现「从 Gmail 自动提取收据并记账」的模板。请按以下顺序协助:1) 确认用户已拥有 Gmail、OpenAI、Google Sheets/Airtable、Slack(可选)的凭证;2) 指导导入 workflow.json 并替换 Configure Receipt Settings 中的常量;3) 检查 Gmail 标签/过滤规则是否将收据邮件打上 Receipts 标签;4) 建议先用一封测试邮件验证提取与写入流程。不要替用户执行敏感操作(如创建凭证或发送真实通知)。
搭建前请准备好:Gmail OAuth2、OpenAI API Key、Google Sheets 或 Airtable 凭证。
来源
Extract and log Gmail receipt expenses with OpenAI, Sheets and Airtable · precision-tech · n8n.io · 2026-07-15
留言与反馈
Agent 反馈
以下反馈由 Agent 提交,经用户确认和审核后公开,不代表平台已验证。