{
  "id": "Nj73lhp3xoOM5vJR",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI-Powered Email Classification and Auto-Reply System with Gmail and GPT",
  "tags": [],
  "nodes": [
    {
      "id": "1c25bce1-dce3-408f-8c17-f4e2cde60f26",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        0,
        80
      ],
      "parameters": {
        "simple": false,
        "filters": {
          "readStatus": "unread"
        },
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e39fd2c4-76b2-4cb9-816e-b0e146b81be3",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        600,
        100
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "2d7cba78-1e46-4563-9683-2151f4bc3cc6",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.output.sender_name }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1f9d175e-dd19-4a5e-b4cf-68c67769514e",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        220,
        240
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "7547e456-d72e-4bd7-a483-e600026132a1",
      "name": "Name Found",
      "type": "n8n-nodes-base.set",
      "position": [
        820,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "0064bc2e-17b5-4d55-945e-ad61c4a7e796",
              "name": "intro_line",
              "type": "string",
              "value": "=Dear {{ $json.output.sender_name }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f5335b2d-2732-47fc-a916-17bd9d40e0da",
      "name": "Name Not Found",
      "type": "n8n-nodes-base.set",
      "position": [
        820,
        200
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "0064bc2e-17b5-4d55-945e-ad61c4a7e796",
              "name": "intro_line",
              "type": "string",
              "value": "=Hey,"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "53f97b56-7bc6-4f25-b3c7-761fb38d7174",
      "name": "OpenAI Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        200,
        960
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "2cc7a078-016e-4205-897d-a6c20f23dd52",
      "name": "Label - Agency Lead",
      "type": "n8n-nodes-base.gmail",
      "position": [
        560,
        420
      ],
      "parameters": {
        "labelIds": [
          "Label_2779457653887615300"
        ],
        "messageId": "={{ $json.message_id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "af773077-758f-4f64-94b2-658784b8689d",
      "name": "Label - Course Request",
      "type": "n8n-nodes-base.gmail",
      "position": [
        560,
        620
      ],
      "parameters": {
        "labelIds": [
          "Label_6090976944524934821"
        ],
        "messageId": "={{ $json.message_id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "a93709f4-15a0-4333-b9e1-aff10ff6c693",
      "name": "Label - Paid Collaborations",
      "type": "n8n-nodes-base.gmail",
      "position": [
        560,
        820
      ],
      "parameters": {
        "labelIds": [
          "Label_2508736995105587525"
        ],
        "messageId": "={{ $json.message_id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "7994e586-a28c-47d6-902f-9389516d13f7",
      "name": "Label - Miscellaneous",
      "type": "n8n-nodes-base.gmail",
      "position": [
        560,
        1020
      ],
      "parameters": {
        "labelIds": [
          "Label_5123095855127638673"
        ],
        "messageId": "={{ $json.message_id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "f7c8cc18-f017-4a5d-92e9-c80d16a1269a",
      "name": "Message a model",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        780,
        420
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini",
          "cachedResultName": "GPT-4.1-MINI"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an intelligent and useful personal assistant that replies to my emails"
            },
            {
              "content": "=Here's the email to reply to:\n\nIntro line for the sender:{{ $('Single Intro Line').item.json.intro_line }}\nEmail Body: {{ $('Gmail Trigger').item.json.text }}\nEmail Subject:{{ $('Gmail Trigger').item.json.subject }}"
            },
            {
              "content": "=Please format the data in this format:\n\n{\n\n\"subject\": \"The subject line fo the email you drafted\"\n\"body\": \"The body of the email you drafted\"\n\n}"
            }
          ]
        },
        "jsonOutput": true
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "5c4ae3ba-5ab9-46c3-aa20-b43d8c62a1a3",
      "name": "Create a draft",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1140,
        420
      ],
      "parameters": {
        "message": "={{ $json.message.content.body }}",
        "options": {
          "threadId": "={{ $('Label - Agency Lead').item.json.threadId }}"
        },
        "subject": "={{ $json.message.content.subject }}",
        "resource": "draft"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "51f46e0e-4280-414d-9f1f-545027c6965a",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        180,
        0
      ],
      "parameters": {
        "width": 300,
        "height": 360,
        "content": "## Name Extractor"
      },
      "typeVersion": 1
    },
    {
      "id": "ee1210c5-62c0-41f5-be0a-0ca4b90879b6",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        -40
      ],
      "parameters": {
        "color": 5,
        "width": 600,
        "height": 400,
        "content": "## Personalised Intro"
      },
      "typeVersion": 1
    },
    {
      "id": "3097cff4-606a-46ab-9982-a79ff30ed097",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -80,
        540
      ],
      "parameters": {
        "color": 4,
        "width": 560,
        "height": 600,
        "content": "## Sentiment Analysis"
      },
      "typeVersion": 1
    },
    {
      "id": "956a90c8-0752-41a3-8496-bf3c5e9eb94f",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        520,
        380
      ],
      "parameters": {
        "color": 6,
        "width": 860,
        "height": 800,
        "content": "## Personalised Intro"
      },
      "typeVersion": 1
    },
    {
      "id": "24ce2fe3-e674-4d47-b7b3-6dc8eb94a328",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1120,
        -100
      ],
      "parameters": {
        "color": 3,
        "width": 960,
        "height": 1240,
        "content": "# Overview\n\nThis template triages incoming Gmail in real time using GPT. It extracts the sender\u2019s name, classifies the message into four categories (Agency Lead, Course Request, Paid Collaborations, Miscellaneous), applies the appropriate Gmail label, and either sends an immediate courtesy reply (for Course Requests and Paid Collaborations) or creates a structured Gmail draft for you to review (for Agency Leads). It also upserts Paid Collaboration contacts into Google Sheets for follow-up.\n\n# Who it\u2019s for\n\nSolo operators and small teams that get mixed inbound and want consistent, fast triage without living in the inbox. These classification specifically is good for Agencies and Coaches\n\n## How it works / What it does\n\n- Trigger: New unread Gmail message/s\n- Name extraction (AI): Pulls the sender name from the email body or signature to personalise replies\n- Topic classification (AI): Routes to Agency Lead, Course Request, Paid Collaborations, or Miscellaneous\n\n## Actions:\n1. Agency Lead \u2192 add label and create GPT-draft\n2. Course Request \u2192 add label and auto-reply\n3. Paid Collaborations \u2192 add label, auto-reply, and upsert to Google Sheets\n4. Miscellaneous \u2192 add label only\n\n## Requirements\n- Gmail account connected in n8n\n- OpenAI credentials\n-  Google Sheet for Paid Collaborations with columns: Name, email, subject\n\n## Setup\n1. Connect Gmail, OpenAI, and (optionally) Google Sheets credentials in n8n\n2. Create four Gmail labels - in my case I created:\n    - Agency lead: This includes people/companies interested in inquiries regarding AI solutions, may it be asking info on what they can do, or me building a solution for them.\n   - Course Request: This includes inquiries from people regarding which courses they should choose in order to understand/study AI or AI Agents \n   - Paid Collaborations: Inquiries regarding sponsorship of books or tools on my channels. There are also situations in which they don't specifically say that they will pay, but they ask me to share their material with my audience\n   - Miscellaneous: the remaining part --> mandatory category to make this work properly.\n3. Create a Google Sheet with 3 columns: email, name, email \n\n## How to customize\n- Edit the four categories and their descriptions in the Classify Email Topic (AI) node\n- Tweak auto-reply copy in the auto-reply nodes; adjust the GPT draft prompt if needed\n- Change the model or temperature in the LLM nodes; extend the Google Sheets schema over time"
      },
      "typeVersion": 1
    },
    {
      "id": "88e31730-9014-4076-bc2a-71d1dab5ef78",
      "name": "Name Extractor",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        220,
        80
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {
          "systemPromptTemplate": "=You are an expert extraction algorithm.\nOnly extract relevant information from the text.\nIf you do not know the value of an attribute asked to extract, you may omit the attribute's value."
        },
        "attributes": {
          "attributes": [
            {
              "name": "sender_name",
              "required": true,
              "description": "=This is the sender name, which corresponds to the signature of the sender at the end of the body of the email. If the name is not available, return an empty string."
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e97c25b3-84f0-4602-86cc-45652f9a6bf8",
      "name": "Single Intro Line",
      "type": "n8n-nodes-base.merge",
      "position": [
        1040,
        100
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "joinMode": "keepNonMatches",
        "fieldsToMatchString": "=intro_line"
      },
      "typeVersion": 3.2
    },
    {
      "id": "0568b7a5-017a-4db1-94f1-bf80057db779",
      "name": "Reducing content before LLM",
      "type": "n8n-nodes-base.set",
      "position": [
        -40,
        720
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "a5357d59-3a7a-4197-b005-a8fa4acea9c0",
              "name": "intro_line",
              "type": "string",
              "value": "={{ $json.intro_line }}"
            },
            {
              "id": "17c5b46b-77e6-43ab-a314-6a36a21651f8",
              "name": "email_body",
              "type": "string",
              "value": "={{ $('Gmail Trigger').item.json.text }}"
            },
            {
              "id": "e534b113-0a05-4312-b577-b1918f03b361",
              "name": "message_id",
              "type": "string",
              "value": "={{ $('Gmail Trigger').item.json.id }}"
            },
            {
              "id": "21ab6542-a5b6-4ca0-9605-4f1c9b27e70d",
              "name": "thread_id",
              "type": "string",
              "value": "={{ $('Gmail Trigger').item.json.threadId }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "9434fc19-bbb8-4778-8e9b-49b7777cac77",
      "name": "AI-Classification & Sentiment Analysis",
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "position": [
        200,
        720
      ],
      "parameters": {
        "options": {},
        "inputText": "={{ $json.email_body }}",
        "categories": {
          "categories": [
            {
              "category": "Agency Lead",
              "description": "This includes people/companies interested in inquiries regarding AI solutions, may it be asking info on what they can do, or me building a solution for them."
            },
            {
              "category": "Course Request",
              "description": "This includes inquiries from people regarding which courses they should choose in order to understand/study AI or AI Agents "
            },
            {
              "category": "Paid Collaborations",
              "description": "Inquiries regarding sponsorship of books or tools on my channels. There are also situations in which they don't specifically say that they will pay, but they ask me to share their material with my audience"
            },
            {
              "category": "Miscellaneous",
              "description": "All the remaining messages"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "680a89f0-148f-4b91-a63f-a428744ba177",
      "name": "Standard Answer",
      "type": "n8n-nodes-base.gmail",
      "position": [
        840,
        620
      ],
      "parameters": {
        "message": "={{ $('AI-Classification & Sentiment Analysis').item.json.intro_line }},\n\nThanks for reaching out.\n\nThis email goes out as soon as I receive a request of a course recommendation.\n\nThis is my phone number: XXXX\n\nPlease reach out to me there!\n\nThanks,\nGiovanni",
        "options": {},
        "emailType": "text",
        "messageId": "={{ $json.id }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "bcd48315-4957-4cd1-81e2-97ea4ea1431e",
      "name": "Standard Answer 2",
      "type": "n8n-nodes-base.gmail",
      "position": [
        840,
        820
      ],
      "parameters": {
        "message": "={{ $('AI-Classification & Sentiment Analysis').item.json.intro_line }},\n\nThanks for reaching out.\n\nThis email goes out as soon as I receive a request of a paid collab.\n\nThis is my phone number: XXXX\n\nPlease reach out to me there!\n\nThanks,\nGiovanni",
        "options": {},
        "emailType": "text",
        "messageId": "={{ $json.id }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "39a258ef-64a3-45df-b048-0de048a4825c",
      "name": "Update DB",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1140,
        820
      ],
      "parameters": {
        "columns": {
          "value": {
            "Name": "={{ $('Single Intro Line').item.json.intro_line.replace('Dear ', '') }}",
            "email": "={{ $('Gmail Trigger').item.json.from.value[0].address }}",
            "subject": "={{ $('Gmail Trigger').item.json.subject }}"
          },
          "schema": [
            {
              "id": "Name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "email",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "subject",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "subject",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "email"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "appendOrUpdate",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1mW0vLLwJXdWBEe5_JxT9HfK1U8ECygmu-zNnfOf93Xo/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1mW0vLLwJXdWBEe5_JxT9HfK1U8ECygmu-zNnfOf93Xo",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1mW0vLLwJXdWBEe5_JxT9HfK1U8ECygmu-zNnfOf93Xo/edit?usp=drivesdk",
          "cachedResultName": "Paid Collaborations"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "030afcdb-4da5-4947-aafe-042dae86a937",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Name Found",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Name Not Found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Name Found": {
      "main": [
        [
          {
            "node": "Single Intro Line",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Name Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Name Extractor": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Name Not Found": {
      "main": [
        [
          {
            "node": "Single Intro Line",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        [
          {
            "node": "Create a draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Name Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Single Intro Line": {
      "main": [
        [
          {
            "node": "Reducing content before LLM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Standard Answer 2": {
      "main": [
        [
          {
            "node": "Update DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "AI-Classification & Sentiment Analysis",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Label - Agency Lead": {
      "main": [
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Label - Course Request": {
      "main": [
        [
          {
            "node": "Standard Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Label - Paid Collaborations": {
      "main": [
        [
          {
            "node": "Standard Answer 2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reducing content before LLM": {
      "main": [
        [
          {
            "node": "AI-Classification & Sentiment Analysis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI-Classification & Sentiment Analysis": {
      "main": [
        [
          {
            "node": "Label - Agency Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label - Course Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label - Paid Collaborations",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label - Miscellaneous",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}