8
n8n 中文网amn8n.com

将Gmail邮件详情存储到MySQL数据库

初级

这是一个Ticket Management领域的自动化工作流,包含 5 个节点。主要使用 Code, MySql, GmailTrigger 等节点。 将Gmail邮件详情存储到MySQL数据库

前置要求
  • MySQL 数据库连接信息
  • Google 账号和 Gmail API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "d6e2f2f655b1125bbcac14a4cac6d2e46c7a150e927f85fc96fdca1a6dc39e0e",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "3a905012-05c9-4799-93e2-3d3faee8dec3",
      "name": "接收邮件",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        0,
        -120
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "YVZiLPppXZw84rIU",
          "name": "Gmail account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "2b4de6f5-5fd4-449b-a708-b883a9606f29",
      "name": "获取客户姓名和邮箱",
      "type": "n8n-nodes-base.code",
      "position": [
        220,
        -120
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Extract Sender Info\nlet sender_email = $json.From.trim();\nlet sender_name = null;\n\nif (sender_email.includes('<')) {\n  sender_name = sender_email.split('<')[0].trim();\n  sender_email = sender_email.split('<')[1].replace('>', '').trim();\n}\n\n// Extract Recipient Info\nlet recipient_email = $json.To.trim();\nlet recipient_name = null;\n\nif (recipient_email.includes('<')) {\n  recipient_name = recipient_email.split('<')[0].trim();\n  recipient_email = recipient_email.split('<')[1].replace('>', '').trim();\n}\n\nreturn {\n  \"sender_name\": sender_name,\n  \"sender_email\": sender_email,\n  \"recipient_name\": recipient_name,\n  \"recipient_email\": recipient_email\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "05206943-79b6-4bc6-a04b-218db9de7000",
      "name": "在 MySQL 中插入新客户",
      "type": "n8n-nodes-base.mySql",
      "onError": "continueRegularOutput",
      "position": [
        440,
        -120
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "contacts",
          "cachedResultName": "contacts"
        },
        "options": {},
        "dataMode": "defineBelow",
        "operation": "upsert",
        "valuesToSend": {
          "values": [
            {
              "value": "={{ $('Receive Email').item.json.threadId }}",
              "column": "threadId"
            },
            {
              "value": "={{ $json.sender_name }}",
              "column": "sender_name"
            },
            {
              "value": "={{ $json.sender_email }}",
              "column": "sender_email"
            },
            {
              "value": "={{ $json.recipient_name }}",
              "column": "recipient_name"
            },
            {
              "value": "={{ $json.recipient_email }}",
              "column": "recipient_email"
            },
            {
              "value": "={{ $('Receive Email').item.json.Subject }}",
              "column": "subject"
            },
            {
              "value": "={{ $('Receive Email').item.json.snippet }}",
              "column": "snippet"
            }
          ]
        },
        "valueToMatchOn": "={{ $('Receive Email').item.json.id }}",
        "columnToMatchOn": "messageId"
      },
      "credentials": {
        "mySql": {
          "id": "MYdqb2i2fz5O21ui",
          "name": "BT Operations Database"
        }
      },
      "typeVersion": 2.4,
      "alwaysOutputData": true
    },
    {
      "id": "ce56a64f-0311-4e54-81a9-2d7291a9da10",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -320,
        -840
      ],
      "parameters": {
        "height": 1340,
        "content": "## 此工作流处理在 Gmail 中接收的邮件,并将每封邮件的详细信息保存到 MySQL 数据库。"
      },
      "typeVersion": 1
    },
    {
      "id": "9b480e55-00f1-4cfb-af90-8038b5034679",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        380,
        -540
      ],
      "parameters": {
        "width": 300,
        "height": 620,
        "content": "## 请先设置此节点"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Receive Email": {
      "main": [
        [
          {
            "node": "Get Client Name and Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Client Name and Email": {
      "main": [
        [
          {
            "node": "Insert New Client in MySQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert New Client in MySQL": {
      "main": [
        []
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

初级 - 工单管理

需要付费吗?

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

工作流信息
难度等级
初级
节点数量5
分类1
节点类型4
难度说明

适合 n8n 新手,包含 1-5 个节点的简单工作流

外部链接
在 n8n.io 查看

分享此工作流