mail page imap connection mime 5 bis rest 16 login page 13

This commit is contained in:
alma 2025-04-16 00:13:30 +02:00
parent 7e15a70def
commit 6eae29cf76
3 changed files with 54 additions and 43 deletions

View File

@ -1,5 +1,6 @@
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
import Imap from 'imap'; import Imap from 'imap';
import nodemailer from 'nodemailer';
import { parseEmailHeaders } from '@/lib/email-parser'; import { parseEmailHeaders } from '@/lib/email-parser';
interface StoredCredentials { interface StoredCredentials {
@ -188,16 +189,24 @@ export async function POST(request: Request) {
); );
} }
const { to, subject, body, attachments } = await request.json(); let body;
try {
if (!to || !subject || !body) { body = await request.json();
} catch (error) {
return NextResponse.json( return NextResponse.json(
{ error: 'Missing required fields' }, { error: 'Invalid JSON in request body' },
{ status: 400 } { status: 400 }
); );
} }
const nodemailer = require('nodemailer'); const { to, subject, body: emailBody, attachments } = body;
if (!to || !subject || !emailBody) {
return NextResponse.json(
{ error: 'Missing required fields: to, subject, or body' },
{ status: 400 }
);
}
const transporter = nodemailer.createTransport({ const transporter = nodemailer.createTransport({
host: credentials.host, host: credentials.host,
@ -213,18 +222,25 @@ export async function POST(request: Request) {
from: credentials.email, from: credentials.email,
to, to,
subject, subject,
text: body, text: emailBody,
attachments: attachments || [], attachments: attachments || [],
}; };
const info = await transporter.sendMail(mailOptions); const info = await transporter.sendMail(mailOptions);
console.log('Email sent:', info.messageId); console.log('Email sent:', info.messageId);
return NextResponse.json({ success: true, messageId: info.messageId }); return NextResponse.json({
success: true,
messageId: info.messageId,
message: 'Email sent successfully'
});
} catch (error) { } catch (error) {
console.error('Error sending email:', error); console.error('Error sending email:', error);
return NextResponse.json( return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to send email' }, {
error: error instanceof Error ? error.message : 'Failed to send email',
details: error instanceof Error ? error.stack : undefined
},
{ status: 500 } { status: 500 }
); );
} }

View File

@ -4,53 +4,47 @@ import Imap from 'imap';
export async function POST(request: Request) { export async function POST(request: Request) {
try { try {
const { email, password, host, port } = await request.json(); const { email, password, host, port } = await request.json();
// Ensure port is a valid number if (!email || !password || !host || !port) {
const portNumber = parseInt(port, 10); return NextResponse.json(
if (isNaN(portNumber) || portNumber < 0 || portNumber > 65535) { { error: 'Missing required fields' },
throw new Error('Invalid port number'); { status: 400 }
);
} }
const imapConfig = { const imapConfig = {
user: email, user: email,
password: password, password,
host: host, host,
port: portNumber, port: parseInt(port),
tls: true, tls: true,
authTimeout: 10000, authTimeout: 10000,
connTimeout: 10000, connTimeout: 10000,
debug: console.log debug: (info: string) => console.log('IMAP Debug:', info)
}; };
console.log('Attempting IMAP connection with config:', {
...imapConfig,
password: '[REDACTED]'
});
const imap = new Imap(imapConfig); const imap = new Imap(imapConfig);
return new Promise((resolve, reject) => { const connectPromise = new Promise((resolve, reject) => {
imap.once('ready', () => { imap.once('ready', () => {
console.log('IMAP connection successful');
imap.end(); imap.end();
resolve(NextResponse.json({ success: true })); resolve(true);
}); });
imap.once('error', (err: Error) => { imap.once('error', (err: Error) => {
console.error('IMAP connection error:', err); imap.end();
reject(NextResponse.json({ reject(err);
error: 'IMAP connection failed',
details: err.message
}, { status: 500 }));
}); });
imap.connect(); imap.connect();
}); });
await connectPromise;
return NextResponse.json({ success: true });
} catch (error) { } catch (error) {
console.error('Error in test-connection:', error); console.error('Error testing connection:', error);
return NextResponse.json({ return NextResponse.json(
error: 'Failed to test connection', { error: error instanceof Error ? error.message : 'Failed to connect to email server' },
details: error instanceof Error ? error.message : 'Unknown error' { status: 500 }
}, { status: 500 }); );
} }
} }

View File

@ -22,7 +22,8 @@ export default function LoginPage() {
setLoading(true); setLoading(true);
try { try {
const response = await fetch('/api/mail', { // Test the connection first
const testResponse = await fetch('/api/mail/test-connection', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -35,18 +36,18 @@ export default function LoginPage() {
}), }),
}); });
const data = await response.json(); const testData = await testResponse.json();
if (!response.ok) { if (!testResponse.ok) {
throw new Error(data.error || 'Failed to connect to email server'); throw new Error(testData.error || 'Failed to connect to email server');
} }
// Store credentials in localStorage // Store credentials in localStorage
localStorage.setItem('imapCredentials', JSON.stringify({ localStorage.setItem('imapCredentials', JSON.stringify({
user: email, email,
password, password,
host, host,
port, port: parseInt(port),
})); }));
// Redirect to mail page // Redirect to mail page