From c110fb07d6cbd12bba6518aff2813bb27c881f74 Mon Sep 17 00:00:00 2001 From: Mat Sz Date: Thu, 11 Jan 2024 18:21:55 +0100 Subject: [PATCH] fix mime words in address headers (fixes #14) --- __tests__/extract.test.ts | 34 ++++++++++++++++++++++++++++++++-- src/extractor.ts | 7 +++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/__tests__/extract.test.ts b/__tests__/extract.test.ts index 732c569..988a9fb 100644 --- a/__tests__/extract.test.ts +++ b/__tests__/extract.test.ts @@ -16,13 +16,13 @@ Some message.`); from: { name: 'A', address: 'a@example.com', - raw: 'A ', + raw: '"A" ', }, to: [ { name: 'B', address: 'b@example.com', - raw: 'B ', + raw: '"B" ', }, ], subject: 'Hello world!', @@ -138,4 +138,34 @@ iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI ); expect(output.attachments?.[0]?.filename).toBe('test.png'); }); + + // https://github.com/mat-sz/letterparser/issues/14 + it('should handle mime words in from/to headers', () => { + const output = + extract(`Subject: =?utf-8?Q?Cancelled_Reservation_-_Automation_-_C's_Location;_Sensors;_Sensor2_-_CEDITtest?= +To: "=?utf-8?Q?qaautomation@xxxystemsdev=2Eonmicrosoft=2Ecom?=" +From: "=?utf-8?Q?sgmh@xxx=2Elocal?=" +Mime-Version: 1.0 +Content-Type: text/plain; charset=utf-8 + +Some message.`); + + expect(output).toMatchObject({ + text: 'Some message.', + from: { + name: 'sgmh@xxx.local', + address: 'sgmh@xxx.local', + raw: '"sgmh@xxx.local" ', + }, + to: [ + { + name: 'qaautomation@xxxystemsdev.onmicrosoft.com', + address: 'qaautomation@xxxystemsdev.onmicrosoft.com', + raw: '"qaautomation@xxxystemsdev.onmicrosoft.com" ', + }, + ], + subject: + "Cancelled Reservation - Automation - C's Location; Sensors; Sensor2 - CEDITtest", + }); + }); }); diff --git a/src/extractor.ts b/src/extractor.ts index b4ea85d..890770a 100644 --- a/src/extractor.ts +++ b/src/extractor.ts @@ -1,3 +1,4 @@ +import { decodeMimeWords } from 'lettercoder'; import { unquote } from './helpers.js'; import { LetterparserNode, @@ -106,11 +107,13 @@ function extractMailbox(raw: string): LetterparserMailbox { const addressEnd = raw.lastIndexOf('>'); if (addressStart !== -1 && addressEnd !== -1) { const address = unquote(raw.substring(addressStart + 1, addressEnd).trim()); - let name = unquote(raw.substring(0, addressStart).trim()); + const name = decodeMimeWords( + unquote(raw.substring(0, addressStart).trim()) + ); return { address, name, - raw, + raw: `"${name}" <${address}>`, }; } else { return {