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