mime change
This commit is contained in:
parent
e45b350175
commit
eb02fd016b
55
app/api/parse-email/route.ts
Normal file
55
app/api/parse-email/route.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { simpleParser } from 'mailparser';
|
||||
import DOMPurify from 'dompurify';
|
||||
import { JSDOM } from 'jsdom';
|
||||
|
||||
// Create a window object for DOMPurify
|
||||
const window = new JSDOM('').window;
|
||||
const purify = DOMPurify(window);
|
||||
|
||||
function cleanHtml(html: string): string {
|
||||
try {
|
||||
return purify.sanitize(html, {
|
||||
ALLOWED_TAGS: ['p', 'br', 'div', 'span', 'a', 'img', 'strong', 'em', 'u', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
||||
ALLOWED_ATTR: ['href', 'src', 'alt', 'title', 'class', 'style'],
|
||||
ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error cleaning HTML:', error);
|
||||
return html;
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { emailContent } = await request.json();
|
||||
|
||||
if (!emailContent) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email content is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const parsed = await simpleParser(emailContent);
|
||||
|
||||
return NextResponse.json({
|
||||
subject: parsed.subject || null,
|
||||
from: parsed.from?.text || null,
|
||||
to: parsed.to?.text || null,
|
||||
cc: parsed.cc?.text || null,
|
||||
bcc: parsed.bcc?.text || null,
|
||||
date: parsed.date || null,
|
||||
html: parsed.html ? cleanHtml(parsed.html) : null,
|
||||
text: parsed.text || null,
|
||||
attachments: parsed.attachments || [],
|
||||
headers: Object.fromEntries(parsed.headers)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error parsing email:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to parse email' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -198,6 +198,17 @@ const initialSidebarItems = [
|
||||
}
|
||||
];
|
||||
|
||||
function formatDate(date: Date | null): string {
|
||||
if (!date) return '';
|
||||
return new Intl.DateTimeFormat('fr-FR', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
}).format(date);
|
||||
}
|
||||
|
||||
async function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward' = 'reply') {
|
||||
if (!email.body) return '';
|
||||
|
||||
@ -211,26 +222,26 @@ async function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward
|
||||
formattedContent = `
|
||||
<div class="forwarded-message">
|
||||
<p>---------- Forwarded message ---------</p>
|
||||
<p>From: ${decoded.from}</p>
|
||||
<p>Date: ${decoded.date.toLocaleString()}</p>
|
||||
<p>Subject: ${decoded.subject}</p>
|
||||
<p>To: ${decoded.to}</p>
|
||||
<p>From: ${decoded.from || ''}</p>
|
||||
<p>Date: ${formatDate(decoded.date)}</p>
|
||||
<p>Subject: ${decoded.subject || ''}</p>
|
||||
<p>To: ${decoded.to || ''}</p>
|
||||
<br>
|
||||
${decoded.html || `<pre>${decoded.text}</pre>`}
|
||||
${decoded.html || `<pre>${decoded.text || ''}</pre>`}
|
||||
</div>
|
||||
`;
|
||||
} else {
|
||||
formattedContent = `
|
||||
<div class="quoted-message">
|
||||
<p>On ${decoded.date.toLocaleString()}, ${decoded.from} wrote:</p>
|
||||
<p>On ${formatDate(decoded.date)}, ${decoded.from || ''} wrote:</p>
|
||||
<blockquote>
|
||||
${decoded.html || `<pre>${decoded.text}</pre>`}
|
||||
${decoded.html || `<pre>${decoded.text || ''}</pre>`}
|
||||
</blockquote>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
return cleanHtml(formattedContent);
|
||||
return formattedContent;
|
||||
} catch (error) {
|
||||
console.error('Error generating reply body:', error);
|
||||
return '';
|
||||
@ -447,18 +458,6 @@ export default function CourrierPage() {
|
||||
loadEmails();
|
||||
}, [currentView]);
|
||||
|
||||
// Format date for display
|
||||
function formatDate(date: Date | null): string {
|
||||
if (!date) return '';
|
||||
return new Intl.DateTimeFormat('fr-FR', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
}).format(date);
|
||||
}
|
||||
|
||||
// Get account color
|
||||
const getAccountColor = (accountId: number) => {
|
||||
const account = accounts.find(acc => acc.id === accountId);
|
||||
|
||||
@ -18,8 +18,12 @@ export interface ParsedEmail {
|
||||
date: Date | null;
|
||||
html: string | null;
|
||||
text: string | null;
|
||||
attachments: Attachment[];
|
||||
headers: Record<string, HeaderValue>;
|
||||
attachments: Array<{
|
||||
filename: string;
|
||||
contentType: string;
|
||||
size: number;
|
||||
}>;
|
||||
headers: Record<string, any>;
|
||||
}
|
||||
|
||||
function getAddressText(address: AddressObject | AddressObject[] | undefined): string | null {
|
||||
@ -31,24 +35,23 @@ function getAddressText(address: AddressObject | AddressObject[] | undefined): s
|
||||
}
|
||||
|
||||
export async function decodeEmail(emailContent: string): Promise<ParsedEmail> {
|
||||
if (isBrowser) {
|
||||
throw new Error('decodeEmail can only be used on the server side');
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = await simpleParser(emailContent);
|
||||
|
||||
const response = await fetch('/api/parse-email', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ emailContent }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to parse email');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return {
|
||||
subject: parsed.subject || null,
|
||||
from: getAddressText(parsed.from),
|
||||
to: getAddressText(parsed.to),
|
||||
cc: getAddressText(parsed.cc),
|
||||
bcc: getAddressText(parsed.bcc),
|
||||
date: parsed.date || null,
|
||||
html: parsed.html ? cleanHtml(parsed.html) : null,
|
||||
text: parsed.text || null,
|
||||
attachments: parsed.attachments || [],
|
||||
headers: Object.fromEntries(parsed.headers)
|
||||
...data,
|
||||
date: data.date ? new Date(data.date) : null
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error parsing email:', error);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user