Neah version mail max att size

This commit is contained in:
alma 2025-04-16 20:23:59 +02:00
parent 772255a5d4
commit 8eb5ebf969
2 changed files with 50 additions and 10 deletions

View File

@ -9,6 +9,9 @@ interface StoredCredentials {
port: number; port: number;
} }
// Maximum attachment size in bytes (10MB)
const MAX_ATTACHMENT_SIZE = 10 * 1024 * 1024;
function getStoredCredentials(): StoredCredentials | null { function getStoredCredentials(): StoredCredentials | null {
const cookieStore = cookies(); const cookieStore = cookies();
const credentialsCookie = cookieStore.get('imap_credentials'); const credentialsCookie = cookieStore.get('imap_credentials');
@ -40,6 +43,28 @@ export async function POST(request: Request) {
const { to, cc, bcc, subject, body, attachments } = await request.json(); const { to, cc, bcc, subject, body, attachments } = await request.json();
// Check attachment sizes
if (attachments?.length) {
const oversizedAttachments = attachments.filter((attachment: any) => {
// Calculate size from base64 content
const size = Math.ceil((attachment.content.length * 3) / 4);
return size > MAX_ATTACHMENT_SIZE;
});
if (oversizedAttachments.length > 0) {
return NextResponse.json(
{
error: 'Attachment size limit exceeded',
details: {
maxSize: MAX_ATTACHMENT_SIZE,
oversizedFiles: oversizedAttachments.map((a: any) => a.name)
}
},
{ status: 400 }
);
}
}
// Create a transporter using SMTP with the same credentials // Create a transporter using SMTP with the same credentials
// Use port 465 for SMTP (Infomaniak's SMTP port) // Use port 465 for SMTP (Infomaniak's SMTP port)
const transporter = createTransport({ const transporter = createTransport({

View File

@ -1184,8 +1184,15 @@ export default function MailPage() {
if (!e.target.files) return; if (!e.target.files) return;
const newAttachments: Attachment[] = []; const newAttachments: Attachment[] = [];
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB in bytes
const oversizedFiles: string[] = [];
for (const file of e.target.files) { for (const file of e.target.files) {
if (file.size > MAX_FILE_SIZE) {
oversizedFiles.push(file.name);
continue;
}
try { try {
// Read file as base64 // Read file as base64
const base64Content = await new Promise<string>((resolve) => { const base64Content = await new Promise<string>((resolve) => {
@ -1208,7 +1215,13 @@ export default function MailPage() {
} }
} }
if (oversizedFiles.length > 0) {
alert(`The following files exceed the 10MB size limit and were not attached:\n${oversizedFiles.join('\n')}`);
}
if (newAttachments.length > 0) {
setAttachments([...attachments, ...newAttachments]); setAttachments([...attachments, ...newAttachments]);
}
}; };
// Add handleSend function for email composition // Add handleSend function for email composition
@ -1230,27 +1243,29 @@ export default function MailPage() {
bcc: composeBcc, bcc: composeBcc,
subject: composeSubject, subject: composeSubject,
body: composeBody, body: composeBody,
attachments: attachments attachments: attachments,
}), }),
}); });
const data = await response.json();
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to send email'); if (data.error === 'Attachment size limit exceeded') {
alert(`Error: ${data.error}\nThe following files are too large:\n${data.details.oversizedFiles.join('\n')}`);
} else {
alert(`Error sending email: ${data.error}`);
}
return;
} }
// Clear compose form and close modal // Clear compose form and close modal
setShowCompose(false);
setComposeTo(''); setComposeTo('');
setComposeCc(''); setComposeCc('');
setComposeBcc(''); setComposeBcc('');
setComposeSubject(''); setComposeSubject('');
setComposeBody(''); setComposeBody('');
setAttachments([]); setAttachments([]);
setShowCc(false); setShowCompose(false);
setShowBcc(false);
// Reload emails to show sent email
loadEmails();
} catch (error) { } catch (error) {
console.error('Error sending email:', error); console.error('Error sending email:', error);
alert('Failed to send email. Please try again.'); alert('Failed to send email. Please try again.');
@ -1347,7 +1362,7 @@ export default function MailPage() {
.replace(/<p[^>]*>/gi, '\n') .replace(/<p[^>]*>/gi, '\n')
.replace(/<\/p>/gi, '') .replace(/<\/p>/gi, '')
.replace(/<[^>]+>/g, '') .replace(/<[^>]+>/g, '')
.replace(/&nbsp;|&zwnj;|&raquo;|&laquo;|&gt;|&lt;|&amp;/g, match => { .replace(/&nbsp;|&zwnj;|&raquo;|&laquo;|&gt;/g, match => {
switch (match) { switch (match) {
case '&nbsp;': return ' '; case '&nbsp;': return ' ';
case '&zwnj;': return ''; case '&zwnj;': return '';