{
  "id": "bRqE6ZhtVXke3fyU",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Predefined Personalized Email Workflow",
  "tags": [],
  "nodes": [
    {
      "id": "ac2e7948-6d2e-4e6c-9dea-bb536c284bd1",
      "name": "When clicking \u2018Execute workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -1376,
        -224
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "ce1155a0-de1c-49e3-ad62-332d80591259",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -704,
        -224
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "7ba408be-13d0-4b97-8953-b979cee79622",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        -32,
        -288
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2,
      "alwaysOutputData": false
    },
    {
      "id": "4d002700-b772-4134-b356-e321a667baeb",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "position": [
        640,
        -224
      ],
      "parameters": {
        "amount": 1
      },
      "typeVersion": 1.1
    },
    {
      "id": "75427d7b-8904-41f3-b0e5-214864fc5471",
      "name": "Parse Email Template from DB",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -480,
        -224
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 449875511,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1UGarQNCplIfKKPSInxZlIC72oosZ45ul5jAQjYfpWrs/edit#gid=449875511",
          "cachedResultName": "Email Template"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1UGarQNCplIfKKPSInxZlIC72oosZ45ul5jAQjYfpWrs",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1UGarQNCplIfKKPSInxZlIC72oosZ45ul5jAQjYfpWrs/edit?usp=drivesdk",
          "cachedResultName": "Leads & Email Template"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "07fc2e28-3552-4169-9e00-a40962587694",
      "name": "Pick a random template",
      "type": "n8n-nodes-base.code",
      "position": [
        -256,
        -224
      ],
      "parameters": {
        "jsCode": "// Get templates from previous node\nconst templates = items.map(item => item.json);\n\n// Check if templates exist\nif (!templates || templates.length === 0) {\n  throw new Error('No templates found');\n}\n\n// Pick a random template\nconst index = Math.floor(Math.random() * templates.length);\nconst template = templates[index];\n\n// Convert body to HTML\nconst bodyHtml = template.Body.replace(/\\n/g, '<br>');\n\n// Return in n8n-compatible format\nreturn [\n  {\n    json: {\n      subject: template.Subject,\n      body: bodyHtml\n    }\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "04898505-3810-454d-a874-ed0c0b7abc1f",
      "name": "Limit",
      "type": "n8n-nodes-base.limit",
      "position": [
        -928,
        -224
      ],
      "parameters": {
        "maxItems": 20
      },
      "typeVersion": 1
    },
    {
      "id": "9a0b6308-74f9-4b6e-aaa2-9b6171e7cef6",
      "name": "Get row(s) in sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -1152,
        -224
      ],
      "parameters": {
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultUrl": "",
          "cachedResultName": ""
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "51cb6840-cf3d-46c7-a155-c0a6c1571d7c",
      "name": "Send an email",
      "type": "n8n-nodes-base.sendGrid",
      "onError": "continueRegularOutput",
      "position": [
        416,
        -288
      ],
      "parameters": {
        "subject": "={{ $json.subject }}",
        "toEmail": "={{ $json.email }}",
        "resource": "mail",
        "contentType": "text/html",
        "contentValue": "={{ $json.body }}",
        "additionalFields": {
          "replyToEmail": ""
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "8e899e27-9aea-4eb5-9e66-433be11268e9",
      "name": "Fix Variables",
      "type": "n8n-nodes-base.set",
      "position": [
        192,
        -288
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "49a9d2c6-d9b3-4548-b00d-f74e2e999ebf",
              "name": "body",
              "type": "string",
              "value": "={{ \n$json.body\n    .replaceAll(\"[BusinessName]\", $json[\"Business Name\"]?.trim() || \"your restaurant\")\n    .replaceAll(\"[Category]\", $json[\"Category\"]?.trim() || \"restaurant\")\n    .replaceAll(\"[Location]\", $json[\"Location\"]?.trim() || \"your area\")\n    .replaceAll(\"[OtherCategories]\", $json[\"Other Categories\"]?.trim() || \"similar restaurants\")\n}}"
            },
            {
              "id": "25e35314-0fdb-40bc-9d81-a025eff70010",
              "name": "subject",
              "type": "string",
              "value": "={{ \n$json.subject\n    .replaceAll(\"[BusinessName]\", $json[\"Business Name\"]?.trim() || \"your restaurant\")\n    .replaceAll(\"[Category]\", $json[\"Category\"]?.trim() || \"restaurant\")\n    .replaceAll(\"[Location]\", $json[\"Location\"]?.trim() || \"your area\")\n    .replaceAll(\"[OtherCategories]\", $json[\"Other Categories\"]?.trim() || \"similar restaurants\")\n}}"
            },
            {
              "id": "eec4abbb-c30f-4aa3-99a3-6045b80eee68",
              "name": "email",
              "type": "string",
              "value": "={{ $json.Email }}"
            },
            {
              "id": "19db0a1f-6c61-4452-8bd0-0ddac9b1b919",
              "name": "ID",
              "type": "string",
              "value": "={{ $json.ID }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "8ebfe20b-4238-4e55-9066-1802265f7f76",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1424,
        -880
      ],
      "parameters": {
        "color": 3,
        "width": 784,
        "height": 608,
        "content": "## \ud83d\udce7 Email Workflow with SendGrid & Predefined Email Templates\n### \u2709\ufe0f Using Predefined Templates\nThis workflow provides a **scalable and reliable** solution for sending **personalized** emails to clients using **SendGrid**.\nIf you prefer not to use an AI agent to generate dynamic email content, this approach is ideal. Here\u2019s why:\n- \u26a1 Faster Execution\n- \ud83c\udfaf Consistency and Control \n- \ud83d\udcb0 Cost Efficiency\n\n\ud83d\udca1 You can always switch to an AI-based template generator later, depending on your workflow goals.\n\n### \ud83d\udd27 Setup Requirements\n- **SendGrid API**\n   -  Authenticate  also your domain and set up a sender identity\n- **Google Sheets API**\n   - Acts as your lightweight CRM\n   - The Sheet should have **two tabs**:\n      1. Leads\n      2. Email Template\n\n\nClick [Here](https://docs.google.com/spreadsheets/d/1UGarQNCplIfKKPSInxZlIC72oosZ45ul5jAQjYfpWrs/edit?usp=sharing) to download the Sample Google Sheets database used for this demo.\n\n### \ud83d\udcec Contact \n#### For Enquiries: buzanalytics@gmail.com\nThank you for exploring this workflow!\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d8ff919b-9064-46d4-aa88-b589ce134ed4",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        208,
        -816
      ],
      "parameters": {
        "color": 4,
        "width": 480,
        "height": 464,
        "content": "## \ud83e\udde9 Extend This Workflow Further\nThis workflow handles sending predefined, personalized emails to clients using **SendGrid**. To build a **complete email automation system**, you can integrate this with additional workflows and tools like **Gmail**, while ensuring proper setup of **SendGrid Webhook Events**.\n\nTo expand this workflow into a full email system, consider adding:\n- \ud83d\udd01 **Webhook** \u2013 Tracks delivery status and engagement (opens, clicks, etc.)\n- \ud83d\uddc2\ufe0f **Reply Updater** \u2013 Updates your database when replies are received\n- \ud83e\udde0 **Email Routing** \u2013 Uses AI to classify and label replies (e.g., Interested, Not Interested), forward Interested Replies to the appropriate department for further engagements\n- \ud83d\udcc6 **Follow-Up Emails** \u2013 Automates 5- and 10-day follow-up sequences\n\nThis modular design gives you flexibility to scale your outreach and track performance more effectively."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a0312591-5e32-4569-8767-492541575714",
  "connections": {
    "Wait": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Limit": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Fix Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fix Variables": {
      "main": [
        [
          {
            "node": "Send an email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send an email": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Parse Email Template from DB",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get row(s) in sheet": {
      "main": [
        [
          {
            "node": "Limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick a random template": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Parse Email Template from DB": {
      "main": [
        [
          {
            "node": "Pick a random template",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Get row(s) in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}