How to access gmail label in n8n

i have a condition where i am fetching emails and categorizing in different category like spam,advertisement,notification,professional,personal etc and starring this mails back into gmail this is working but now the condition is i have to ceate dfiffernt labels for different category and check if this label exits can do not create and proceed further if not create new label and proceed further .how can i implement this in n8n.

Here the issue is for the first time if i am creating new label its working but if once the labels are created in gmail . i want to check where there are new mail with if its spam do not create spam label skip it and move to the next flow . I dont know how to achieve this .

Hi, can you please share you workflow with ctrl-c/ctrl-v so it becomes dynamic in the interface (instead of a screenshot)

reg,
j.

its giving code by ctrl c+ ctrl v
{
“nodes”: [
{
“parameters”: {},
“type”: “n8n-nodes-base.manualTrigger”,
“typeVersion”: 1,
“position”: [
-220,
-100
],
“id”: “b266e66a-68c2-4ec3-ba21-6cfb97f77aa5”,
“name”: “When clicking ‘Test workflow’”
},
{
“parameters”: {
“rules”: {
“values”: [
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“leftValue”: “={{ $json.Email_Type }}”,
“rightValue”: “=advertisement”,
“operator”: {
“type”: “string”,
“operation”: “equals”
},
“id”: “c2230c80-7bc4-40d6-9391-eaa1c55ae449”
}
],
“combinator”: “and”
}
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “0a2787a4-46c5-4d62-8fe7-474d1f62bfa8”,
“leftValue”: “={{ $json.Email_Type }}”,
“rightValue”: “professional”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
}
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “aa448566-d1ea-4428-9997-9d428e574f61”,
“leftValue”: “={{ $json.Email_Type }}”,
“rightValue”: “spam”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
}
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “b066b584-5547-44c6-b409-46c0ce96a029”,
“leftValue”: “={{ $json.Email_Type }}”,
“rightValue”: “other”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
}
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “52eb0a9d-d630-4b07-86cb-e64e09b2c9b7”,
“leftValue”: “={{ $json.Email_Type }}”,
“rightValue”: “personal”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
}
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “d9ac97d9-9f84-4be1-b9d1-cd14de2cb085”,
“leftValue”: “={{ $json.Email_Type }}”,
“rightValue”: “notification”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
}
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.switch”,
“typeVersion”: 3.2,
“position”: [
2080,
-160
],
“id”: “8d09d412-97cd-46b3-bcf5-f5d29cb2b789”,
“name”: “Switch”
},
{
“parameters”: {
“operation”: “addLabels”,
“messageId”: “={{ $(‘Switch’).item.json.Email_id }}”,
“labelIds”: “={{ $(‘Filter labels from gmail’).item.json.} }}”
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
2420,
-620
],
“id”: “375af51a-29b3-43ce-a6fd-efeb94556567”,
“name”: “Advertisement”,
“webhookId”: “ac229633-ea4e-4c5a-9044-55c6ce9b9b2d”,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
}
},
{
“parameters”: {
“operation”: “addLabels”,
“messageId”: “={{ $(‘Switch’).item.json.Email_id }}”,
“labelIds”: “={{ $(‘Gmail4’).item.json.id }}”
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
2420,
-220
],
“id”: “bdcb8f6c-3650-4398-86ed-4d24166977ec”,
“name”: “Spam”,
“webhookId”: “f249f7ea-4168-4ddf-88eb-2b13841225ca”,
“credentials”: {
“gmailOAuth2”: {
“id”: “yrNURJxFlRIOXpzR”,
“name”: “Gmail account”
}
}
},
{
“parameters”: {
“operation”: “addLabels”,
“messageId”: “={{ $(‘Switch’).item.json.Email_id }}”,
“labelIds”: “={{ $(‘Gmail5’).item.json.id }}”
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
2420,
-20
],
“id”: “5b6d4a13-2542-42c4-8270-ea3d1ce1c643”,
“name”: “others”,
“webhookId”: “db56475f-1665-41e3-ba30-5d4e45562a40”,
“credentials”: {
“gmailOAuth2”: {
“id”: “yrNURJxFlRIOXpzR”,
“name”: “Gmail account”
}
}
},
{
“parameters”: {
“language”: “python”,
“pythonCode”: “# Get all the input data\ninput_data = input.all()\n\n# Create a list to store the output items\noutput_items = []\n\n# Loop through each item in the input data\nfor item in input_data:\n # Print the full item data to check its structure\n print(item) # Log the structure to see what the input looks like\n\n # Extract the snippet (or use ‘No Snippet’ if it’s missing)\n snippet = item[‘json’].get(‘snippet’, ‘No Snippet’) # Extract snippet\n id_value = item[‘json’].get(‘id’, ‘No ID’) # Extract id\n from_value = item[‘json’].get(‘From’, ‘No From’) # Extract ‘from’ field\n subject = item[‘json’].get(‘Subject’, ‘No Subject’) # Extract subject (new field)\n\n # If both snippet and from are missing, fall back to default values\n if snippet == ‘No Snippet’ and from_value == ‘No From’:\n from_value = ‘Unknown Sender’\n\n # Create a unique key based on snippet (or subject if you want)\n unique_key = id_value # Combine first 10 chars of snippet and id for uniqueness\n\n # Prepare the item data\n item_data = {\n ‘json’: {\n ‘snippet’: snippet,\n ‘id’: id_value,\n ‘from’: from_value,\n ‘subject’: subject \n }\n }\n\n # Append this item to the output list\n output_items.append(item_data)\n\n# Return the list of output items\nreturn output_items\n"
},
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
1000,
-100
],
“id”: “69cf5f50-0f5e-4b6b-a383-9e888eb9d704”,
“name”: “Filter labels from gmail”
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “8ab230e5-8c8a-4195-9631-740f3ccea3a7”,
“name”: “from”,
“value”: “={{ $json.from.split(‘<’)[1].split(‘>’)[0]}}”,
“type”: “string”
}
]
},
“includeOtherFields”: true,
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
1200,
-100
],
“id”: “61fe0e06-b558-44c1-958f-6e1456b93af1”,
“name”: “Remove extra text from email_address”
},
{
“parameters”: {
“method”: “POST”,
“url”: “https://api.sambanova.ai/v1/chat/completions”,
“sendHeaders”: true,
“headerParameters”: {
“parameters”: [
{
“name”: “Authorization”,
“value”: “Bearer c76e5eea-483e-4d9b-a702-7b89628dbe9f”
}
]
},
“sendBody”: true,
“specifyBody”: “json”,
“jsonBody”: “={\n "stream": false,\n "model": "Meta-Llama-3.1-8B-Instruct",\n "messages": [\n {\n "role": "system",\n "content": "You are a helpful assistant"\n },\n {\n "role": "user",\n "content": "Please categorize the following email snippet. The possible categories are: professional, advertisement, spam, personal or other.Please categorize my emails related to package status updates (received, shipped, delivered, etc.) and bank transactions (credited, received, etc.) under the ‘Notification’ category. Here are the details: Snippet = {{ $json.snippet}}, id = {{ $json.id }},subject= {{ $json.subject }}.Based on the content, please return the type of email .Please extract the id_value and content from the provided JSON data. Please give the response in this format email_from ({{ $json.from }}) : email_type : email_id_value {{ $json.id }}, thats it no other explantion."\n }\n ]\n}”,
“options”: {}
},
“type”: “n8n-nodes-base.httpRequest”,
“typeVersion”: 4.2,
“position”: [
1440,
-100
],
“id”: “a4cf396f-300b-4484-b0a4-c6edc1e78239”,
“name”: “Checking the Type of email”
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “2f5fe713-736c-433d-ac4e-16df327c0496”,
“name”: “Data”,
“value”: “={{ $json.choices[0].message.content }}”,
“type”: “string”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
1660,
-100
],
“id”: “687b260d-7c67-4de4-897c-4679e5e2576b”,
“name”: “Extracting content”
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “2006fe2c-a9e8-47cd-826f-b9dbc51c6ef6”,
“name”: “Email_from”,
“value”: "={{ \n $json.Data.match(/([a-zA-Z0-9.
%±]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g) \n ? $json.Data.match(/([a-zA-Z0-9._%±]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g)[0].replace(/\n/g, ‘’).trim() \n : ‘’ \n }}”,
“type”: “string”
},
{
“id”: “6ecda2f4-55f3-49b6-9eb2-0c1b87fc4c2d”,
“name”: “Email_id”,
“value”: “={{ \n $json.Data.match(/: ([a-zA-Z0-9]{10,})$/) \n ? $json.Data.match(/: ([a-zA-Z0-9]{10,})$/)[1] \n : ‘’ \n }}\n”,
“type”: “string”
},
{
“id”: “2297dbde-7e39-4450-b24c-9cef3c528cd3”,
“name”: “Email_Type”,
“value”: “={{\n $json.Data.split(" : ")[0] === "Notification" ? "notification" : $json.Data.split(" : ")[1] ? $json.Data.split(" : ")[1] : ‘’\n}}”,
“type”: “string”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
1860,
-100
],
“id”: “96efff3f-cfd8-48e9-9d96-e7d061d851cc”,
“name”: “Extracting Email address ,id and type of email”
},
{
“parameters”: {
“operation”: “addLabels”,
“messageId”: “={{ $(‘Switch’).item.json.Email_id }}”,
“labelIds”: “={{ $(‘Gmail6’).item.json.id }}”
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
2420,
180
],
“id”: “c88b04ae-a8b1-4c24-856d-6757dbdd47ef”,
“name”: “Personal”,
“webhookId”: “f249f7ea-4168-4ddf-88eb-2b13841225ca”,
“credentials”: {
“gmailOAuth2”: {
“id”: “yrNURJxFlRIOXpzR”,
“name”: “Gmail account”
}
}
},
{
“parameters”: {},
“type”: “n8n-nodes-base.wait”,
“typeVersion”: 1.1,
“position”: [
1120,
-300
],
“id”: “b1b63f7e-8567-4f7c-a402-9d11d1a9415b”,
“name”: “Wait”,
“webhookId”: “4cd3f3f3-8a19-48b6-9ad5-05104db66a25”,
“executeOnce”: true,
“alwaysOutputData”: true,
“onError”: “continueErrorOutput”
},
{
“parameters”: {
“resource”: “label”,
“operation”: “create”,
“name”: “=Professional”,
“options”: {}
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
140,
-300
],
“id”: “202d347b-2daf-4607-90dc-fb36ff1f2d09”,
“name”: “Gmail3”,
“webhookId”: “dbd9e570-77bb-4f83-8e02-b957ee7e02bc”,
“executeOnce”: true,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
},
“onError”: “continueRegularOutput”
},
{
“parameters”: {
“resource”: “label”,
“operation”: “create”,
“name”: “=Junk”,
“options”: {}
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
320,
-300
],
“id”: “78aa890c-8573-4bcf-a9b8-e9dc054880f8”,
“name”: “Gmail4”,
“webhookId”: “dbd9e570-77bb-4f83-8e02-b957ee7e02bc”,
“executeOnce”: true,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
},
“onError”: “continueRegularOutput”
},
{
“parameters”: {
“resource”: “label”,
“operation”: “create”,
“name”: “Others”,
“options”: {}
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
540,
-300
],
“id”: “fbbfca53-98ce-48ed-9a9b-1bc44c5388d5”,
“name”: “Gmail5”,
“webhookId”: “dbd9e570-77bb-4f83-8e02-b957ee7e02bc”,
“executeOnce”: true,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
},
“onError”: “continueRegularOutput”
},
{
“parameters”: {
“resource”: “label”,
“operation”: “create”,
“name”: “=Personal”,
“options”: {}
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
740,
-300
],
“id”: “c9b820c3-bfa8-4fbb-a426-19f0c865b977”,
“name”: “Gmail6”,
“webhookId”: “dbd9e570-77bb-4f83-8e02-b957ee7e02bc”,
“executeOnce”: true,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
},
“onError”: “continueRegularOutput”
},
{
“parameters”: {
“resource”: “label”,
“operation”: “create”,
“name”: “=Notification”,
“options”: {}
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
920,
-300
],
“id”: “42ae19be-0db0-4385-81c6-fed024f0116b”,
“name”: “Gmail7”,
“webhookId”: “dbd9e570-77bb-4f83-8e02-b957ee7e02bc”,
“executeOnce”: true,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
},
“onError”: “continueRegularOutput”
},
{
“parameters”: {
“operation”: “addLabels”,
“messageId”: “={{ $(‘Switch’).item.json.Email_id }}”,
“labelIds”: “={{ $(‘Gmail3’).item.json.id}}”
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
2420,
-420
],
“id”: “1d34358a-85cf-4f7b-949b-875306efca07”,
“name”: “Professional”,
“webhookId”: “d1947251-327d-4d5b-9d66-aa2db991e9df”,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
}
},
{
“parameters”: {
“operation”: “addLabels”,
“messageId”: “={{ $(‘Switch’).item.json.Email_id }}”,
“labelIds”: [
“Label_44”
]
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
2420,
380
],
“id”: “6dd76ba2-8e8a-4835-9047-be8ebabfcedc”,
“name”: “Notification”,
“webhookId”: “c8933abd-4448-4288-982b-8bd53314469e”,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
}
},
{
“parameters”: {
“operation”: “getAll”,
“limit”: 10,
“filters”: {
“labelIds”:
}
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
-20,
-100
],
“id”: “7c5caf06-bc32-4d06-a009-17523e3a5eb8”,
“name”: “Gmail”,
“webhookId”: “3a0c5338-9a8e-49a1-8050-cca12f0f0d0c”,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
}
},
{
“parameters”: {
“resource”: “label”,
“limit”: “=”
},
“type”: “n8n-nodes-base.gmail”,
“typeVersion”: 2.1,
“position”: [
0,
-300
],
“id”: “6a55b94d-a77b-411d-8c81-b615b01eb480”,
“name”: “Gmail1”,
“webhookId”: “dbd9e570-77bb-4f83-8e02-b957ee7e02bc”,
“executeOnce”: true,
“credentials”: {
“gmailOAuth2”: {
“id”: “0ryGg7ZdWt0ApNmK”,
“name”: “Gmail account 3”
}
},
“onError”: “continueRegularOutput”
},
{
“parameters”: {
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “feaac499-0f88-47a0-9ee0-8f4b05f7ccbb”,
“leftValue”: “={{ $json.labels.find(id => "advertisement") }}”,
“rightValue”: “”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
},
“options”: {}
},
“type”: “n8n-nodes-base.if”,
“typeVersion”: 2.2,
“position”: [
200,
-100
],
“id”: “fe3e72b9-d71e-45d6-b365-80ab435dee92”,
“name”: “If”
}
],
“connections”: {
“When clicking ‘Test workflow’”: {
“main”: [
[
{
“node”: “Gmail”,
“type”: “main”,
“index”: 0
}
]
]
},
“Switch”: {
“main”: [
[
{
“node”: “Advertisement”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “Professional”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “Spam”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “others”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “Personal”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “Notification”,
“type”: “main”,
“index”: 0
}
]
]
},
“Advertisement”: {
“main”: [

]
},
“Filter labels from gmail”: {
“main”: [
[
{
“node”: “Remove extra text from email_address”,
“type”: “main”,
“index”: 0
}
]
]
},
“Remove extra text from email_address”: {
“main”: [
[
{
“node”: “Checking the Type of email”,
“type”: “main”,
“index”: 0
}
]
]
},
“Checking the Type of email”: {
“main”: [
[
{
“node”: “Extracting content”,
“type”: “main”,
“index”: 0
}
]
]
},
“Extracting content”: {
“main”: [
[
{
“node”: “Extracting Email address ,id and type of email”,
“type”: “main”,
“index”: 0
}
]
]
},
“Extracting Email address ,id and type of email”: {
“main”: [
[
{
“node”: “Switch”,
“type”: “main”,
“index”: 0
}
]
]
},
“Wait”: {
“main”: [

]
},
“Gmail3”: {
“main”: [
[
{
“node”: “Gmail4”,
“type”: “main”,
“index”: 0
}
]
]
},
“Gmail4”: {
“main”: [
[
{
“node”: “Gmail5”,
“type”: “main”,
“index”: 0
}
]
]
},
“Gmail5”: {
“main”: [
[
{
“node”: “Gmail6”,
“type”: “main”,
“index”: 0
}
]
]
},
“Gmail6”: {
“main”: [
[
{
“node”: “Gmail7”,
“type”: “main”,
“index”: 0
}
]
]
},
“Gmail7”: {
“main”: [
[
{
“node”: “Wait”,
“type”: “main”,
“index”: 0
}
]
]
},
“Gmail”: {
“main”: [
[
{
“node”: “If”,
“type”: “main”,
“index”: 0
}
]
]
},
“Gmail1”: {
“main”: [
[
{
“node”: “Gmail3”,
“type”: “main”,
“index”: 0
}
]
]
},
“If”: {
“main”: [
[
{
“node”: “Filter labels from gmail”,
“type”: “main”,
“index”: 0
}
]
]
}
},
“pinData”: {},
“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “b6d65c5e05cfe7d29ece5de304e03eb2ac263f17ab5bff6f65d9b5e77d8bc94e”
}
}

please include it in a code node so it displays correctly

ok thankyou so much sorry i am new to n8n so i din’t knew it

Hi, no problem. the most important part is that you are here to learn.

Have you seen something like this?:

It would seem more scalable if you would have it done by an LLM directly and create clear textual rules of what it should do.

(as TBH it’s not very clear of exactly what you want to achieve. as well as, your logic is very fixed on a certain strucuture. you will need to adapt it each time if something changes.,)

reg,
J.

I am trying this solution if it can work

1 Like

i don’t have gpt4 to connect api

Hi, Yes you do need to have an API key for openAI. It might be possible to do something like this with the free ones, but you will need to try yourself.

reg,
J.

hey , i have tried the solution you using open.ai chat free version of gemini but this logic is when labels are already created in gmail .This will not work if new label should create according to category

hi, the workflow i have shared was not meant to be a full solution to your problem. It was meant as a starting point to have a better base for creating a solution.

regards,
J.

okay thankyouu so much for the help

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.