From a0209539a4630359fe51ecdbee1f6789d294c2a9 Mon Sep 17 00:00:00 2001 From: Aero Mail Dev Date: Fri, 17 Apr 2026 05:00:11 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20HTML-=D0=BF=D0=B8=D1=81=D1=8C=D0=BC?= =?UTF-8?q?=D0=B0=20=D1=86=D0=B8=D1=82=D0=B8=D1=80=D1=83=D1=8E=D1=82=D1=81?= =?UTF-8?q?=D1=8F=20=D0=B8=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D1=8E=D1=82=D1=81=D1=8F=20=D0=BA=D0=B0=D0=BA=20multipart?= =?UTF-8?q?/alternative?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Проблема: ответы на HTML-письма всегда уходили как text/plain, получатели видели голый текст без форматирования оригинала. Решение: - ComposeData.htmlBody — хранит HTML-блокквоту оригинального письма - quoteBodyHtml() в compose.store строит
из body.html с атрибуцией (От/Дата/Тема) при ответе и пересылке - buildHtmlBody() в imap.provider собирает полный HTML: текст пользователя (через textToHtml) + htmlBody блокквота - sendMessage() и buildRawMessage() передают html: если htmlBody задан, nodemailer шлёт multipart/alternative (text/plain + text/html) Для plain-text оригиналов htmlBody = undefined → поведение не меняется. Co-Authored-By: Claude Sonnet 4.6 --- src/main/providers/imap.provider.ts | 32 ++++++++++++++++++++++ src/renderer/src/stores/compose.store.ts | 34 ++++++++++++++++++++++-- src/shared/types/message.ts | 3 ++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/main/providers/imap.provider.ts b/src/main/providers/imap.provider.ts index 43f7e7e..42a48ea 100644 --- a/src/main/providers/imap.provider.ts +++ b/src/main/providers/imap.provider.ts @@ -319,6 +319,7 @@ export class ImapProvider implements IEmailProvider { bcc: compose.bcc.length ? compose.bcc.join(', ') : undefined, subject: compose.subject, text: compose.body, + html: compose.htmlBody ? this.buildHtmlBody(compose) : undefined, inReplyTo: compose.inReplyTo, references: compose.references, attachments: compose.attachments?.map((a) => ({ path: a.path, filename: a.filename })) ?? [] @@ -367,6 +368,7 @@ export class ImapProvider implements IEmailProvider { bcc: compose.bcc.length ? compose.bcc.join(', ') : undefined, subject: compose.subject, text: compose.body, + html: compose.htmlBody ? this.buildHtmlBody(compose) : undefined, messageId, inReplyTo: compose.inReplyTo, references: compose.references, @@ -625,6 +627,36 @@ export class ImapProvider implements IEmailProvider { } } + /** + * Строит полный HTML-body письма: текст пользователя (→ HTML) + HTML-блокквота оригинала. + * compose.htmlBody содержит только блокквоту; пользовательский текст берём из compose.body + * до разделителя "--- Оригинальное письмо ---". + */ + private buildHtmlBody(compose: ComposeData): string { + const SEPARATOR = '\n\n--- Оригинальное письмо ---' + const replyText = compose.body.includes(SEPARATOR) + ? compose.body.split(SEPARATOR)[0].trim() + : compose.body.trim() + + return ImapProvider.textToHtml(replyText) + (compose.htmlBody ?? '') + } + + /** + * Конвертирует plain text в минимальный HTML (параграфы + переносы строк). + */ + private static textToHtml(text: string): string { + if (!text) return '' + const escaped = text + .replace(/&/g, '&') + .replace(//g, '>') + const paragraphs = escaped.split(/\n\n+/).filter((p) => p.trim()) + if (paragraphs.length === 0) return '

' + return paragraphs + .map((p) => `

${p.replace(/\n/g, '
')}

`) + .join('\n') + } + /** * Ищет имя отправителя в последнем письме папки Sent. * Возвращает display name из заголовка From или null если Sent пуста / не найдена. diff --git a/src/renderer/src/stores/compose.store.ts b/src/renderer/src/stores/compose.store.ts index 3a4a180..506c520 100644 --- a/src/renderer/src/stores/compose.store.ts +++ b/src/renderer/src/stores/compose.store.ts @@ -31,12 +31,16 @@ function htmlToText(html: string): string { .trim() } +function escHtml(s: string): string { + return s.replace(/&/g, '&').replace(//g, '>') +} + +/** Plain-text версия цитаты (для text/plain части письма) */ function quoteBody(envelope: MessageEnvelope, body: MessageBody | null): string { const date = envelope.date ? new Date(envelope.date).toLocaleString('ru') : '' const from = envelope.from.name ? `${envelope.from.name} <${envelope.from.email}>` : envelope.from.email - // body.text проставляется в main-процессе; fallback на HTML если вдруг null const rawText = body?.text ?? (body?.html ? htmlToText(body.html) : '') const lines = rawText ? rawText.split('\n').map((l) => `> ${l}`).join('\n') @@ -44,6 +48,30 @@ function quoteBody(envelope: MessageEnvelope, body: MessageBody | null): string return `\n\n--- Оригинальное письмо ---\nОт: ${from}\nДата: ${date}\nТема: ${envelope.subject}\n\n${lines}` } +/** + * HTML-блокквота оригинала (для text/html части письма). + * Возвращает undefined если оригинал — plain-text (тогда шлём только text/plain). + */ +function quoteBodyHtml(envelope: MessageEnvelope, body: MessageBody | null): string | undefined { + if (!body?.html) return undefined + const date = envelope.date ? new Date(envelope.date).toLocaleString('ru') : '' + const from = envelope.from.name + ? `${escHtml(envelope.from.name)} <${escHtml(envelope.from.email)}>` + : escHtml(envelope.from.email) + return ( + `
` + + `
` + + `— Оригинальное письмо —
` + + `От: ${from}
` + + `Дата: ${escHtml(date)}
` + + `Тема: ${escHtml(envelope.subject)}` + + `
` + + `
` + + body.html + + `
` + ) +} + export const useComposeStore = defineStore('compose', () => { const { invoke } = useIpc() const accountsStore = useAccountsStore() @@ -87,6 +115,7 @@ export const useComposeStore = defineStore('compose', () => { bcc: [], subject: envelope.subject.startsWith('Re:') ? envelope.subject : `Re: ${envelope.subject}`, body: quoteBody(envelope, body), + htmlBody: quoteBodyHtml(envelope, body), inReplyTo: envelope.messageId, // RFC 2822: References = родительские references + messageId родителя // body.references содержит полную цепочку, полученную при загрузке тела @@ -107,7 +136,8 @@ export const useComposeStore = defineStore('compose', () => { mode: 'forward', to: [], cc: [], bcc: [], subject: envelope.subject.startsWith('Fwd:') ? envelope.subject : `Fwd: ${envelope.subject}`, - body: quoteBody(envelope, body) + body: quoteBody(envelope, body), + htmlBody: quoteBodyHtml(envelope, body) } invoke('compose:open', session) } diff --git a/src/shared/types/message.ts b/src/shared/types/message.ts index 0ed6222..3184dcf 100644 --- a/src/shared/types/message.ts +++ b/src/shared/types/message.ts @@ -58,7 +58,8 @@ export interface ComposeData { cc: string[] bcc: string[] subject: string - body: string // plain text + body: string // plain text (полная версия: текст пользователя + текстовая цитата) + htmlBody?: string // HTML-блокквота оригинала; если задана — шлём multipart/alternative attachments?: ComposeAttachment[] inReplyTo?: string // Message-ID оригинала (для ответа) references?: string // References header