{
  "name": "邮件营销自动化工作流",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "weeksInterval": 1,
              "triggerAtDay": [2],
              "triggerAtHour": 10
            }
          ]
        }
      },
      "name": "每周定时触发",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [0, 300]
    },
    {
      "parameters": {
        "url": "YOUR_CRM_OR_FEISHU_BITABLE_API_URL",
        "method": "GET",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer YOUR_CRM_API_TOKEN" }
          ]
        },
        "options": {}
      },
      "name": "拉取订阅者",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "jsCode": "const subs = $json.items || $json.records || [];\nconst groups = {};\nfor (const s of subs) {\n  const f = s.fields || s;\n  const tag = f['标签'] || f.tag || f.segment || 'default';\n  if (!groups[tag]) groups[tag] = [];\n  groups[tag].push({ email: f['邮箱'] || f.email, name: f['姓名'] || f.name || '' });\n}\nreturn Object.entries(groups).map(([tag, list]) => ({\n  json: { segment: tag, subscribers: list, count: list.length }\n}));"
      },
      "name": "按标签行为分群",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [480, 300]
    },
    {
      "parameters": {
        "url": "https://api.deepseek.com/v1/chat/completions",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer YOUR_LLM_API_KEY" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"model\":\"deepseek-chat\",\"messages\":[{\"role\":\"system\",\"content\":\"你是邮件营销文案专家。按订阅者分群生成个性化邮件，输出 A/B 两个变体，每个含 subject(主题行,≤50字) 与 body(正文,纯文本,≤300字,含 [姓名] 占位与退订链接 [[unsubscribe]])。仅输出 JSON：subjectA,bodyA,subjectB,bodyB。不编造用户敏感属性(种族/健康/收入等)，只依据分群标签与公开行为。\"},{\"role\":\"user\",\"content\":\"={{$json.segment}} 分群，共 {{$json.count}} 人。请生成个性化邮件 A/B 变体。\"}]}",
        "options": {}
      },
      "name": "LLM生成邮件",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [720, 300]
    },
    {
      "parameters": {
        "jsCode": "const resp = JSON.parse($json.choices[0].message.content);\nconst seg = $('按标签行为分群').item.json;\nconst half = Math.floor(seg.count / 2);\nreturn [\n  { json: { variant: 'A', subject: resp.subjectA, body: resp.bodyA, segment: seg.segment, recipients: seg.subscribers.slice(0, half), total: seg.count } },\n  { json: { variant: 'B', subject: resp.subjectB, body: resp.bodyB, segment: seg.segment, recipients: seg.subscribers.slice(half), total: seg.count } }\n];"
      },
      "name": "解析LLM结果",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [960, 300]
    },
    {
      "parameters": {
        "fromEmail": "newsletter@yourdomain.com",
        "toEmail": "={{$json.recipients.map(r=>r.email).filter(Boolean).join(',')}}",
        "subject": "={{$json.subject}}",
        "emailFormat": "text",
        "text": "={{$json.body}}",
        "options": {}
      },
      "name": "发送邮件",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [1200, 300]
    },
    {
      "parameters": {
        "url": "YOUR_METRICS_API_URL",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer YOUR_METRICS_API_TOKEN" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"variant\":\"={{$json.variant}}\",\"segment\":\"={{$json.segment}}\",\"sent\":\"={{$json.recipients.length}}\",\"timestamp\":\"={{$now}}\"}",
        "options": {}
      },
      "name": "回写发送记录",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [1440, 300]
    },
    {
      "parameters": {
        "httpMethod": "GET",
        "path": "email-track",
        "options": {}
      },
      "name": "追踪打开点击",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [0, 600]
    },
    {
      "parameters": {
        "url": "YOUR_METRICS_API_URL",
        "method": "GET",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer YOUR_METRICS_API_TOKEN" }
          ]
        },
        "options": {}
      },
      "name": "拉取发送打开计数",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [240, 600]
    },
    {
      "parameters": {
        "jsCode": "const q = $('追踪打开点击').item.json.query || {};\nconst variant = q.variant || 'A';\nconst data = $json;\nconst sent = data.sent && data.sent[variant] !== undefined ? data.sent[variant] : 0;\nconst opened = data.opened && data.opened[variant] !== undefined ? data.opened[variant] : 0;\nconst openRate = sent > 0 ? Math.round((opened / sent) * 1000) / 10 : 0;\nreturn { json: { variant, sent, opened, openRate, segment: q.segment || '' } };"
      },
      "name": "计算打开率",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [480, 600]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json.openRate}}",
              "value2": 20,
              "operation": "smaller"
            },
            {
              "value1": "={{$json.sent}}",
              "value2": 50,
              "operation": "largerEqual"
            }
          ]
        }
      },
      "name": "判断打开率",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [720, 600]
    },
    {
      "parameters": {
        "url": "https://api.deepseek.com/v1/chat/completions",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer YOUR_LLM_API_KEY" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"model\":\"deepseek-chat\",\"messages\":[{\"role\":\"system\",\"content\":\"你是邮件营销文案专家。当前变体打开率低于阈值，请基于分群重新生成 3 个候选主题行与对应正文，强调不同切入角度(利益/紧迫/好奇)。仅输出 JSON：candidates:[{subject,body}]。保留 [[unsubscribe]] 退订链接，不推断用户敏感属性。\"},{\"role\":\"user\",\"content\":\"={{$json.segment}} 分群，变体 {{$json.variant}} 当前打开率 {{$json.openRate}}%，已发 {{$json.sent}} 封。请重生成文案。\"}]}",
        "options": {}
      },
      "name": "触发换文案重测",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [960, 600]
    }
  ],
  "connections": {
    "每周定时触发": {
      "main": [
        [ { "node": "拉取订阅者", "type": "main", "index": 0 } ]
      ]
    },
    "拉取订阅者": {
      "main": [
        [ { "node": "按标签行为分群", "type": "main", "index": 0 } ]
      ]
    },
    "按标签行为分群": {
      "main": [
        [ { "node": "LLM生成邮件", "type": "main", "index": 0 } ]
      ]
    },
    "LLM生成邮件": {
      "main": [
        [ { "node": "解析LLM结果", "type": "main", "index": 0 } ]
      ]
    },
    "解析LLM结果": {
      "main": [
        [ { "node": "发送邮件", "type": "main", "index": 0 } ]
      ]
    },
    "发送邮件": {
      "main": [
        [ { "node": "回写发送记录", "type": "main", "index": 0 } ]
      ]
    },
    "追踪打开点击": {
      "main": [
        [ { "node": "拉取发送打开计数", "type": "main", "index": 0 } ]
      ]
    },
    "拉取发送打开计数": {
      "main": [
        [ { "node": "计算打开率", "type": "main", "index": 0 } ]
      ]
    },
    "计算打开率": {
      "main": [
        [ { "node": "判断打开率", "type": "main", "index": 0 } ]
      ]
    },
    "判断打开率": {
      "main": [
        [ { "node": "触发换文案重测", "type": "main", "index": 0 } ],
        []
      ]
    }
  }
}
