Persistent & Unfixable Character Encoding Issue with IMAP Trigger

Hello,

I’m experiencing a severe character encoding issue with the IMAP trigger that I have been unable to solve after extensive troubleshooting. Special characters like , , and are being garbled into multi-character sequences like â€TM, –, and ₹.

The Problem:

The text is corrupted as soon as it comes out of the Email Trigger (IMAP) node. The garbled text persists even after trying numerous advanced JavaScript methods in a Code node.

My Environment:
n8n Version: Running version [email protected]

Workflow JSON:
{
“name”: “Write”,
“nodes”: [
{
“parameters”: {
“fieldsToAggregate”: {
“fieldToAggregate”: [
{
“fieldToAggregate”: “Email”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.aggregate”,
“typeVersion”: 1,
“position”: [
-880,
-2096
],
“id”: “711939cd-ebf7-46b4-b150-42e8ae7e6f79”,
“name”: “Aggregate1”
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“name”: “allEmails”,
“value”: “={{$json["Email"].join(",")}}”,
“type”: “string”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
-656,
-2096
],
“id”: “233cb912-a30d-4ba5-964d-d8785e1d8276”,
“name”: “Set allEmails”
},
{
“parameters”: {
“mode”: “combine”,
“combineBy”: “combineByPosition”,
“options”: {}
},
“type”: “n8n-nodes-base.merge”,
“typeVersion”: 3,
“position”: [
-432,
-2096
],
“id”: “3045b694-b187-440a-bef8-8a0f5a6d8f89”,
“name”: “Merge Data”
},
{
“parameters”: {
“postProcessAction”: “nothing”,
“options”: {}
},
“type”: “n8n-nodes-base.emailReadImap”,
“typeVersion”: 2,
“position”: [
-1776,
-2096
],
“id”: “782f5462-06c2-4ebd-bbbd-2adf0711354c”,
“name”: “Email Trigger (IMAP)1”,
“credentials”: {
“imap”: {
“id”: “7JYhLKy207qNIbc2”,
“name”: “IMAP account”
}
}
},
{
“parameters”: {
“conditions”: {
“string”: [
{
“value1”: “={{$json["subject"]}}”,
“operation”: “contains”,
“value2”: “Write”
}
]
},
“combineOperation”: “any”
},
“id”: “651c771c-4b0e-43b2-91b9-ff4857cece5e”,
“name”: “Filter Subject1”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 1,
“position”: [
-1552,
-2096
]
},
{
“parameters”: {
“documentId”: {
“__rl”: true,
“value”: “1o02lDDfi6LEQ9a2LdFCjze9bmywaa4OOXmunHj0RJCo”,
“mode”: “list”
},
“sheetName”: {
“__rl”: true,
“value”: “gid=0”,
“mode”: “list”
},
“options”: {}
},
“id”: “41b3433f-31f3-4f1e-b942-b30f8acb7cc7”,
“name”: “Google Sheets Lookup1”,
“type”: “n8n-nodes-base.googleSheets”,
“typeVersion”: 3,
“position”: [
-1104,
-2096
],
“credentials”: {
“googleSheetsOAuth2Api”: {
“id”: “S0yw7b8vuLmFgzpK”,
“name”: “Google Sheets account”
}
}
},
{
“parameters”: {
“fromEmail”: “[email protected]”,
“toEmail”: “={{$json["allEmails"]}}”,
“subject”: “=”,
“text”: “={{$json.cleanedText}}”,
“html”: “={{$json.cleanedText}}”,
“options”: {}
},
“id”: “a53c1d38-2011-4193-87b8-7c0a761a9706”,
“name”: “Send Email1”,
“type”: “n8n-nodes-base.emailSend”,
“typeVersion”: 1,
“position”: [
-208,
-2096
],
“credentials”: {
“smtp”: {
“id”: “wtsigrcJVCr2JS12”,
“name”: “SMTP account”
}
}
},
{
“parameters”: {
“jsCode”: “// This is a completely new approach.\n// We will ignore the broken ‘textPlain’ field and rebuild clean text from the ‘textHtml’ field.\n\nfor (const item of items) {\n let text = item.json.textHtml || "";\n\n // ----- STAGE 1: Decode common HTML entities -----\n // This converts things like ‘'’ and ’ ’ into real characters.\n text = text.replace(/'/g, "'");\n text = text.replace(/"/g, ‘"’);\n text = text.replace(/&/g, "&");\n text = text.replace(/</g, "<");\n text = text.replace(/>/g, ">");\n text = text.replace(/ /g, " ");\n\n // ----- STAGE 2: Strip all remaining HTML tags -----\n // This removes all tags like

,
, etc., leaving only the text content.\n text = text.replace(/<[^>]*>/g, ‘’);\n\n // ----- STAGE 3: Fix the stubborn encoding errors -----\n // After cleaning the HTML, we run the targeted replacements on the result.\n text = text.replace(/â€TM/g, "’"); // Apostrophe\n text = text.replace(/–/g, "–"); // Dash\n text = text.replace(/₹/g, "₹"); // Rupee\n text = text.replace(/ /g, " "); // Narrow Space\n text = text.replace(/ /g, " "); // Non-breaking space\n text = text.replace(/\u00A0/g, ’ '); // Non-breaking space\n\n // ----- STAGE 4: Clean up extra whitespace -----\n // This trims whitespace from the start/end and collapses multiple blank lines.\n text = text.trim();\n text = text.replace(/(\r\n|\n|\r){2,}/g, ‘\n\n’);\n\n item.json.cleanedText = text;\n}\n\nreturn items;”
},
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
-1328,
-2096
],
“id”: “5e1cedc5-4850-4cad-8f37-c60910aadddf”,
“name”: “Code”
}
],
“pinData”: {},
“connections”: {
“Email Trigger (IMAP)1”: {
“main”: [
[
{
“node”: “Filter Subject1”,
“type”: “main”,
“index”: 0
}
]
]
},
“Filter Subject1”: {
“main”: [
[
{
“node”: “Code”,
“type”: “main”,
“index”: 0
}
]
]
},
“Google Sheets Lookup1”: {
“main”: [
[
{
“node”: “Aggregate1”,
“type”: “main”,
“index”: 0
}
]
]
},
“Aggregate1”: {
“main”: [
[
{
“node”: “Set allEmails”,
“type”: “main”,
“index”: 0
}
]
]
},
“Set allEmails”: {
“main”: [
[
{
“node”: “Merge Data”,
“type”: “main”,
“index”: 0
}
]
]
},
“Merge Data”: {
“main”: [
[
{
“node”: “Send Email1”,
“type”: “main”,
“index”: 0
}
]
]
},
“Code”: {
“main”: [
[
{
“node”: “Google Sheets Lookup1”,
“type”: “main”,
“index”: 0
}
]
]
}
},
“active”: true,
“settings”: {
“executionOrder”: “v1”
},
“versionId”: “326cb221-20a6-4a5d-9838-602628489214”,
“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “8e30da2bd43177052339df7305966534d27cc7cf722e353e019ce5941023f208”
},
“id”: “6LfalduADM8OLocK”,
“tags”:
}

Example Output from IMAP Node:
[
{
“textHtml”: “<div dir="ltr">

<div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">---------- Forwarded message ---------
From: <strong class="gmail_sendername" dir="auto">IT PIXROCKVFX <span dir="auto"><<a href="mailto:[email protected]">[email protected]>
Date: Tue, Jul 22, 2025 at 11:11 AM
Subject: Best gaming PC build under ₹1,65,000 in July 2025 | Write
To: <<a href="mailto:[email protected]">[email protected]>



<div dir="ltr">If you’re aiming for the sweet spot between elite-tier gaming and smooth creative productivity, this build is the answer. It is built for those who want top-tier performance in both gaming and creative workloads, this build combines AMD’s 3D V-Cache gaming king with NVIDIA’s powerful new RTX 5070 Ti, all inside a premium glass chassis. It's equally comfortable handling AAA gaming, 3D rendering, and heavy multitasking, while staying cool and upgrade-ready for the future.

AMD Ryzen 7 7800X3D – ₹39,000

The 7800X3d is hands down the undisputed gaming king in AMD’s lineup, it features 3D V-Cache for ultra-low latency and phenomenal gaming performance. With 8 cores and 16 threads, it’s not just great for gaming, it’s also excellent for creative workloads like After Effects, 3D modeling, and DaVinci Resolve. All this with superb power efficiency.

Gigabyte GeForce RTX 5070 Ti Windforce OC 12GB – ₹87,000
 A DLSS 4-capable beast. The RTX 5070 Ti brings exceptional 1440p Ultra and light 4K gaming performance, paired with the efficiency of the Ada Lovelace architecture. Ideal for ray tracing, high refresh rates, and GPU-intensive software like Blender, Resolve, and Unreal Engine.\r\n\r\n”,
“textPlain”: “---------- Forwarded message ---------\r\nFrom: IT PIXROCKVFX [email protected]\r\nDate: Tue, Jul 22, 2025 at 11:11 AM\r\nSubject: Best gaming PC build under ₹1,65,000 in July 2025 | Write\r\nTo: [email protected]\r\n\r\n\r\nIf you’re aiming for the sweet spot between elite-tier gaming and smooth\r\ncreative productivity, this build is the answer. It is built for those who\r\nwant top-tier performance in both gaming and creative workloads, this build\r\ncombines AMD’s 3D V-Cache gaming king with NVIDIA’s powerful new RTX 5070\r\nTi, all inside a premium glass chassis. It’s equally comfortable handling\r\nAAA gaming, 3D rendering, and heavy multitasking, while staying cool and\r\nupgrade-ready for the future.\r\n\r\nAMD Ryzen 7 7800X3D – ₹39,000\r\n\r\nThe 7800X3d is hands down the undisputed gaming king in AMD’s lineup, it\r\nfeatures 3D V-Cache for ultra-low latency and phenomenal gaming\r\nperformance. With 8 cores and 16 threads, it’s not just great for gaming,\r\nit’s also excellent for creative workloads like After Effects, 3D modeling,\r\nand DaVinci Resolve. All this with superb power efficiency.\r\n\r\nGigabyte GeForce RTX 5070 Ti Windforce OC 12GB – ₹87,000\r\n A DLSS 4-capable beast. The RTX 5070 Ti brings exceptional 1440p Ultra\r\nand light 4K gaming performance, paired with the efficiency of the Ada\r\nLovelace architecture. Ideal for ray tracing, high refresh rates, and\r\nGPU-intensive software like Blender, Resolve, and Unreal Engine.\r\n”,
“metadata”: {
“delivered-to”: “[email protected]”,
“received-spf”: “pass (zohomail.in: domain of _spf.google.com designates 209.85.216.53 as permitted sender) client-ip=209.85.216.53; [email protected]; helo=mail-pj1-f53.google.com;”,
“authentication-results”: “mx.zohomail.in;\tdkim=pass;\tspf=pass (zohomail.in: domain of _spf.google.com designates 209.85.216.53 as permitted sender) [email protected];\tdmarc=pass(p=none dis=none) header.from=gmail.com”,
“arc-seal”: “i=1; a=rsa-sha256; t=1753168172; cv=none; \td=zohomail.in; s=zohoarc; \tb=Of87hBo0wCbVJpsg9kDPP0KuqLO9wBZ6NtQ8iQDBNySCftkwRNBma81r60hjbLgL0UJ+sk7e+HrtVQO46FP52xaNlgja7zXCPyNkFyaArP/poOs0I1QhKQ0WwRiOEYaEyrmXGqUMp09+6509SfJwZVDNnWxQU9+a5UoXT3ZkL4I=”,
“arc-message-signature”: “i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; \tt=1753168172; h=Content-Type:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To:Cc; \tbh=zb2+AfbvuCsPn8eSwOf7dJA9YRDwZK/uMnOel6F09jU=; \tb=NEVx9VolXfufXgXpvKOwxQLwORtyI3UXcuFq8pknDCYUOI1xDq7xsKKPOr51WN3CmXf+0jKPoiOIHpjmj8SiP2BqtsS4BqU0i5eQbWXitvPdAZyjEBg1opMYe+2sekX5FI+/0V7XTJlLzmlJIZVY4V40oQChmANjBM5k05mGA/c=”,
“arc-authentication-results”: “i=1; mx.zohomail.in;\tdkim=pass;\tspf=pass (zohomail.in: domain of _spf.google.com designates 209.85.216.53 as permitted sender) [email protected];\tdmarc=pass header.from=[email protected] (p=none dis=none)”,
“return-path”: “[email protected]”,
“received”: “from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by mx.zoho.in\twith SMTPS id 1753168172561950.863534724401; Tue, 22 Jul 2025 12:39:32 +0530 (IST)”,
“dkim-signature”: “v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753168169; x=1753772969; darn=pixrockvfx.com; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=zb2+AfbvuCsPn8eSwOf7dJA9YRDwZK/uMnOel6F09jU=; b=ZFu3LF8SV8m8WcFle91coUy+Pe/RvuO3BxlIV9xtv4FF+s5yj2BcqkOF8noOj/NDTE 2Ucf/lvhjP4vIoKEBIyKt/EVUzAIgyLD811irhr95i/6eEP0890dR4ry3HfMV5qg6xZU Ze21P43w3cj47CkmXV1KPwo2N3nyI9wItQ+MRpWfAWeTd9vAxeUTol7GVmr8cmCg0l4B nTzW9XM5tLY5108u9cY9O5pB1FdrMS6wPO3r/3Q/YFeNRKdcFV2sLXfQrnYzjJB+9HPT hkVKbc826O7Wdoiw20iN5FOhjpeCB5ym+IiI2FINahr1scICRq9udxtlYZOiG1mrcvFn II6w==”,
“x-google-dkim-signature”: “v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753168169; x=1753772969; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zb2+AfbvuCsPn8eSwOf7dJA9YRDwZK/uMnOel6F09jU=; b=di59GvEf/fWsASkXHRcttmoOqUPRP6/VZJuFxzZRi5k+WYIHnzmIRHyYMtmZm9K/Ox 7CK/9GRJHAlEthmdsmi4wJydF9Zw7rq7b8xeRxbafWVtq/oujUzZkXtFvYLC8P9Vg6u+ DRfWkj9SFsbnYSreLHlEk5/pejeQXuoPxRkq/tVFy3heal+38UJIyHGkFYN3wTb+fSTN OlxkG5ntr8oxbDDsX1zoVh5//M9Amby37VABJuLqWRn3sL2cgAIYBrghpUWaxJwbnx31 1m5F6TCSkCHzQamYVy+TzdU4KcvKd2iEHdRjv6Jys+YSj9mz+QI7l4mAIsNsns0g8p/3 yS0A==”,
“x-gm-message-state”: “AOJu0YzZoEguJi9ADAYXtlbBt2WaYDOwPg1lJInG2l/5Iwye8UBf4C3w\tMxxf6aj6b2REQo7TjxfnaGT9RZnrb4vbEToNtvilfMo5dwlERriF/9SS+SYfkprau6woFTl70W1\tYwTXTQRJ4WR5PMTfUL+v7TLyFwF3KcgRYWbw4”,
“x-gm-gg”: “ASbGncuC3DNzakVMbLUN9x7O5COVK57RxutBZg1yEUiBfS6tal7qRQ1R+xssyRRjMoZ\t/3MNQkjP9zERVF+ul11sn1EuzHLotXExXFOmb+4N/ZTO7W8TmGufk6O4mzdzFqoNZL2EjD6lJYl\tFXtJRu6R2ksQx4/5VIy4+mrvUZ07oNpi1n+zJPyJs3NWTu7wxcXYgx41J+//QwxKblxdPW7a+tZ\tliMfBnci122awiTS8A=”,
“x-google-smtp-source”: “AGHT+IHKZ5HB/bFzxOPJEsuReepV2kt8MP8jvERG4BrqL4s1Cz2ssP1bqQ644W+u+bsI336a0l15uxRoNpnP5YJtAeg=”,
“x-received”: “by 2002:a17:90b:2ecb:b0:312:daf3:bac9 with SMTP id 98e67ed59e1d1-31c9e793822mr27143549a91.34.1753168168995; Tue, 22 Jul 2025 00:09:28 -0700 (PDT)”,
“mime-version”: “1.0”,
“references”: “CA+xJiV8cY1__U30rE-r=xnmr5wsifM1BQyaN4zu8ycue5BTGJw@mail.gmail.com”,
“in-reply-to”: “CA+xJiV8cY1__U30rE-r=xnmr5wsifM1BQyaN4zu8ycue5BTGJw@mail.gmail.com”,
“x-gm-features”: “Ac12FXxsUe9Xx-8ZF0xA_noAibN1NW9O0vxNoiufqdoyWHwVrdSrKPrsDu3Kg5k”,
“message-id”: “CA+xJiV_6g8LWz=Sf4H10QdwybT7k8i89Aad12KKEww+kSZKtxQ@mail.gmail.com”,
“content-type”: “multipart/alternative; boundary="000000000000f1d1ac063a7f45e7"”,
“x-zohomail-dkim”: “pass (identity @gmail.com)”,
“x-zm-messageid”: “1753168173376110500”
},
“attributes”: {
“uid”: 7439
},
“from”: “IT PIXROCKVFX [email protected]”,
“date”: “Tue, 22 Jul 2025 12:38:53 +0530”,
“subject”: “Fwd: Best gaming PC build under ₹1,65,000 in July 2025 | Write”,
“to”: “[email protected]
}
]

What I’ve Already Tried:

I have worked with an AI assistant to try and fix this in a Code node, but nothing has worked. The cleanedText field in the output is always the same as the original broken textPlain. We have tried:

  1. Re-encoding from latin1: Using Buffer.from(rawText, 'latin1').toString('utf8');.
  2. Re-encoding from windows-1252: This produced an “Unknown encoding” error, suggesting my n8n environment is missing some encoding libraries.
  3. Targeted String Replacement: Using .replace(/â€TM/g, "’"); and other variations. This did nothing, proving the characters in the string are not what they appear to be.
  4. Rebuilding from HTML: A multi-stage script that used textHtml, decoded HTML entities, stripped all tags, and then ran the targeted replacements. This also failed to change the text.

The problem seems to be in how the IMAP node is parsing the email body at a very low level. Any help or insight from the community would be greatly appreciated.

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