{
  "name": "Google Sheets Bulk Email Campaign Sender",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "trigger-campaign",
        "options": {}
      },
      "id": "sbem0001-0001-4000-8000-000000000001",
      "name": "Campaign Trigger Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": { "__rl": true, "mode": "id", "value": "={{ $json.body.sheet_id || $env.SUBSCRIBERS_SHEET_ID }}" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Subscribers" },
        "filters": {
          "conditions": [
            {
              "field": "opted_in",
              "type": "string",
              "operation": "equal",
              "value": "true"
            }
          ]
        },
        "options": { "headerRow": 1 }
      },
      "id": "sbem0001-0002-4000-8000-000000000002",
      "name": "Read Opted-In Subscribers",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [460, 300]
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "leftValue": "={{ $json.email }}",
              "rightValue": "",
              "operator": { "type": "string", "operation": "notEmpty" }
            },
            {
              "leftValue": "={{ $json.unsubscribed }}",
              "rightValue": "true",
              "operator": { "type": "string", "operation": "notEqual" }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "sbem0001-0003-4000-8000-000000000003",
      "name": "Filter Valid Recipients",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 1,
      "position": [680, 300]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.email }}",
        "subject": "={{ $('Campaign Trigger Webhook').item.json.body.subject }}",
        "emailType": "html",
        "message": "={{ $('Campaign Trigger Webhook').item.json.body.body.replace('{{name}}', $json.name || 'there') }}",
        "options": {}
      },
      "id": "sbem0001-0004-4000-8000-000000000004",
      "name": "Send Campaign Email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [900, 300]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": { "__rl": true, "mode": "id", "value": "={{ $env.SUBSCRIBERS_SHEET_ID }}" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Subscribers" },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "email": "={{ $('Filter Valid Recipients').item.json.email }}",
            "last_emailed": "={{ $now.toISO() }}"
          },
          "matchingColumns": ["email"]
        },
        "options": {}
      },
      "id": "sbem0001-0005-4000-8000-000000000005",
      "name": "Update Last Emailed",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1120, 300]
    }
  ],
  "connections": {
    "Campaign Trigger Webhook": {
      "main": [[{ "node": "Read Opted-In Subscribers", "type": "main", "index": 0 }]]
    },
    "Read Opted-In Subscribers": {
      "main": [[{ "node": "Filter Valid Recipients", "type": "main", "index": 0 }]]
    },
    "Filter Valid Recipients": {
      "main": [[{ "node": "Send Campaign Email", "type": "main", "index": 0 }]]
    },
    "Send Campaign Email": {
      "main": [[{ "node": "Update Last Emailed", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "meta": {},
  "pinData": {}
}
