8
n8n 中文网amn8n.com

使用Gemini 2.5 Pro AI分类自动标记Gmail邮件

中级

这是一个Miscellaneous, AI Summarization, Multimodal AI领域的自动化工作流,包含 9 个节点。主要使用 Set, Code, Gmail, Aggregate, GmailTrigger 等节点。 使用Gemini 2.5 Pro AI分类自动标记Gmail邮件

前置要求
  • Google 账号和 Gmail API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "58a9e858b82757a75629d435ec0da2448ad5d9e906a9ce3b7dcbb64c90fda9fe",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "f5583750-3712-45ab-b80d-e6ef59e1e6dd",
      "name": "Gmail 触发器",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -384,
        -80
      ],
      "parameters": {
        "filters": {
          "readStatus": "unread"
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "DMM81jCjgMwmkeIf",
          "name": "N8N Automations Google Credential"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "39fb0933-4c78-4209-aa47-4bc2b76aac83",
      "name": "向模型发送消息",
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "position": [
        416,
        -80
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "models/gemini-2.5-pro",
          "cachedResultName": "models/gemini-2.5-pro"
        },
        "options": {
          "systemMessage": "=You are an email classifier assistant.\nOnly respond with the most suitable existing Gmail label for this email from the following list: {{ $json.labelList }}.\nNever invent a label—not in the list. Respond with multiple matching label names which are seperated with single comma's make sure every labels are seperated using a comma, and nothing else."
        },
        "messages": {
          "values": [
            {
              "content": "=Below is a new email.\nSubject: {{ $('Gmail Trigger').item.json.Subject }}\nBody: {{ $('Gmail Trigger').item.json.snippet }}\nWhich Gmail label does this email belong to?"
            }
          ]
        }
      },
      "credentials": {
        "googlePalmApi": {
          "id": "GrbWNcmNxQFQaJje",
          "name": "Gemini API n8n-automation"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4a2fcba6-5e5e-4fea-8a87-94f774a18e54",
      "name": "为邮件添加标签",
      "type": "n8n-nodes-base.gmail",
      "position": [
        976,
        -80
      ],
      "webhookId": "fe713b17-a055-40d5-9e3d-1ef22940d963",
      "parameters": {
        "labelIds": "={{ $json.labelIds }}",
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "DMM81jCjgMwmkeIf",
          "name": "N8N Automations Google Credential"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "4838c10e-bda2-4d72-ad5e-f50090ec4f9d",
      "name": "获取多个标签",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -176,
        -80
      ],
      "webhookId": "4d911551-faee-4329-a67a-9a2ee147088a",
      "parameters": {
        "resource": "label",
        "returnAll": true
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "DMM81jCjgMwmkeIf",
          "name": "N8N Automations Google Credential"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "310a8233-f123-473b-90ad-0df5417c66e1",
      "name": "编辑字段",
      "type": "n8n-nodes-base.set",
      "position": [
        240,
        -80
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "9628092d-b702-4543-a3ab-e225641f4b21",
              "name": "labelList",
              "type": "string",
              "value": "={{ $json.labels.map(item => item.name).join(', ') }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "3c2c6444-2a70-414e-92fc-df97b3d2ca27",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        32,
        -80
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "labels"
      },
      "typeVersion": 1
    },
    {
      "id": "5d4dea5a-6117-416a-bbf3-5c9595e479dd",
      "name": "代码",
      "type": "n8n-nodes-base.code",
      "position": [
        768,
        -80
      ],
      "parameters": {
        "jsCode": "// Get all input items (your AI output should be in items[0], and label map in items[0].json.labels)\nconst items = $input.all();\n\n// 1. Parse comma-separated label names returned by the AI node\nlet resultLabels = items[0].json.content.parts[0].text\n  .split(\",\")\n  .map((label) => label.trim())\n  .filter((label) => label.length > 0);\n\n// 2. Get the label mapping array from aggregate node (should be in items[0].json.labels)\nconst labelObjects = $('Aggregate').first().json.labels || [];\nconsole.log(\"LABEL\", labelObjects);\n\n// 3. Map label names to IDs using the mapping array\nconst labelIds = resultLabels\n  .map((lName) => {\n    const match = labelObjects.find((obj) => obj.name === lName);\n    return match ? match.id : null;\n  })\n  .filter(Boolean); // remove nulls\n\n// 4. Output as n8n expects\nreturn [\n  {\n    json: {\n      labelIds,\n    },\n  },\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "2691c250-40e4-41b8-9b5e-f553cd842a2f",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1008,
        -256
      ],
      "parameters": {
        "width": 496,
        "height": 480,
        "content": "## 📨 AI Gmail 自动标签器:工作原理"
      },
      "typeVersion": 1
    },
    {
      "id": "0409c111-441c-4ff5-99e6-39098ca775a8",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1248,
        -192
      ],
      "parameters": {
        "width": 464,
        "height": 336,
        "content": "## 🛠️ 使用方法"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Add label to message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Get many labels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many labels": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 杂项, AI 摘要总结, 多模态 AI

需要付费吗?

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

工作流信息
难度等级
中级
节点数量9
分类3
节点类型7
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

外部链接
在 n8n.io 查看

分享此工作流