Preserve QOutlook threading headers in EML

This commit is contained in:
Курнат Андрей
2026-06-01 19:56:44 +03:00
parent 0a75ec7b1a
commit 9bd8827549
5 changed files with 69 additions and 3 deletions
+28 -1
View File
@@ -177,6 +177,9 @@ describe("outlookMail helpers", () => {
"Cc: copy@example.com",
"Bcc: hidden@example.com",
"Reply-To: support@example.com",
"Message-ID: <message-1@example.com>",
"In-Reply-To: <parent@example.com>",
"References: <root@example.com> <parent@example.com>",
"Subject: =?UTF-8?B?0JzRg9C70YzRgtC40L/QsNGA0YI=?=",
"Date: Mon, 01 Jun 2026 10:00:00 +0300",
'Content-Type: multipart/alternative; boundary="alt"',
@@ -202,6 +205,9 @@ describe("outlookMail helpers", () => {
expect(imported.cc).toBe("copy@example.com");
expect(imported.bcc).toBe("hidden@example.com");
expect(imported.replyTo).toBe("support@example.com");
expect(imported.messageId).toBe("<message-1@example.com>");
expect(imported.inReplyTo).toBe("<parent@example.com>");
expect(imported.references).toBe("<root@example.com> <parent@example.com>");
});
it("импортирует multipart/mixed EML с вложением", async () => {
@@ -251,7 +257,13 @@ describe("outlookMail helpers", () => {
{
senderSmtpAddress: "sender@example.com",
subject: "MSG отчет",
headers: "Reply-To: support@example.com\r\nFrom: sender@example.com",
headers: [
"Reply-To: support@example.com",
"Message-ID: <msg-1@example.com>",
"In-Reply-To: <msg-parent@example.com>",
"References: <msg-root@example.com> <msg-parent@example.com>",
"From: sender@example.com"
].join("\r\n"),
bodyHtml: "<p>HTML&nbsp;текст</p>",
messageDeliveryTime: "Mon, 01 Jun 2026 10:00:00 GMT",
recipients: [
@@ -270,6 +282,9 @@ describe("outlookMail helpers", () => {
cc: "copy@example.com",
bcc: "hidden@example.com",
replyTo: "support@example.com",
messageId: "<msg-1@example.com>",
inReplyTo: "<msg-parent@example.com>",
references: "<msg-root@example.com> <msg-parent@example.com>",
subject: "MSG отчет",
body: "HTML текст",
time: "Mon, 01 Jun 2026 10:00:00 GMT",
@@ -299,6 +314,9 @@ describe("outlookMail helpers", () => {
cc: "copy@example.com",
bcc: "hidden@example.com",
replyTo: "support@example.com",
messageId: "<message-1@example.com>",
inReplyTo: "<parent@example.com>",
references: "<root@example.com> <parent@example.com>",
subject: "Тема",
body: "Текст",
time: "Mon, 01 Jan 2024 10:00:00 +0000",
@@ -312,6 +330,9 @@ describe("outlookMail helpers", () => {
expect(eml).toContain("Cc: copy@example.com");
expect(eml).toContain("Bcc: hidden@example.com");
expect(eml).toContain("Reply-To: support@example.com");
expect(eml).toContain("Message-ID: <message-1@example.com>");
expect(eml).toContain("In-Reply-To: <parent@example.com>");
expect(eml).toContain("References: <root@example.com> <parent@example.com>");
expect(eml).toContain("Subject: =?UTF-8?B?");
expect(eml.endsWith("Текст")).toBe(true);
});
@@ -325,6 +346,9 @@ describe("outlookMail helpers", () => {
cc: "copy@example.com",
bcc: "hidden@example.com",
replyTo: "support@example.com",
messageId: "<message-1@example.com>",
inReplyTo: "<parent@example.com>",
references: "<root@example.com> <parent@example.com>",
subject: "Тема",
body: "Текст",
time: "Mon, 01 Jan 2024 10:00:00 +0000",
@@ -350,6 +374,9 @@ describe("outlookMail helpers", () => {
expect(eml).toContain("Cc: copy@example.com");
expect(eml).toContain("Bcc: hidden@example.com");
expect(eml).toContain("Reply-To: support@example.com");
expect(eml).toContain("Message-ID: <message-1@example.com>");
expect(eml).toContain("In-Reply-To: <parent@example.com>");
expect(eml).toContain("References: <root@example.com> <parent@example.com>");
expect(eml).toContain("filename*=utf-8''%D0%BE%D1%82%D1%87%D0%B5%D1%82%2E%74%78%74");
expect(extractMessageBody(parsed.headers, parsed.body)).toBe("Текст");
expect(attachments[0]).toMatchObject({
+24
View File
@@ -90,6 +90,7 @@ export async function importEmlFile(file: File): Promise<ImportedMailMessage> {
...(parsed.headers.cc ? { cc: decodeMimeWords(parsed.headers.cc) } : {}),
...(parsed.headers.bcc ? { bcc: decodeMimeWords(parsed.headers.bcc) } : {}),
...(parsed.headers["reply-to"] ? { replyTo: decodeMimeWords(parsed.headers["reply-to"]) } : {}),
...mailThreadHeaders(parsed.headers),
subject: decodeMimeWords(parsed.headers.subject || "Без темы"),
body: extractMessageBody(parsed.headers, parsed.body),
time: parsed.headers.date || new Date().toLocaleString("ru-RU"),
@@ -127,6 +128,7 @@ export function msgDataToMailMessage(reader: Pick<MsgReaderInstance, "getAttachm
...(cc ? { cc } : {}),
...(bcc ? { bcc } : {}),
...(replyTo ? { replyTo } : {}),
...mailThreadHeaders(headerFields),
subject: firstText(data.subject, decodeMimeWords(headerFields.subject || ""), "Без темы"),
body: msgBodyText(data),
time: firstText(data.messageDeliveryTime, data.clientSubmitTime, data.creationTime, data.lastModificationTime, headerFields.date, new Date().toLocaleString("ru-RU")),
@@ -421,6 +423,7 @@ export function buildEmlSource(message: MailMessage): string {
...optionalAddressHeaders(message),
`Subject: ${encodeHeaderValue(message.subject || "Без темы")}`,
`Date: ${sanitizeHeaderValue(message.time || new Date().toUTCString())}`,
...optionalThreadHeaders(message),
"MIME-Version: 1.0",
`Content-Type: multipart/mixed; boundary="${boundary}"`,
"",
@@ -443,6 +446,7 @@ export function buildEmlSource(message: MailMessage): string {
...optionalAddressHeaders(message),
`Subject: ${encodeHeaderValue(message.subject || "Без темы")}`,
`Date: ${sanitizeHeaderValue(message.time || new Date().toUTCString())}`,
...optionalThreadHeaders(message),
"MIME-Version: 1.0",
"Content-Type: text/plain; charset=utf-8",
"Content-Transfer-Encoding: 8bit",
@@ -461,6 +465,26 @@ function optionalAddressHeaders(message: MailMessage): string[] {
];
}
function optionalThreadHeaders(message: MailMessage): string[] {
return [
...(message.messageId?.trim() ? [`Message-ID: ${sanitizeHeaderValue(message.messageId)}`] : []),
...(message.inReplyTo?.trim() ? [`In-Reply-To: ${sanitizeHeaderValue(message.inReplyTo)}`] : []),
...(message.references?.trim() ? [`References: ${sanitizeHeaderValue(message.references)}`] : [])
];
}
function mailThreadHeaders(headers: MailHeaders): Pick<MailMessage, "messageId" | "inReplyTo" | "references"> {
const messageId = sanitizeHeaderValue(headers["message-id"] || "");
const inReplyTo = sanitizeHeaderValue(headers["in-reply-to"] || "");
const references = sanitizeHeaderValue(headers.references || "");
return {
...(messageId ? { messageId } : {}),
...(inReplyTo ? { inReplyTo } : {}),
...(references ? { references } : {})
};
}
export function decodeMimeWords(value: string): string {
return value
.replace(/(=\?[^?]+\?[bqBQ]\?[^?]+\?=)\s+(?==\?[^?]+\?[bqBQ]\?[^?]+\?=)/g, "$1")