8
n8n 中文网amn8n.com

📨 使用GPT-4o mini自动分类和标记现有Gmail邮件

高级

这是一个Personal Productivity, AI Summarization领域的自动化工作流,包含 18 个节点。主要使用 Gmail, Filter, Switch, ManualTrigger, Agent 等节点。 📨 使用GPT-4o mini自动分类和标记现有Gmail邮件

前置要求
  • Google 账号和 Gmail API 凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "0a82a9d5784b67ad963ceff89283b2e40cb771be10fe58ba2682f3598a37c6e1",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "a31a7280-34ee-44bf-80e4-0a366ab0e649",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -560,
        1392
      ],
      "parameters": {
        "color": 7,
        "width": 736,
        "height": 972,
        "content": "## 1. Manual Trigger + Gmail Fetch + Label Checker\n[Read more about Manual Trigger node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.manualworkflowtrigger/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.manualTrigger)\n[Read more about Gmail Get node](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.gmail/draft-operations/#get-many-drafts)\n[Read more about Filter node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.filter/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.filter)\n\n🖱️ Manual inbox processing\n\nThis section starts the workflow manually with a click, allowing you to process a batch of existing emails already in your Gmail inbox.\nIt fetches a defined number of messages (default: 50) and uses a Filter node to skip emails that already have labels — ensuring only unlabeled emails are passed to AI.\nYou can adjust the number of emails to fetch by changing the limit in the Gmail node."
      },
      "typeVersion": 1
    },
    {
      "id": "69df5c49-b33d-4d71-a687-b60650f17297",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1184,
        1392
      ],
      "parameters": {
        "width": 588,
        "height": 1628,
        "content": "## 📨 Categorize and Label Existing Gmail Emails Automatically with GPT-4o mini\n\n## 👥 Who's it for\n\nThis workflow is perfect for individuals or teams who want to sort and label existing emails in their Gmail inbox 🗃️ using AI. Ideal for cleaning up unlabeled emails in bulk — no coding required!\n\nFor sorting incoming emails messages in your gmail inbox, please use this free workflow: [Categorize and Label Incoming Gmail Emails Automatically with GPT-4o mini](https://n8n.io/workflows/5595-categorize-and-label-gmail-emails-automatically-with-gpt-4o-mini/)\n\n## 🤖 What it does\n\nIt manually processes a selected number of existing Gmail emails, skips those that already have labels, sends the content to an AI Agent powered by GPT-4o mini 🧠, and applies a relevant Gmail label based on the email content. All labels must already exist in Gmail.\n\n## ⚙️ How it works\n\n▶️ Manual Trigger – The workflow starts manually when you click \"Execute Workflow\".\n\n📥 Gmail Get Many Messages – Pulls a batch of existing inbox emails (default: 50).\n\n🚫 Filter – Skips emails that already have one or more labels.\n\n🧠 AI Agent (GPT-4o mini) – Analyzes the content and assigns a category.\n\n🧾 Structured Output Parser – Converts the AI output into structured JSON.\n\n🔀 Switch Node – Routes each email to the right label based on the AI result.\n\n🏷️ Gmail Nodes – Apply the correct Gmail label to the email.\n\n## 📋 Requirements\n\nGmail account connected to n8n\n\nGmail labels must be manually created in your inbox beforehand\n\nLabels must exactly match the category names defined in the AI prompt\n\nOpenAI credentials with GPT-4o mini access\n\nn8n's AI Agent & Structured Output Parser nodes\n\n## 🛠️ How to set up\n\nIn your Gmail account, create all the labels you want to use for categorizing emails\n\nOpen the workflow and adjust the email fetch limit in the Gmail node (e.g., 50, 100)\n\nConfirm that the Filter skips emails that already have labels\n\nDefine your categories in the AI Agent prompt — these must match the Gmail labels exactly\n\nIn the Switch Node, create a condition for each label/category\n\nEnsure each Gmail Label Node applies the correct existing label\n\nSave the workflow and run it manually whenever you want to organize your inbox ✅\n\n## 🎨 How to customize the workflow\nAdd or remove categories in the AI prompt & Switch Node\n\nAdjust the batch size of emails to process more or fewer per run\n\nFine-tune the AI prompt to suit your inbox type (e.g., work, personal, client support)\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "fd2816b9-05f1-4717-8a48-eee68b34e9ed",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        208,
        1392
      ],
      "parameters": {
        "color": 7,
        "width": 560,
        "height": 972,
        "content": "## 2. AI Categorization + Structured Output\n\n[Read more about AI Agent node](https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.agent/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=%40n8n%2Fn8n-nodes-langchain.agent)\n[Read more about Structured Output Parser node](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.outputparserstructured/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=%40n8n%2Fn8n-nodes-langchain.outputParserStructured)\n\n🧠 Analyze and extract label\n\nThis section uses an AI Agent powered by GPT-4o mini to analyze the subject and body of each email and determine the appropriate label based on your custom prompt.\nThe AI's response is parsed by the Structured Output Parser, which converts it into clean JSON like:\n{ \"email_label\": \"work\" }\nMake sure the categories returned match exactly with the Gmail labels you've already created."
      },
      "typeVersion": 1
    },
    {
      "id": "f36f9faf-daa7-47ac-8508-46dda10483e9",
      "name": "Sticky Note13",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        1392
      ],
      "parameters": {
        "color": 7,
        "width": 760,
        "height": 1620,
        "content": "## 3. Apply labels based on AI output\n[Read more about Switch node editing](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.switch/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.switch)\n[Read more about Gmail node editing](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.gmail/message-operations/#add-label-to-a-message)\n\n🏷️ Apply labels based on AI output\nThe Switch node uses email_label from the AI response to route each email to the correct label path.\nEach branch corresponds to one category.\nThen, each Gmail node applies the label that matches the category.\n⚠️ Make sure each route and label name matches exactly."
      },
      "typeVersion": 1
    },
    {
      "id": "23ffde1f-a7ec-47e8-b4bb-6f1741e32074",
      "name": "Give a Label AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        336,
        1792
      ],
      "parameters": {
        "text": "=Topic: {{ $json.subject }}\nDescription: {{ $json.text }}\nSender: {{ $json.from.text }}",
        "options": {
          "systemMessage": "=**Your Role**\n\nYou are an intelligent email assistant responsible for sorting incoming messages. Your task is to analyze the content, subject, and sender of each email and assign it to the appropriate category.\n\n**Categories**\n\nCategories for sorting:\n\n-Work: Emails related to jobs, coworkers, meetings, projects, or professional tasks.\n\n-Personal: Messages from friends, family, or general personal life content.\n\n-Finance: Bank emails, invoices, receipts, bills, or payment confirmations.\n\n-Shopping: Order confirmations, promotions, receipts, or product updates from stores.\n\n-Travel: Booking confirmations, tickets, hotel or car rental details, itineraries.\n\n-Newsletters: Recurring marketing or informational emails from subscriptions or blogs.\n\n-Others: Relevant emails that don’t clearly fit into any of the categories above.\n\n**Instructions**\n\n-For each email:\n\n-Analyze the content, subject, and sender\n\n-Assign it to the appropriate category\n\n-Add a label with the category\n\n-If you are unsure, do not label the message\n\n**Response Format**\n\nAlways return the result in JSON format:\n\n{\n  \"email_label\": \"Shopping\"\n}\n\nIMPORTANT – the response must use the exact label format as listed (capitalized with spaces).\n"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.8
    },
    {
      "id": "1bd62e9b-2b93-4e79-99c0-f993a04079b3",
      "name": "Label Checker Filter",
      "type": "n8n-nodes-base.filter",
      "position": [
        0,
        1792
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "6c287523-4ae2-43c4-8f9a-faf756f01855",
              "operator": {
                "type": "string",
                "operation": "notContains"
              },
              "leftValue": "={{ $json.labelIds[0] }}",
              "rightValue": "Label"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "c2d50354-0427-41bf-89f8-fb931bfcaefa",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        832,
        2096
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Work",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "10893f03-231e-4dba-bfdf-69530f908b4d",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Work"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Personal",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "23e430c2-ac8b-4fb5-bb5a-1fafb9e41b09",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Personal"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Finance",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "53698e8e-f038-405d-a1d3-274dba7fb2a2",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Finance"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Shopping",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "b3b7ffc6-d046-402f-8c9d-2e19d00b49a7",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Shopping"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Travel",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "4e0a670b-4017-4d83-b01a-78a79c55daf2",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Travel"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Newsletters",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "f0079792-cf4b-4c3e-8aed-48e2dddb4e05",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Newsletters"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Others",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "a0c39fdd-3312-4a34-8cd4-41a1a10f9149",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Others"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "1300cdf3-5b22-45ee-af81-5d94723c10e3",
      "name": "Work",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1312,
        1712
      ],
      "webhookId": "5bdf6b2d-0758-46b7-aaf6-986bfe12ec95",
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "cb4ea5c6-a638-425d-8b45-a8621a8a0e33",
      "name": "Personal",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1312,
        1872
      ],
      "webhookId": "94e0ab36-8603-4087-b255-60540d7b590a",
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "1db6e7ff-6770-4a66-8f05-3fae33b597f0",
      "name": "Finance",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1312,
        2032
      ],
      "webhookId": "77838975-4c75-48c8-be70-eed18459a492",
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "19233ac1-848d-4d3b-abca-4022f3ffa17c",
      "name": "Shopping",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1152,
        2176
      ],
      "webhookId": "f609cdc3-d9a6-425c-89ff-e4e8c6bcf3cf",
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "99b15563-82da-4321-8ff0-60c21861f764",
      "name": "travel",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1312,
        2288
      ],
      "webhookId": "3af1d5fe-1f25-49ea-9c9d-faa959fa7dbe",
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "bfa96076-0b6f-4c39-8f75-e2e6312d2831",
      "name": "Newsletters",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1312,
        2464
      ],
      "webhookId": "6d81f863-edcb-487c-a553-638b2b6bfd0a",
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "2f2937a3-22b8-4a71-920d-1393056eee25",
      "name": "Others",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1312,
        2656
      ],
      "webhookId": "ea8c4ab1-7d55-48ec-9395-5042bf4e6abc",
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "bf124f1b-0167-479c-a91f-0d759f3f7e60",
      "name": "When clicking ‘Execute workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -496,
        1792
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "3adb0acd-3a61-451b-b93a-bebf60b9c213",
      "name": "Get many messages",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -256,
        1792
      ],
      "webhookId": "155bf8bb-cac7-4d9b-91aa-afdc0cbee0f2",
      "parameters": {
        "filters": {},
        "operation": "getAll"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "pVkuuYNT0KTxFcw0",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "12bd7049-1364-4339-af20-f50a765fda3b",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        544,
        2032
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"email_label\": \"business\"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "84d8bff0-4363-4fd1-b830-68c23952d7d0",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        288,
        2032
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "gpt-4o-mini"
        },
        "options": {}
      },
      "typeVersion": 1.2
    }
  ],
  "pinData": {},
  "connections": {
    "c2d50354-0427-41bf-89f8-fb931bfcaefa": {
      "main": [
        [
          {
            "node": "1300cdf3-5b22-45ee-af81-5d94723c10e3",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "cb4ea5c6-a638-425d-8b45-a8621a8a0e33",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "1db6e7ff-6770-4a66-8f05-3fae33b597f0",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "19233ac1-848d-4d3b-abca-4022f3ffa17c",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "99b15563-82da-4321-8ff0-60c21861f764",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "bfa96076-0b6f-4c39-8f75-e2e6312d2831",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "2f2937a3-22b8-4a71-920d-1393056eee25",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "3adb0acd-3a61-451b-b93a-bebf60b9c213": {
      "main": [
        [
          {
            "node": "1bd62e9b-2b93-4e79-99c0-f993a04079b3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "84d8bff0-4363-4fd1-b830-68c23952d7d0": {
      "ai_languageModel": [
        [
          {
            "node": "23ffde1f-a7ec-47e8-b4bb-6f1741e32074",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "1bd62e9b-2b93-4e79-99c0-f993a04079b3": {
      "main": [
        [
          {
            "node": "23ffde1f-a7ec-47e8-b4bb-6f1741e32074",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "23ffde1f-a7ec-47e8-b4bb-6f1741e32074": {
      "main": [
        [
          {
            "node": "c2d50354-0427-41bf-89f8-fb931bfcaefa",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "12bd7049-1364-4339-af20-f50a765fda3b": {
      "ai_outputParser": [
        [
          {
            "node": "23ffde1f-a7ec-47e8-b4bb-6f1741e32074",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "bf124f1b-0167-479c-a91f-0d759f3f7e60": {
      "main": [
        [
          {
            "node": "3adb0acd-3a61-451b-b93a-bebf60b9c213",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 个人效率, AI 摘要总结

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量18
分类2
节点类型8
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Arlin Perez

Arlin Perez

@arlindeveloper

👋 Hi! I'm Arlin - a QA Engineer with 2 years of experience and a background in Dart & Flutter for mobile app development. ⚙️ I'm passionate about automation and love building efficient workflows using n8n. 📬 Reach out for any help with custom workflows

外部链接
在 n8n.io 查看

分享此工作流

分类

分类: 34