8
n8n 中文网amn8n.com

自动邮件标签器

高级

这是一个AI Summarization, Multimodal AI领域的自动化工作流,包含 27 个节点。主要使用 Set, Code, Gmail, Merge, Filter 等节点。 AI驱动的智能Gmail标签管理与Discord通知

前置要求
  • Google 账号和 Gmail API 凭证
  • Discord Bot Token 或 Webhook
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "p88sqsRiRSpYb4Da",
  "meta": {
    "instanceId": "c341ffddc74ebf27df927741c2e7cfe51645fd6ab7947c701ecbb8accd585228",
    "templateCredsSetupCompleted": true
  },
  "name": "自动邮件标签器",
  "tags": [
    {
      "id": "KXa95L999LpnSKKK",
      "name": "Gmail",
      "createdAt": "2025-08-30T14:29:58.156Z",
      "updatedAt": "2025-08-30T14:29:58.156Z"
    }
  ],
  "nodes": [
    {
      "id": "9bf78ec9-a9a9-4db5-8cb0-4eb6cb6cf3b5",
      "name": "基础LLM链",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        608,
        32
      ],
      "parameters": {
        "text": "=Provide a summary of the email in JSON format. You will provide a (1) Id, (2) summary, (3) extract the relevant entity such as person, organization, (4) provide a list of up to 5 labels from the list provided in the \"Labels List\" below to categorize the email. Or if none are appropriate, please come up with new labels (up to 5).\n\nHere is the email content:\nFrom: {{ $json.From }}\nSubject: {{ $json.Subject }}\nContent: {{ $json.snippet }}\nLabel List: {{ $json.existing_labels.map(label => label.name) }}\n\nHere is an example output:\n{\n    \"summary\": \"This is an email advertising a new designer collection of goods from Haider Ackermann on Mr. Porter. It is Haider Ackermann's debut collection.\",\n    \"people\": \"Mr. Porter\",\n    \"labels\":\n    [\n        \"Advertisement\",\n        \"Designer Label\",\n        \"Clothes\",\n        \"Online Shopping\"\n    ]\n}\n\nYour Output:\n",
        "batching": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "retryOnFail": true,
      "typeVersion": 1.7,
      "waitBetweenTries": 100
    },
    {
      "id": "0216c4d9-041b-41d0-ae1d-6965399bfc61",
      "name": "Gmail 触发器",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -432,
        304
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "CKyTztL6Pu4sMVhC",
          "name": "Gmail account"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "d8d3a318-0477-4e5d-8e1d-2d7ab073f217",
      "name": "结构化输出解析器",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        736,
        320
      ],
      "parameters": {
        "autoFix": true,
        "jsonSchemaExample": "{\n    \"messageId\": \"\",\n    \"summary\": \"This email higlights the convenience of using an Alexa-enabled smart thermostat for temperature control and mentions Alexa's ability to assist with shopping lists, specifically laundry detergent. It also references a 'Weekly Spotlight' feature related to product sorting.\",\n    \"people\": \"Alexa\",\n    \"labels\":\n    [\n        \"Smart Home\",\n        \"Voice Assistant\",\n        \"Thermostat\",\n        \"Shopping\",\n        \"Productivity\"\n    ]\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "99594eaa-8459-4cb9-905e-399549cb8b72",
      "name": "创建标签",
      "type": "n8n-nodes-base.gmail",
      "onError": "continueRegularOutput",
      "position": [
        1472,
        400
      ],
      "webhookId": "365633e2-f709-4412-9176-bf921b3f717d",
      "parameters": {
        "name": "={{ $json[\"output.labels\"] }}",
        "options": {},
        "resource": "label",
        "operation": "create"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "CKyTztL6Pu4sMVhC",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "c2beef7d-1352-40ac-868c-be730a463d69",
      "name": "代码",
      "type": "n8n-nodes-base.code",
      "position": [
        1216,
        320
      ],
      "parameters": {
        "jsCode": "var item = $input.first();\nvar new_labels = new Set(item.json.output.labels) // new labels defined by LLM\nvar old_labels = new Set(item.json.existing_labels.map(label => label.name)) // message labels\nvar label_array = item.json.existing_labels // users label dictionary\nvar labels_to_create = new Set();\nvar existing_labels = new Set();\n\n// identify new labels to create\nnew_labels.forEach(label => {\n  if (old_labels.has(label)) {\n    console.log(\"dupe found: \" + label)\n    existing_labels.add(label)\n  } else {\n    console.log(\"unique found: \" + label)\n    labels_to_create.add(label)\n  }\n});\n\n// each new label to create ends up in output.labels array\nitem.json.output.labels = []\nlabels_to_create.forEach(label => item.json.output.labels.push(label))\n\n// existing labels end up in output.old_labels array\n// for each old label, look up the label id\nitem.json.output.old_labels = []\nexisting_labels.forEach(label => {\n  var label_obj = label_array.find(l => l.name == label)\n  if (label_obj) {\n    console.log(\"replacing \" + label + \" with id \" + label_obj.id)\n    item.json.output.old_labels.push(label_obj.id)\n    item.json.output.added_label_names.push(label_obj.name)\n  }\n})\n\nitem.json.output.messageId = item.json.id;\n\nreturn $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "c8153181-5a9a-4ec7-a61b-f559d34e0d3f",
      "name": "合并2",
      "type": "n8n-nodes-base.merge",
      "position": [
        1040,
        48
      ],
      "parameters": {
        "mode": "combineBySql",
        "query": "SELECT * FROM input1 LEFT JOIN input2",
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "3ab1de7a-2dc1-4199-98f5-1e0e0cd8fcc3",
      "name": "拆分输出",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1344,
        400
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "output.labels"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "758ce7fa-b66a-4018-b576-e754cff5e5f9",
      "name": "合并3",
      "type": "n8n-nodes-base.merge",
      "position": [
        1888,
        128
      ],
      "parameters": {
        "mode": "combineBySql",
        "query": "SELECT * FROM input1 LEFT JOIN input2",
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "7be3f984-3cf7-4868-8148-8e3a012028e3",
      "name": "为邮件添加标签",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2208,
        16
      ],
      "webhookId": "8a2d4934-7637-40d0-84a3-987d7fe688e3",
      "parameters": {
        "labelIds": "={{ $json.labels_to_add }}",
        "messageId": "={{ $json.output.messageId }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "CKyTztL6Pu4sMVhC",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "97c77c1d-0857-46cf-ba2a-4e38b0b48e4f",
      "name": "发送消息",
      "type": "n8n-nodes-base.discord",
      "position": [
        2848,
        304
      ],
      "webhookId": "88a8856f-4974-45a6-b941-e5ddb5da4302",
      "parameters": {
        "content": "={{ $json.text.split(\"MESSAGE STARTS HERE\").last() }}",
        "guildId": {
          "__rl": true,
          "mode": "list",
          "value": "1411315313664069778",
          "cachedResultUrl": "https://discord.com/channels/1411315313664069778",
          "cachedResultName": "terribletap's server"
        },
        "options": {},
        "resource": "message",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "1411315314624430103",
          "cachedResultUrl": "https://discord.com/channels/1411315313664069778/1411315314624430103",
          "cachedResultName": "general"
        }
      },
      "credentials": {
        "discordBotApi": {
          "id": "EllllgZJlaKOTtPf",
          "name": "Discord Bot account"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "4e637594-2570-471e-89a3-129c990c70f7",
      "name": "获取多个标签",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -80,
        400
      ],
      "webhookId": "59d1eb15-6c4c-4963-9eee-5bca6e0a3364",
      "parameters": {
        "resource": "label",
        "returnAll": true
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "CKyTztL6Pu4sMVhC",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "0652a435-b021-46bb-911e-8d66ad4ab262",
      "name": "筛选器",
      "type": "n8n-nodes-base.filter",
      "position": [
        80,
        400
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "652bbdfc-8258-4acf-b8cc-38f88a69d87c",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              },
              "leftValue": "={{ $json.id }}",
              "rightValue": "Label"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "fb63935c-5a27-4e9e-96b8-5d73301890bf",
      "name": "聚合1",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        224,
        400
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "existing_labels"
      },
      "typeVersion": 1
    },
    {
      "id": "1173c4fc-0037-4c9f-81cc-0e52cd4caa2a",
      "name": "聚合2",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1600,
        400
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "created_labels"
      },
      "typeVersion": 1
    },
    {
      "id": "19f5848c-d7e6-45c1-8ed9-c5a6aa6c9f5e",
      "name": "编辑字段3",
      "type": "n8n-nodes-base.set",
      "position": [
        2064,
        16
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "30b6e814-f461-4957-bee3-433acf9df86e",
              "name": "output.messageId",
              "type": "string",
              "value": "={{ $json.output.messageId }}"
            },
            {
              "id": "cc6b96ee-7eed-463f-b324-511395366671",
              "name": "labels_to_add",
              "type": "array",
              "value": "={{ $json.output.old_labels.concat($json.created_labels.map(item => item.id))}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a537fe85-ff6b-44f6-8676-ec118a451f57",
      "name": "遍历项目",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -288,
        304
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "23a89c87-5b44-4a1d-8750-2b817984e33a",
      "name": "OpenAI Chat 10001",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        608,
        736
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "/home/rocketegg/.cache/huggingface/hub/models--unsloth--gpt-oss-20b-GGUF/snapshots/c6cedd4259adbfe7e4d4d983a0400bf4cc38e7db/gpt-oss-20b-F16.gguf",
          "cachedResultName": "/home/rocketegg/.cache/huggingface/hub/models--unsloth--gpt-oss-20b-GGUF/snapshots/c6cedd4259adbfe7e4d4d983a0400bf4cc38e7db/gpt-oss-20b-F16.gguf"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "LKEGGdxiA0Bf5HVT",
          "name": "OpenAi localhost 10001"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "3a3c249c-b2a5-4b26-8565-345a78bbe347",
      "name": "OpenAI Chat 10000",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -688,
        -688
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "/home/rocketegg/.cache/huggingface/hub/models--unsloth--GLM-4.5-Air-GGUF/snapshots/a5133889a6e29d42a1e71784b2ae8514fb28156f/Q4_K_S/GLM-4.5-Air-Q4_K_S-00001-of-00002.gguf",
          "cachedResultName": "/home/rocketegg/.cache/huggingface/hub/models--unsloth--GLM-4.5-Air-GGUF/snapshots/a5133889a6e29d42a1e71784b2ae8514fb28156f/Q4_K_S/GLM-4.5-Air-Q4_K_S-00001-of-00002.gguf"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "hXmYMFnRaBrbl3No",
          "name": "OpenAi localhost 10000"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "7514e196-94bf-462e-95c4-8698978e8b9e",
      "name": "基础 LLM 链1",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        2592,
        304
      ],
      "parameters": {
        "text": "=INSTRUCTIONS: Rewrite the message below with fun emojis per the instructions below. PREPEND the message with \"MESSAGE STARTS HERE\".\n\nI'm sending a fun discord message so I need the following summary in markdown with emojis appropriate for the content (e.g. financial emails with financial emojis, advertisements with advertisement emojis, and depending on what they are selling, the appropriate emoji, etc).\n\n---\nCONTENT OF THE MESSAGE:\n\nHello! I finished labeling an email.\n\n**From:** {{ $json.From }}\n**Subject:** {{ $json.Subject }}\n**Summary:** {{ $json.output.summary }}\n**Entity:** {{ $json.output.people }}\n**Date:** {{ new Date(parseInt($json.internalDate)) }}\n**Id:** {{ $json.id }}\n\n**Labels Added:**\n{{ $json.created_labels.map(l => `- ${l.name}`).join('\\n> ') }}\n\n**Appended Labels:**\n{{ $json.output.added_label_names.map(name => `- ${name}`).join('\\n> ') }}\n\n--- OUTPUT:\nMESSAGE STARTS HERE:\n\n",
        "batching": {},
        "promptType": "define"
      },
      "retryOnFail": true,
      "typeVersion": 1.7
    },
    {
      "id": "dd821c19-35ed-4295-89d5-d3202ea7258a",
      "name": "合并",
      "type": "n8n-nodes-base.merge",
      "position": [
        2464,
        304
      ],
      "parameters": {
        "mode": "combineBySql",
        "query": "SELECT * FROM input1 LEFT JOIN input2 ",
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "e05d0e9c-161e-4082-a09b-cada4ffcca4c",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1808,
        -48
      ],
      "parameters": {
        "width": 608,
        "height": 288,
        "content": "### 使用新标签和现有标签标记消息"
      },
      "typeVersion": 1
    },
    {
      "id": "4091dfff-76e6-44a9-bdf4-f333d1b721e6",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2432,
        240
      ],
      "parameters": {
        "color": 4,
        "width": 608,
        "height": 288,
        "content": "### 重写并将消息通知发送到discord"
      },
      "typeVersion": 1
    },
    {
      "id": "25e5a3f4-b5bc-465a-9701-4fc4ee59c02d",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1184,
        240
      ],
      "parameters": {
        "color": 5,
        "width": 608,
        "height": 288,
        "content": "### 如果需要,创建额外的gmail标签。"
      },
      "typeVersion": 1
    },
    {
      "id": "f7cb8412-72b5-4e34-8118-8c7b6a99d64a",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -128,
        240
      ],
      "parameters": {
        "color": 3,
        "width": 608,
        "height": 288,
        "content": "### 查找现有的Gmail标签作为LLM选择的选项。"
      },
      "typeVersion": 1
    },
    {
      "id": "48d5b5a3-19b9-4b6f-8072-5cd4c51e9441",
      "name": "将标签合并到列表中(供LLM选择)",
      "type": "n8n-nodes-base.merge",
      "position": [
        368,
        288
      ],
      "parameters": {
        "mode": "combineBySql",
        "query": "SELECT * FROM input1 LEFT JOIN input2 ",
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "20de5d24-adc4-464c-94c5-8da4729e3682",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        544,
        -48
      ],
      "parameters": {
        "color": 6,
        "width": 608,
        "height": 288,
        "content": "### LLM选择标签或定义新标签"
      },
      "typeVersion": 1
    },
    {
      "id": "4c12d9c2-2f47-4c1b-92b1-7164c038efe8",
      "name": "添加旧标签 []",
      "type": "n8n-nodes-base.set",
      "position": [
        896,
        32
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "b4b52de9-a671-4d06-baaa-241f3ec4dea8",
              "name": "output",
              "type": "object",
              "value": "={{ $json.output }}"
            },
            {
              "id": "a27b466e-acad-4e8a-b69f-846d3220595f",
              "name": "output.old_labels",
              "type": "array",
              "value": "[]"
            },
            {
              "id": "25481eb7-7e0e-400b-b80a-f0b460e85c42",
              "name": "output.added_label_names",
              "type": "array",
              "value": "[]"
            }
          ]
        },
        "duplicateItem": true
      },
      "typeVersion": 3.4
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v1",
    "saveExecutionProgress": true
  },
  "versionId": "df9e9f97-93f7-4e3e-9092-e85ffda7dcd6",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Basic LLM Chain1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter": {
      "main": [
        [
          {
            "node": "Aggregate1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge2": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge3": {
      "main": [
        [
          {
            "node": "Edit Fields3",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Create a label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate1": {
      "main": [
        [
          {
            "node": "Merge labels into list (for llm to pick)",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Aggregate2": {
      "main": [
        [
          {
            "node": "Merge3",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Edit Fields3": {
      "main": [
        [
          {
            "node": "Add label to message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a label": {
      "main": [
        [
          {
            "node": "Aggregate2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a message": {
      "main": [
        []
      ]
    },
    "Basic LLM Chain": {
      "main": [
        [
          {
            "node": "Add old_labels []",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many labels": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Get many labels",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge labels into list (for llm to pick)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Basic LLM Chain1": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add old_labels []": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat 10000": {
      "ai_languageModel": [
        []
      ]
    },
    "OpenAI Chat 10001": {
      "ai_languageModel": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Structured Output Parser",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Basic LLM Chain1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Add label to message": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Merge labels into list (for llm to pick)": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 1
          },
          {
            "node": "Basic LLM Chain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - AI 摘要总结, 多模态 AI

需要付费吗?

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

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

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

外部链接
在 n8n.io 查看

分享此工作流