The IMAP node encoding

The imap node does something with the encoding. Here is the full text of the letter with technical fields:

Received: from mail-collectors-ext-production-main-66.klg.yp-c.yandex.net (mail-collectors-ext-production-main-66.klg.yp-c.yandex.net [2a02:6b8:c42:dda8:0:5e2b:7654:0])
by mail-nwsmtp-mxback-production-main-76.klg.yp-c.yandex.net (mxback/Yandex) with HTTP id RbNSYj9L64Y0-y05P3QKh
for [email protected]; Fri, 28 Mar 2025 11:37:27 +0300
X-Yandex-Fwd: 2
Received: from [email protected] ([77.88.21.125])
by mail.yandex.ru with POP3 id QbNNNQ3LkuQ0
for 1130000042718651@2760933; Fri, 28 Mar 2025 11:37:27 +0300
X-yandex-pop-server: imap.yandex.ru
X-yandex-rpop-id: 2760933
X-yandex-rpop-info: [email protected]
X-yandex-rpop-foldername: SU5CT1g=
Received: from postback17b.mail.yandex.net (postback17b.mail.yandex.net [2a02:6b8:c02:900:1:45:d181:da17])
by mail-notsolitesrv-production-main-87.sas.yp-c.yandex.net (notsolitesrv/Yandex) with LMTPS id wlxx7ShWAl5I-vG302R7a;
Fri, 28 Mar 2025 11:36:00 +0300
Received: from mail-nwsmtp-smtp-production-main-63.sas.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-63.sas.yp-c.yandex.net [IPv6:2a02:6b8:c37:7da2:0:640:6456:0])
by postback17b.mail.yandex.net (Yandex) with ESMTPS id 6523060BB2;
Fri, 28 Mar 2025 11:36:00 +0300 (MSK)
Received: by mail-nwsmtp-smtp-production-main-63.sas.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id qZNs7mPLduQ0-zeb3b7yW;
Fri, 28 Mar 2025 11:36:00 +0300
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail;
t=1743150960; bh=Z5fcybnqI61Om0MK6MuFAKA3pmWte1Rou5SCuRnLkRA=;
h=Subject:Message-ID:Reply-To:To:From:Date;
b=YoDy6v8vRQL5SV/OJEv/zG108KCSQ/vZEz14UTNx5voMh6qeU+LlmB6rAa9oHfn4N
aRRLlhbT+fI9BfkwU8keiHB7eG063eSP940MqdXqVtAY1nhH+7um5l7FFvSFVNFO5G
pA9hUj7ipu+ELr8SxHX15b81x9NaIvqZYNVGfyUo=
Date: Fri, 28 Mar 2025 11:34:45 +0300
From: =?windows-1251?B?yvDl78ru7O/r5ery?= [email protected]
Reply-To: =?windows-1251?B?yvDl78ru7O/r5ery?= [email protected]
Organization: =?windows-1251?B?zs7OICLK8OXvyu7s7+vl6vIi?=
X-Priority: 3 (Normal)
Message-ID: [email protected]
To: =?windows-1251?B?0eXw4+Xp?= [email protected]
Subject: =?windows-1251?B?x+Dv8O7xIO3gIOjn4+7y7uLr5e3o5SDj4Onq6CDv7iDDztHSIDI1MjYtNzA=?=
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1251
Content-Transfer-Encoding: 8bit
X-Yandex-Forward: 3c7a78b1979ee567785fafe659fc4605

Добрый день.

Гайка М27 ГОСТ 2526-70, ст.12Х18Н10Т - 50 штук

С уважением, Владимир


Компания “КрепКомплект™”
(812) 363-34-09, 938-00-12

The IMAP node receives such data:
[
{
“textHtml”: “”,
“textPlain”: “������ ����.\r\n\r\n ����� �27 ���� 2526-70, ��.12�18�10� - 50 ����\r\n \r\n\r\n� ���������, ��������\r\n\r\n–\r\n�������� "������������"\r\n(812) 363-34-09, 938-00-12\r\n\r\n”,
“metadata”: {
“received”: “from mail-collectors-ext-production-main-66.klg.yp-c.yandex.net (mail-collectors-ext-production-main-66.klg.yp-c.yandex.net [2a02:6b8:c42:dda8:0:5e2b:7654:0])\tby mail-nwsmtp-mxback-production-main-76.klg.yp-c.yandex.net (mxback/Yandex) with HTTP id RbNSYj9L64Y0-y05P3QKh\tfor [email protected]; Fri, 28 Mar 2025 11:37:27 +0300”,
“x-yandex-fwd”: “2”,
“x-yandex-pop-server”: “imap.yandex.ru”,
“x-yandex-rpop-id”: “2760933”,
“x-yandex-rpop-info”: “[email protected]”,
“x-yandex-rpop-foldername”: “SU5CT1g=”,
“dkim-signature”: “v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail;\tt=1743150960; bh=Z5fcybnqI61Om0MK6MuFAKA3pmWte1Rou5SCuRnLkRA=;\th=Subject:Message-ID:Reply-To:To:From:Date;\tb=YoDy6v8vRQL5SV/OJEv/zG108KCSQ/vZEz14UTNx5voMh6qeU+LlmB6rAa9oHfn4N\t aRRLlhbT+fI9BfkwU8keiHB7eG063eSP940MqdXqVtAY1nhH+7um5l7FFvSFVNFO5G\t pA9hUj7ipu+ELr8SxHX15b81x9NaIvqZYNVGfyUo=”,
“reply-to”: “КрепКомплект [email protected]”,
“organization”: “ООО "КрепКомплект"”,
“x-priority”: “3 (Normal)”,
“message-id”: “[email protected]”,
“mime-version”: “1.0”,
“content-type”: “text/plain; charset=windows-1251”,
“content-transfer-encoding”: “8bit”,
“x-yandex-forward”: “3c7a78b1979ee567785fafe659fc4605”
},
“date”: “Fri, 28 Mar 2025 11:34:45 +0300”,
“from”: “КрепКомплект [email protected]”,
“to”: “Сергей [email protected]”,
“subject”: “Запрос на изготовление гайки по ГОСТ 2526-70”
}
]
Thus, such text: "Добрый день.
Гайка М27 ГОСТ 2526-70, ст.12Х18Н10Т - 50 штук
С уважением, Владимир

Компания “КрепКомплект™”
(812) 363-34-09, 938-00-12"
turns into this:
“������ ����.\r\n\r\n ����� �27 ���� 2526-70, ��.12�18�10� - 50 ����\r\n \r\n\r\n� ���������, ��������\r\n\r\n–\r\n�������� "������������"\r\n(812) 363-34-09, 938-00-12\r\n\r\n”

How can this be fixed?

Share the output returned by the last node

Information on your n8n setup

  • **n8n version: 1.80.3
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • **Operating system: Ubuntu 22.04

Try re-encode this with a code node after the fact, something like:

for (const item of items) {
  if (item.json.textPlain) {
    let charset = 'windows-1251';
    
    if (item.json.metadata && item.json.metadata['content-type']) {
      const contentType = item.json.metadata['content-type'];
      const charsetMatch = contentType.match(/charset=([^;]+)/i);
      if (charsetMatch) {
        charset = charsetMatch[1].trim();
      }
    }
    
    // Convert using Node.js built-in Buffer capabilities
    try {
      // Create a buffer from the binary data
      const buffer = Buffer.from(item.json.textPlain, 'binary');
      // Decode using TextDecoder (built into Node.js)
      const decoder = new TextDecoder(charset);
      item.json.textPlain = decoder.decode(buffer);
    } catch (error) {
      // If decoding fails, add an error field but keep original data
      item.json.decodingError = error.message;
    }
  }
  
  return item;
}

Here is a question about the work of the IMAP node. It immediately gives the wrong encoding. Then it is impossible to decode, I tried different methods.

Have you tried something like the above? What are the results/errors?

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