{
  "id": "V9UTwynrya6oV15o",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Client Email Draft Generator",
  "tags": [],
  "nodes": [
    {
      "id": "a6ae1e66-84f5-4404-a45d-a45e9b4d45c9",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1168,
        -416
      ],
      "parameters": {
        "width": 320,
        "height": 1040,
        "content": "# Personalized Client Email Draft  Generator\n# How it works:\nThis workflow automates sending personalized client emails based on portfolio data and market trends. It retrieves client details from Google Sheets and fetches the latest market news using an API. The workflow filters new clients and validates important fields like email and portfolio value as a safeguard. It then prepares structured data and uses AI to generate a professional, tailored email. The email is formatted and sent through Gmail with a delay to control sending rate. Finally, the workflow updates the client status in Google Sheets to \u201cSent\u201d. \n\n# Setup Steps:\n## Data setup\nCreate a Google Sheet with required fields and connect it along with the market news API.\n\n## Processing & validation\nFilter new clients, attach market summary and validate required fields using IF node.\n\n## AI & email\nConnect AI for email generation and Gmail for sending, with a wait node for rate control.\n\n## Tracking\nUpdate client status to \u201cSent\u201d in Google Sheets after email delivery."
      },
      "typeVersion": 1
    },
    {
      "id": "8015dfdd-cf95-46c4-bd40-8ea302d3d58c",
      "name": "Fetch Market News",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -304,
        0
      ],
      "parameters": {
        "url": "https://www.alphavantage.co/query",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "function",
              "value": "NEWS_SENTIMENT"
            },
            {
              "name": "tickers",
              "value": "SPY"
            },
            {
              "name": "apikey",
              "value": ""
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "15e937ee-ac46-4921-92bb-affbc5dd12b3",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -768,
        -192
      ],
      "parameters": {
        "color": 7,
        "width": 688,
        "height": 400,
        "content": "## Fetch Client & Market Data\n\nRetrieves client records from Google Sheets, including portfolio, risk level and email. Simultaneously fetches the latest market news and sentiment from Alpha Vantage. Combines and processes this data to provide a summary for further personalized email generation."
      },
      "typeVersion": 1
    },
    {
      "id": "8db92e7a-7ad3-49de-a786-91b8c20c59c7",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        -192
      ],
      "parameters": {
        "color": 7,
        "width": 1008,
        "height": 384,
        "content": "## Process & Generate Email\n\nFilters new clients, attaches market summary and structures client data. then check the email is valid or not. if it is valid then Sends this information to the AI model, which generates a personalized, professional HTML email tailored to the client\u2019s profile and market trends."
      },
      "typeVersion": 1
    },
    {
      "id": "fc1611f7-96ec-4132-9e01-e71a8d110ac6",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1024,
        -176
      ],
      "parameters": {
        "color": 7,
        "width": 784,
        "height": 384,
        "content": "## Send & Track Email\n\nFormats AI-generated email into final fields (To, Subject, Body), sends it via Gmail and updates the client\u2019s status in Google Sheets to \u201cSent\u201d to prevent duplicate emails."
      },
      "typeVersion": 1
    },
    {
      "id": "b70c3667-8962-48fc-98a5-55d33f949283",
      "name": "Start Workflow",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -736,
        0
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "51314db2-3555-489e-a4c2-3bb09239faca",
      "name": "Get Client Data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -512,
        0
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T4hLgEOS5u4qi29QlnvUKxHeH5zzPhgW7Da6T3QXINg/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1T4hLgEOS5u4qi29QlnvUKxHeH5zzPhgW7Da6T3QXINg",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T4hLgEOS5u4qi29QlnvUKxHeH5zzPhgW7Da6T3QXINg/edit?usp=drivesdk",
          "cachedResultName": "ClientPortfolio"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "7a656b96-d7c9-4ba0-b69b-42b46fe288a0",
      "name": "Filter New Clients & Attach Market Summary",
      "type": "n8n-nodes-base.code",
      "position": [
        32,
        -16
      ],
      "parameters": {
        "jsCode": "// Get all rows from Google Sheets\nconst clients = $items(\"Get Client Data\");\n\n// Default professional fallback summary\nlet summary = \"Global markets are experiencing moderate fluctuations. Maintaining a diversified portfolio and aligning investments with your risk profile is recommended.\";\n\n//  real API summary\nif ($json && $json.feed && $json.feed.length > 0) {\n  summary = $json.feed[0].summary;\n}\n\n// Filter ONLY new clients \nconst newClients = clients.filter(client => {\n  const status = client.json.Status;\n\n  return (\n    status === undefined ||\n    status === null ||\n    status.toString().trim() === \"\"\n  );\n});\n\n// Stop workflow if no new client\nif (newClients.length === 0) {\n  return [];\n}\n\n// Take ONLY first new client\nconst client = newClients[0];\n\n// Return formatted data\nreturn [\n  {\n    json: {\n      ...client.json,\n      marketSummary: summary\n    }\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "62b8f18f-dc52-4d19-b577-6d494f3a737a",
      "name": "Prepare Client Data for AI",
      "type": "n8n-nodes-base.set",
      "position": [
        240,
        -16
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "8e289053-be3c-42ea-99d5-8012e71c1760",
              "name": "ClientID",
              "type": "string",
              "value": "={{$json.ClientID}}"
            },
            {
              "id": "df7e193b-adc4-4a9b-b72e-4587e4a03cd5",
              "name": "ClientName",
              "type": "string",
              "value": "={{ $json.ClientName }}"
            },
            {
              "id": "32afbc5d-ca57-47b3-98b6-33b6be31deb8",
              "name": "Email",
              "type": "string",
              "value": "={{ $json.Email }}"
            },
            {
              "id": "02c2cd4d-7d17-4f17-8038-05625ebf854b",
              "name": "PortfolioValue",
              "type": "string",
              "value": "={{ $json.PortfolioValue }}"
            },
            {
              "id": "17d4ab89-1dff-4947-9c81-864eb50f183d",
              "name": "RiskLevel",
              "type": "string",
              "value": "={{ $json.RiskLevel }}"
            },
            {
              "id": "0496e0a9-9f91-4f4d-ab06-d9170366fc8f",
              "name": "marketSummary",
              "type": "string",
              "value": "={{ $json.marketSummary }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "4eec0e8d-0935-47a3-b68a-4ab0cfb26c33",
      "name": "Validate Client Data",
      "type": "n8n-nodes-base.if",
      "position": [
        448,
        -16
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "0e4582f2-5463-4951-ba57-abd6b2a8791c",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "Email",
              "rightValue": ""
            },
            {
              "id": "9a0ef1c7-6be4-423c-9d7d-9f527b7ea8b8",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "PortfolioValue",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "db19ceea-ca7e-4221-bf03-aff353e1ff90",
      "name": "Generate Personalized Email",
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "position": [
        672,
        -32
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "models/gemini-2.5-flash",
          "cachedResultName": "models/gemini-2.5-flash"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "=You are a professional financial advisor. Generate a personalized, polite and professional email.\n\nClient:\nName: {{$json.ClientName}}\nEmail: {{$json.Email}}\nRisk Level: {{$json.RiskLevel}}\n\nPortfolio Value: {{$json.PortfolioValue}}\n\nMarket Summary:\n{{ $json.marketSummary }}\n\nInstructions:\n- Friendly greeting\n- Mention portfolio value clearly\n- Give advice based on risk level\n- Keep it short & professional\n- Use HTML (<p>, <ul>, <li>, <strong>)\n- DO NOT include ``` or extra text\n\nOutput only HTML."
            }
          ]
        },
        "builtInTools": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "46a575bf-fcaf-4101-bf13-08cb569480b1",
      "name": "Format Email",
      "type": "n8n-nodes-base.set",
      "position": [
        1104,
        -16
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "f035ef89-3a74-44dd-a657-e82faf9a8009",
              "name": "To",
              "type": "string",
              "value": "={{ $('Prepare Client Data for AI').item.json.Email }}"
            },
            {
              "id": "14a75d91-cc12-484d-9ac1-f67cdf99ae77",
              "name": "Subject",
              "type": "string",
              "value": "=Portfolio Update - {{ $('Filter New Clients & Attach Market Summary').item.json.ClientName }}"
            },
            {
              "id": "22f3624a-f294-47b7-829a-6690a8ad8544",
              "name": "Body",
              "type": "string",
              "value": "={{$json.content.parts[0].text}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "2d406785-79b0-4a8d-a1da-e7ae3d0962db",
      "name": "Delay Email Sending",
      "type": "n8n-nodes-base.wait",
      "position": [
        1296,
        -16
      ],
      "parameters": {},
      "typeVersion": 1.1
    },
    {
      "id": "2f0a2e01-942f-4d38-bf00-37d70c16f669",
      "name": "Send Email via Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1488,
        -16
      ],
      "parameters": {
        "sendTo": "={{$json.To}}",
        "message": "={{$json.Body}}",
        "options": {},
        "subject": "={{$json.Subject}}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "d2831235-bb71-4118-8380-0c0125b53074",
      "name": "Update Email Status in Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1664,
        -16
      ],
      "parameters": {
        "columns": {
          "value": {
            "Status": "Sent",
            "ClientID": "={{ $('Filter New Clients & Attach Market Summary').item.json.ClientID }}"
          },
          "schema": [
            {
              "id": "ClientID",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ClientID",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ClientName",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "ClientName",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "Email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "PortfolioName",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "PortfolioName",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "PortfolioValue",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "PortfolioValue",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "RiskLevel",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "RiskLevel",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "DateAdded",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "DateAdded",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Status",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "type": "number",
              "display": true,
              "removed": true,
              "readOnly": true,
              "required": false,
              "displayName": "row_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "ClientID"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T4hLgEOS5u4qi29QlnvUKxHeH5zzPhgW7Da6T3QXINg/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1T4hLgEOS5u4qi29QlnvUKxHeH5zzPhgW7Da6T3QXINg",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T4hLgEOS5u4qi29QlnvUKxHeH5zzPhgW7Da6T3QXINg/edit?usp=drivesdk",
          "cachedResultName": "ClientPortfolio"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    }
  ],
  "active": false,
  "settings": {
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "94667558-a263-4056-ac25-55a2d6e44839",
  "connections": {
    "Format Email": {
      "main": [
        [
          {
            "node": "Delay Email Sending",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Workflow": {
      "main": [
        [
          {
            "node": "Get Client Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Client Data": {
      "main": [
        [
          {
            "node": "Fetch Market News",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Market News": {
      "main": [
        [
          {
            "node": "Filter New Clients & Attach Market Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delay Email Sending": {
      "main": [
        [
          {
            "node": "Send Email via Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email via Gmail": {
      "main": [
        [
          {
            "node": "Update Email Status in Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Client Data": {
      "main": [
        [
          {
            "node": "Generate Personalized Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Client Data for AI": {
      "main": [
        [
          {
            "node": "Validate Client Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Personalized Email": {
      "main": [
        [
          {
            "node": "Format Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter New Clients & Attach Market Summary": {
      "main": [
        [
          {
            "node": "Prepare Client Data for AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}