feat(email-to-pdf): add inline images, clickable links, and embedded attachments
- Add CID inline image support via base64 data URI replacement - Implement clickable link extraction from HTML anchors using regex - Embed email attachments into PDF using pymupdf embfile_add - Reduce font sizes for more compact PDF output (18px subject, 12px body) - Format date with timezone (UTC+HH:MM) while preserving original time - Clean email address formatting (Name (email) instead of <brackets>) - Add UI options: page size selector, CC/BCC toggle, attachments toggle
This commit is contained in:
268
src/js/logic/email-to-pdf-page.ts
Normal file
268
src/js/logic/email-to-pdf-page.ts
Normal file
@@ -0,0 +1,268 @@
|
||||
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
||||
import { downloadFile, formatBytes } from '../utils/helpers.js';
|
||||
import { state } from '../state.js';
|
||||
import { createIcons, icons } from 'lucide';
|
||||
import { parseEmailFile, renderEmailToHtml } from './email-to-pdf.js';
|
||||
import { PyMuPDF } from '@bentopdf/pymupdf-wasm';
|
||||
import { getWasmBaseUrl } from '../config/wasm-cdn-config.js';
|
||||
|
||||
const EXTENSIONS = ['.eml', '.msg'];
|
||||
const TOOL_NAME = 'Email';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
||||
const dropZone = document.getElementById('drop-zone');
|
||||
const processBtn = document.getElementById('process-btn');
|
||||
const fileDisplayArea = document.getElementById('file-display-area');
|
||||
const fileControls = document.getElementById('file-controls');
|
||||
const addMoreBtn = document.getElementById('add-more-btn');
|
||||
const clearFilesBtn = document.getElementById('clear-files-btn');
|
||||
const backBtn = document.getElementById('back-to-tools');
|
||||
|
||||
if (backBtn) {
|
||||
backBtn.addEventListener('click', () => {
|
||||
window.location.href = import.meta.env.BASE_URL;
|
||||
});
|
||||
}
|
||||
|
||||
const convertOptions = document.getElementById('convert-options');
|
||||
|
||||
const updateUI = async () => {
|
||||
if (!fileDisplayArea || !processBtn || !fileControls) return;
|
||||
|
||||
if (state.files.length > 0) {
|
||||
fileDisplayArea.innerHTML = '';
|
||||
|
||||
for (let index = 0; index < state.files.length; index++) {
|
||||
const file = state.files[index];
|
||||
const fileDiv = document.createElement('div');
|
||||
fileDiv.className =
|
||||
'flex items-center justify-between bg-gray-700 p-3 rounded-lg text-sm';
|
||||
|
||||
const infoContainer = document.createElement('div');
|
||||
infoContainer.className = 'flex flex-col overflow-hidden';
|
||||
|
||||
const nameSpan = document.createElement('div');
|
||||
nameSpan.className = 'truncate font-medium text-gray-200 text-sm mb-1';
|
||||
nameSpan.textContent = file.name;
|
||||
|
||||
const metaSpan = document.createElement('div');
|
||||
metaSpan.className = 'text-xs text-gray-400';
|
||||
metaSpan.textContent = formatBytes(file.size);
|
||||
|
||||
infoContainer.append(nameSpan, metaSpan);
|
||||
|
||||
const removeBtn = document.createElement('button');
|
||||
removeBtn.className =
|
||||
'ml-4 text-red-400 hover:text-red-300 flex-shrink-0';
|
||||
removeBtn.innerHTML = '<i data-lucide="trash-2" class="w-4 h-4"></i>';
|
||||
removeBtn.onclick = () => {
|
||||
state.files = state.files.filter((_, i) => i !== index);
|
||||
updateUI();
|
||||
};
|
||||
|
||||
fileDiv.append(infoContainer, removeBtn);
|
||||
fileDisplayArea.appendChild(fileDiv);
|
||||
}
|
||||
|
||||
createIcons({ icons });
|
||||
fileControls.classList.remove('hidden');
|
||||
if (convertOptions) convertOptions.classList.remove('hidden');
|
||||
(processBtn as HTMLButtonElement).disabled = false;
|
||||
} else {
|
||||
fileDisplayArea.innerHTML = '';
|
||||
fileControls.classList.add('hidden');
|
||||
if (convertOptions) convertOptions.classList.add('hidden');
|
||||
(processBtn as HTMLButtonElement).disabled = true;
|
||||
}
|
||||
};
|
||||
|
||||
const resetState = () => {
|
||||
state.files = [];
|
||||
state.pdfDoc = null;
|
||||
updateUI();
|
||||
};
|
||||
|
||||
const convertToPdf = async () => {
|
||||
try {
|
||||
if (state.files.length === 0) {
|
||||
showAlert(
|
||||
'No Files',
|
||||
`Please select at least one ${TOOL_NAME} file (.eml or .msg).`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const pageSizeSelect = document.getElementById(
|
||||
'page-size'
|
||||
) as HTMLSelectElement;
|
||||
const includeCcBccCheckbox = document.getElementById(
|
||||
'include-cc-bcc'
|
||||
) as HTMLInputElement;
|
||||
const includeAttachmentsCheckbox = document.getElementById(
|
||||
'include-attachments'
|
||||
) as HTMLInputElement;
|
||||
|
||||
const pageSize =
|
||||
(pageSizeSelect?.value as 'a4' | 'letter' | 'legal') || 'a4';
|
||||
const includeCcBcc = includeCcBccCheckbox?.checked ?? true;
|
||||
const includeAttachments = includeAttachmentsCheckbox?.checked ?? true;
|
||||
|
||||
showLoader('Loading PDF engine...');
|
||||
const pymupdf = new PyMuPDF(getWasmBaseUrl('pymupdf'));
|
||||
await pymupdf.load();
|
||||
|
||||
if (state.files.length === 1) {
|
||||
const originalFile = state.files[0];
|
||||
showLoader(`Parsing ${originalFile.name}...`);
|
||||
|
||||
const email = await parseEmailFile(originalFile);
|
||||
|
||||
showLoader('Generating PDF...');
|
||||
const htmlContent = renderEmailToHtml(email, {
|
||||
includeCcBcc,
|
||||
includeAttachments,
|
||||
pageSize,
|
||||
});
|
||||
|
||||
const pdfBlob = await (pymupdf as any).htmlToPdf(htmlContent, {
|
||||
pageSize,
|
||||
margins: { top: 50, right: 50, bottom: 50, left: 50 },
|
||||
attachments: email.attachments
|
||||
.filter((a) => a.content)
|
||||
.map((a) => ({
|
||||
filename: a.filename,
|
||||
content: a.content!,
|
||||
})),
|
||||
});
|
||||
const fileName = originalFile.name.replace(/\.[^.]+$/, '') + '.pdf';
|
||||
|
||||
downloadFile(pdfBlob, fileName);
|
||||
hideLoader();
|
||||
|
||||
showAlert(
|
||||
'Conversion Complete',
|
||||
`Successfully converted ${originalFile.name} to PDF.`,
|
||||
'success',
|
||||
() => resetState()
|
||||
);
|
||||
} else {
|
||||
showLoader('Converting emails...');
|
||||
const JSZip = (await import('jszip')).default;
|
||||
const zip = new JSZip();
|
||||
|
||||
for (let i = 0; i < state.files.length; i++) {
|
||||
const file = state.files[i];
|
||||
showLoader(
|
||||
`Converting ${i + 1}/${state.files.length}: ${file.name}...`
|
||||
);
|
||||
|
||||
try {
|
||||
const email = await parseEmailFile(file);
|
||||
|
||||
const htmlContent = renderEmailToHtml(email, {
|
||||
includeCcBcc,
|
||||
includeAttachments,
|
||||
pageSize,
|
||||
});
|
||||
|
||||
const pdfBlob = await (pymupdf as any).htmlToPdf(htmlContent, {
|
||||
pageSize,
|
||||
margins: { top: 50, right: 50, bottom: 50, left: 50 },
|
||||
attachments: email.attachments
|
||||
.filter((a) => a.content)
|
||||
.map((a) => ({
|
||||
filename: a.filename,
|
||||
content: a.content!,
|
||||
})),
|
||||
});
|
||||
const baseName = file.name.replace(/\.[^.]+$/, '');
|
||||
const pdfBuffer = await pdfBlob.arrayBuffer();
|
||||
zip.file(`${baseName}.pdf`, pdfBuffer);
|
||||
} catch (e: any) {
|
||||
console.error(`Failed to convert ${file.name}:`, e);
|
||||
}
|
||||
}
|
||||
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
downloadFile(zipBlob, 'emails-converted.zip');
|
||||
|
||||
hideLoader();
|
||||
|
||||
showAlert(
|
||||
'Conversion Complete',
|
||||
`Successfully converted ${state.files.length} ${TOOL_NAME} file(s) to PDF.`,
|
||||
'success',
|
||||
() => resetState()
|
||||
);
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error(`[${TOOL_NAME}2PDF] ERROR:`, e);
|
||||
hideLoader();
|
||||
showAlert(
|
||||
'Error',
|
||||
`An error occurred during conversion. Error: ${e.message}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const handleFileSelect = (files: FileList | null) => {
|
||||
if (files && files.length > 0) {
|
||||
state.files = [...state.files, ...Array.from(files)];
|
||||
updateUI();
|
||||
}
|
||||
};
|
||||
|
||||
if (fileInput && dropZone) {
|
||||
fileInput.addEventListener('change', (e) => {
|
||||
handleFileSelect((e.target as HTMLInputElement).files);
|
||||
});
|
||||
|
||||
dropZone.addEventListener('dragover', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.add('bg-gray-700');
|
||||
});
|
||||
|
||||
dropZone.addEventListener('dragleave', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove('bg-gray-700');
|
||||
});
|
||||
|
||||
dropZone.addEventListener('drop', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove('bg-gray-700');
|
||||
const files = e.dataTransfer?.files;
|
||||
if (files && files.length > 0) {
|
||||
const validFiles = Array.from(files).filter((f) => {
|
||||
const name = f.name.toLowerCase();
|
||||
return EXTENSIONS.some((ext) => name.endsWith(ext));
|
||||
});
|
||||
if (validFiles.length > 0) {
|
||||
const dataTransfer = new DataTransfer();
|
||||
validFiles.forEach((f) => dataTransfer.items.add(f));
|
||||
handleFileSelect(dataTransfer.files);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fileInput.addEventListener('click', () => {
|
||||
fileInput.value = '';
|
||||
});
|
||||
}
|
||||
|
||||
if (addMoreBtn) {
|
||||
addMoreBtn.addEventListener('click', () => {
|
||||
fileInput.click();
|
||||
});
|
||||
}
|
||||
|
||||
if (clearFilesBtn) {
|
||||
clearFilesBtn.addEventListener('click', () => {
|
||||
resetState();
|
||||
});
|
||||
}
|
||||
|
||||
if (processBtn) {
|
||||
processBtn.addEventListener('click', convertToPdf);
|
||||
}
|
||||
});
|
||||
393
src/js/logic/email-to-pdf.ts
Normal file
393
src/js/logic/email-to-pdf.ts
Normal file
@@ -0,0 +1,393 @@
|
||||
import PostalMime from 'postal-mime';
|
||||
import MsgReader from '@kenjiuno/msgreader';
|
||||
import { formatBytes, escapeHtml } from '../utils/helpers.js';
|
||||
import type { EmailAttachment, ParsedEmail, EmailRenderOptions } from '@/types';
|
||||
|
||||
// Re-export types for convenience
|
||||
export type { EmailAttachment, ParsedEmail, EmailRenderOptions };
|
||||
|
||||
/**
|
||||
* Format email address without angle brackets for cleaner display
|
||||
*/
|
||||
function formatAddress(
|
||||
name: string | undefined,
|
||||
email: string | undefined
|
||||
): string {
|
||||
if (name && email) {
|
||||
return `${name} (${email})`;
|
||||
}
|
||||
return email || name || '';
|
||||
}
|
||||
|
||||
export async function parseEmlFile(file: File): Promise<ParsedEmail> {
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const parser = new PostalMime();
|
||||
const email = await parser.parse(arrayBuffer);
|
||||
|
||||
const from =
|
||||
formatAddress(email.from?.name, email.from?.address) || 'Unknown Sender';
|
||||
|
||||
const to = (email.to || [])
|
||||
.map((addr) => formatAddress(addr.name, addr.address))
|
||||
.filter(Boolean);
|
||||
|
||||
const cc = (email.cc || [])
|
||||
.map((addr) => formatAddress(addr.name, addr.address))
|
||||
.filter(Boolean);
|
||||
|
||||
const bcc = (email.bcc || [])
|
||||
.map((addr) => formatAddress(addr.name, addr.address))
|
||||
.filter(Boolean);
|
||||
|
||||
// Helper to map parsing result to EmailAttachment
|
||||
const mapAttachment = (att: any): EmailAttachment => {
|
||||
let content: Uint8Array | undefined;
|
||||
let size = 0;
|
||||
if (att.content) {
|
||||
if (att.content instanceof ArrayBuffer) {
|
||||
content = new Uint8Array(att.content);
|
||||
size = content.byteLength;
|
||||
} else if (att.content instanceof Uint8Array) {
|
||||
content = att.content;
|
||||
size = content.byteLength;
|
||||
}
|
||||
}
|
||||
return {
|
||||
filename: att.filename || 'unnamed',
|
||||
size,
|
||||
contentType: att.mimeType || 'application/octet-stream',
|
||||
content,
|
||||
contentId: att.contentId
|
||||
? att.contentId.replace(/^<|>$/g, '')
|
||||
: undefined,
|
||||
};
|
||||
};
|
||||
|
||||
const attachments: EmailAttachment[] = [
|
||||
...(email.attachments || []).map(mapAttachment),
|
||||
...((email as any).inline || []).map(mapAttachment),
|
||||
];
|
||||
|
||||
// Preserve original date string from headers
|
||||
let rawDateString = '';
|
||||
if (email.headers) {
|
||||
const dateHeader = email.headers.find(
|
||||
(h) => h.key.toLowerCase() === 'date'
|
||||
);
|
||||
if (dateHeader) {
|
||||
rawDateString = dateHeader.value as string;
|
||||
}
|
||||
}
|
||||
if (!rawDateString && email.date) {
|
||||
rawDateString = email.date; // fallback if header missing but parsed date exists as string?
|
||||
}
|
||||
|
||||
let parsedDate: Date | null = null;
|
||||
if (email.date) {
|
||||
try {
|
||||
parsedDate = new Date(email.date);
|
||||
if (isNaN(parsedDate.getTime())) {
|
||||
parsedDate = null;
|
||||
}
|
||||
} catch {
|
||||
parsedDate = null;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
subject: email.subject || '(No Subject)',
|
||||
from,
|
||||
to,
|
||||
cc,
|
||||
bcc,
|
||||
date: parsedDate,
|
||||
rawDateString,
|
||||
htmlBody: email.html || '',
|
||||
textBody: email.text || '',
|
||||
attachments,
|
||||
};
|
||||
}
|
||||
|
||||
export async function parseMsgFile(file: File): Promise<ParsedEmail> {
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const msgReader = new MsgReader(arrayBuffer);
|
||||
const msgData = msgReader.getFileData();
|
||||
|
||||
const from =
|
||||
formatAddress(msgData.senderName, msgData.senderEmail) || 'Unknown Sender';
|
||||
|
||||
const to: string[] = [];
|
||||
const cc: string[] = [];
|
||||
const bcc: string[] = [];
|
||||
|
||||
if (msgData.recipients) {
|
||||
for (const recipient of msgData.recipients) {
|
||||
const recipientStr = formatAddress(recipient.name, recipient.email);
|
||||
if (!recipientStr) continue;
|
||||
|
||||
const recipType = String(recipient.recipType).toLowerCase();
|
||||
if (recipType === 'cc' || recipType === '2') {
|
||||
cc.push(recipientStr);
|
||||
} else if (recipType === 'bcc' || recipType === '3') {
|
||||
bcc.push(recipientStr);
|
||||
} else {
|
||||
to.push(recipientStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const attachments: EmailAttachment[] = (msgData.attachments || []).map(
|
||||
(att: any) => ({
|
||||
filename: att.fileName || att.name || 'unnamed',
|
||||
size: att.content?.length || 0,
|
||||
contentType: att.mimeType || 'application/octet-stream',
|
||||
content: att.content ? new Uint8Array(att.content) : undefined,
|
||||
contentId: att.pidContentId
|
||||
? att.pidContentId.replace(/^<|>$/g, '')
|
||||
: undefined,
|
||||
})
|
||||
);
|
||||
|
||||
let date: Date | null = null;
|
||||
let rawDateString = '';
|
||||
if (msgData.messageDeliveryTime) {
|
||||
rawDateString = msgData.messageDeliveryTime;
|
||||
date = new Date(msgData.messageDeliveryTime);
|
||||
} else if (msgData.clientSubmitTime) {
|
||||
rawDateString = msgData.clientSubmitTime;
|
||||
date = new Date(msgData.clientSubmitTime);
|
||||
}
|
||||
|
||||
return {
|
||||
subject: msgData.subject || '(No Subject)',
|
||||
from,
|
||||
to,
|
||||
cc,
|
||||
bcc,
|
||||
date,
|
||||
rawDateString,
|
||||
htmlBody: msgData.bodyHtml || '',
|
||||
textBody: msgData.body || '',
|
||||
attachments,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a raw RFC 2822 date string into a nicer human-readable format,
|
||||
* while preserving the original timezone and time.
|
||||
* Example input: "Sun, 8 Jan 2017 20:37:44 +0200"
|
||||
* Example output: "Sunday, January 8, 2017 at 8:37 PM (+0200)"
|
||||
*/
|
||||
function formatRawDate(raw: string): string {
|
||||
try {
|
||||
// Regex to parse RFC 2822 date parts: Day, DD Mon YYYY HH:MM:SS Timezone
|
||||
const match = raw.match(
|
||||
/([A-Za-z]{3}),\s+(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{2}):(\d{2})(?::(\d{2}))?\s+([+-]\d{4})/
|
||||
);
|
||||
|
||||
if (match) {
|
||||
const [
|
||||
,
|
||||
dayAbbr,
|
||||
dom,
|
||||
monthAbbr,
|
||||
year,
|
||||
hoursStr,
|
||||
minsStr,
|
||||
secsStr,
|
||||
timezone,
|
||||
] = match;
|
||||
|
||||
// Map abbreviations to full names
|
||||
const days: Record<string, string> = {
|
||||
Sun: 'Sunday',
|
||||
Mon: 'Monday',
|
||||
Tue: 'Tuesday',
|
||||
Wed: 'Wednesday',
|
||||
Thu: 'Thursday',
|
||||
Fri: 'Friday',
|
||||
Sat: 'Saturday',
|
||||
};
|
||||
const months: Record<string, string> = {
|
||||
Jan: 'January',
|
||||
Feb: 'February',
|
||||
Mar: 'March',
|
||||
Apr: 'April',
|
||||
May: 'May',
|
||||
Jun: 'June',
|
||||
Jul: 'July',
|
||||
Aug: 'August',
|
||||
Sep: 'September',
|
||||
Oct: 'October',
|
||||
Nov: 'November',
|
||||
Dec: 'December',
|
||||
};
|
||||
|
||||
const fullDay = days[dayAbbr] || dayAbbr;
|
||||
const fullMonth = months[monthAbbr] || monthAbbr;
|
||||
|
||||
// Convert to 12-hour format manually
|
||||
let hours = parseInt(hoursStr, 10);
|
||||
const ampm = hours >= 12 ? 'PM' : 'AM';
|
||||
hours = hours % 12;
|
||||
hours = hours ? hours : 12; // the hour '0' should be '12'
|
||||
|
||||
// Format timezone: +0200 -> UTC+02:00
|
||||
const tzSign = timezone.substring(0, 1);
|
||||
const tzHours = timezone.substring(1, 3);
|
||||
const tzMins = timezone.substring(3, 5);
|
||||
const formattedTz = `UTC${tzSign}${tzHours}:${tzMins}`;
|
||||
|
||||
return `${fullDay}, ${fullMonth} ${dom}, ${year} at ${hours}:${minsStr} ${ampm} (${formattedTz})`;
|
||||
}
|
||||
} catch (e) {
|
||||
// Fallback to raw string if parsing fails
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace CID references in HTML with base64 data URIs
|
||||
*/
|
||||
function processInlineImages(
|
||||
html: string,
|
||||
attachments: EmailAttachment[]
|
||||
): string {
|
||||
if (!html) return html;
|
||||
|
||||
// Create a map of contentIds to attachments
|
||||
const cidMap = new Map<string, EmailAttachment>();
|
||||
attachments.forEach((att) => {
|
||||
if (att.contentId) {
|
||||
cidMap.set(att.contentId, att);
|
||||
}
|
||||
});
|
||||
|
||||
// Replace src="cid:..."
|
||||
return html.replace(/src=["']cid:([^"']+)["']/g, (match, cid) => {
|
||||
const att = cidMap.get(cid);
|
||||
if (att && att.content) {
|
||||
// Convert Uint8Array to base64
|
||||
let binary = '';
|
||||
const len = att.content.byteLength;
|
||||
for (let i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode(att.content[i]);
|
||||
}
|
||||
const base64 =
|
||||
typeof btoa === 'function'
|
||||
? btoa(binary)
|
||||
: Buffer.from(binary, 'binary').toString('base64');
|
||||
return `src="data:${att.contentType};base64,${base64}"`;
|
||||
}
|
||||
return match; // Keep original if not found
|
||||
});
|
||||
}
|
||||
|
||||
export function renderEmailToHtml(
|
||||
email: ParsedEmail,
|
||||
options: EmailRenderOptions = {}
|
||||
): string {
|
||||
const { includeCcBcc = true, includeAttachments = true } = options;
|
||||
|
||||
let processedHtml = '';
|
||||
if (email.htmlBody) {
|
||||
processedHtml = processInlineImages(email.htmlBody, email.attachments);
|
||||
} else {
|
||||
processedHtml = `<pre style="white-space: pre-wrap; font-family: inherit; margin: 0;">${escapeHtml(email.textBody)}</pre>`;
|
||||
}
|
||||
|
||||
// Format date in a human-readable way
|
||||
let dateStr = 'Unknown Date';
|
||||
if (email.rawDateString) {
|
||||
dateStr = formatRawDate(email.rawDateString);
|
||||
} else if (email.date && !isNaN(email.date.getTime())) {
|
||||
dateStr = email.date.toLocaleString('en-US', {
|
||||
weekday: 'long',
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
hour: 'numeric',
|
||||
minute: '2-digit',
|
||||
hour12: true,
|
||||
});
|
||||
}
|
||||
|
||||
const attachmentHtml =
|
||||
includeAttachments && email.attachments.length > 0
|
||||
? `
|
||||
<div style="margin-top: 20px; padding-top: 15px; border-top: 1px solid #cccccc;">
|
||||
<p style="margin: 0 0 8px 0; font-size: 11px; color: #666; font-weight: 600;">Attachments (${email.attachments.length})</p>
|
||||
<ul style="margin: 0; padding-left: 20px; font-size: 10px; color: #555;">
|
||||
${email.attachments
|
||||
.map(
|
||||
(att) =>
|
||||
`<li style="margin-bottom: 5px;">${escapeHtml(att.filename)} <span style="color: #999;">(${formatBytes(att.size)})</span></li>`
|
||||
)
|
||||
.join('')}
|
||||
</ul>
|
||||
</div>
|
||||
`
|
||||
: '';
|
||||
|
||||
// Build CC/BCC rows
|
||||
let ccBccHtml = '';
|
||||
if (includeCcBcc) {
|
||||
if (email.cc.length > 0) {
|
||||
ccBccHtml += `
|
||||
<div style="margin-bottom: 8px;">
|
||||
<span style="font-weight: 600; color: #666; margin-right: 8px;">CC:</span>
|
||||
<span style="color: #333;">${escapeHtml(email.cc.join(', '))}</span>
|
||||
</div>`;
|
||||
}
|
||||
if (email.bcc.length > 0) {
|
||||
ccBccHtml += `
|
||||
<div style="margin-bottom: 8px;">
|
||||
<span style="font-weight: 600; color: #666; margin-right: 8px;">BCC:</span>
|
||||
<span style="color: #333;">${escapeHtml(email.bcc.join(', '))}</span>
|
||||
</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto; padding: 40px 20px; background: #fff;">
|
||||
<div style="border-bottom: 2px solid #999999; padding-bottom: 20px; margin-bottom: 30px;">
|
||||
<h1 style="font-size: 18px; font-weight: 600; color: #1a1a1a; margin: 0 0 12px 0;">${escapeHtml(email.subject)}</h1>
|
||||
<div style="font-size: 12px; color: #555;">
|
||||
<div style="margin-bottom: 8px;">
|
||||
<span style="font-weight: 600; color: #666; margin-right: 8px;">From:</span>
|
||||
<span style="color: #333;">${escapeHtml(email.from)}</span>
|
||||
</div>
|
||||
<div style="margin-bottom: 8px;">
|
||||
<span style="font-weight: 600; color: #666; margin-right: 8px;">To:</span>
|
||||
<span style="color: #333;">${escapeHtml(email.to.join(', ') || 'Unknown')}</span>
|
||||
</div>
|
||||
${ccBccHtml}
|
||||
<div style="margin-bottom: 8px;">
|
||||
<span style="font-weight: 600; color: #666; margin-right: 8px;">Date:</span>
|
||||
<span style="color: #333;">${escapeHtml(dateStr)}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div style="font-size: 12px; color: #333;">
|
||||
${processedHtml}
|
||||
</div>
|
||||
${attachmentHtml}
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
export async function parseEmailFile(file: File): Promise<ParsedEmail> {
|
||||
const ext = file.name.toLowerCase().split('.').pop();
|
||||
|
||||
if (ext === 'eml') {
|
||||
return parseEmlFile(file);
|
||||
} else if (ext === 'msg') {
|
||||
return parseMsgFile(file);
|
||||
} else {
|
||||
throw new Error(`Unsupported file type: .${ext}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user