Skip to content

Commit

Permalink
fix mime words in address headers (fixes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-sz committed Jan 11, 2024
1 parent b2840f4 commit c110fb0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
34 changes: 32 additions & 2 deletions __tests__/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Some message.`);
from: {
name: 'A',
address: '[email protected]',
raw: 'A <[email protected]>',
raw: '"A" <[email protected]>',
},
to: [
{
name: 'B',
address: '[email protected]',
raw: 'B <[email protected]>',
raw: '"B" <[email protected]>',
},
],
subject: 'Hello world!',
Expand Down Expand Up @@ -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?=" <[email protected]>
From: "=?utf-8?Q?sgmh@xxx=2Elocal?=" <[email protected]>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Some message.`);

expect(output).toMatchObject({
text: 'Some message.',
from: {
name: '[email protected]',
address: '[email protected]',
raw: '"[email protected]" <[email protected]>',
},
to: [
{
name: '[email protected]',
address: '[email protected]',
raw: '"[email protected]" <[email protected]>',
},
],
subject:
"Cancelled Reservation - Automation - C's Location; Sensors; Sensor2 - CEDITtest",
});
});
});
7 changes: 5 additions & 2 deletions src/extractor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { decodeMimeWords } from 'lettercoder';
import { unquote } from './helpers.js';
import {
LetterparserNode,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c110fb0

Please sign in to comment.