mail page imap connection mime 5 bis rest 16 login page 13
This commit is contained in:
parent
7e15a70def
commit
6eae29cf76
@ -1,5 +1,6 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import Imap from 'imap';
|
||||
import nodemailer from 'nodemailer';
|
||||
import { parseEmailHeaders } from '@/lib/email-parser';
|
||||
|
||||
interface StoredCredentials {
|
||||
@ -188,16 +189,24 @@ export async function POST(request: Request) {
|
||||
);
|
||||
}
|
||||
|
||||
const { to, subject, body, attachments } = await request.json();
|
||||
|
||||
if (!to || !subject || !body) {
|
||||
let body;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing required fields' },
|
||||
{ error: 'Invalid JSON in request body' },
|
||||
{ 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({
|
||||
host: credentials.host,
|
||||
@ -213,18 +222,25 @@ export async function POST(request: Request) {
|
||||
from: credentials.email,
|
||||
to,
|
||||
subject,
|
||||
text: body,
|
||||
text: emailBody,
|
||||
attachments: attachments || [],
|
||||
};
|
||||
|
||||
const info = await transporter.sendMail(mailOptions);
|
||||
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) {
|
||||
console.error('Error sending email:', error);
|
||||
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 }
|
||||
);
|
||||
}
|
||||
|
||||
@ -4,53 +4,47 @@ import Imap from 'imap';
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { email, password, host, port } = await request.json();
|
||||
|
||||
// Ensure port is a valid number
|
||||
const portNumber = parseInt(port, 10);
|
||||
if (isNaN(portNumber) || portNumber < 0 || portNumber > 65535) {
|
||||
throw new Error('Invalid port number');
|
||||
|
||||
if (!email || !password || !host || !port) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing required fields' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const imapConfig = {
|
||||
user: email,
|
||||
password: password,
|
||||
host: host,
|
||||
port: portNumber,
|
||||
password,
|
||||
host,
|
||||
port: parseInt(port),
|
||||
tls: true,
|
||||
authTimeout: 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);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const connectPromise = new Promise((resolve, reject) => {
|
||||
imap.once('ready', () => {
|
||||
console.log('IMAP connection successful');
|
||||
imap.end();
|
||||
resolve(NextResponse.json({ success: true }));
|
||||
resolve(true);
|
||||
});
|
||||
|
||||
imap.once('error', (err: Error) => {
|
||||
console.error('IMAP connection error:', err);
|
||||
reject(NextResponse.json({
|
||||
error: 'IMAP connection failed',
|
||||
details: err.message
|
||||
}, { status: 500 }));
|
||||
imap.end();
|
||||
reject(err);
|
||||
});
|
||||
|
||||
imap.connect();
|
||||
});
|
||||
|
||||
await connectPromise;
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error in test-connection:', error);
|
||||
return NextResponse.json({
|
||||
error: 'Failed to test connection',
|
||||
details: error instanceof Error ? error.message : 'Unknown error'
|
||||
}, { status: 500 });
|
||||
console.error('Error testing connection:', error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : 'Failed to connect to email server' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -22,7 +22,8 @@ export default function LoginPage() {
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/mail', {
|
||||
// Test the connection first
|
||||
const testResponse = await fetch('/api/mail/test-connection', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'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) {
|
||||
throw new Error(data.error || 'Failed to connect to email server');
|
||||
if (!testResponse.ok) {
|
||||
throw new Error(testData.error || 'Failed to connect to email server');
|
||||
}
|
||||
|
||||
// Store credentials in localStorage
|
||||
localStorage.setItem('imapCredentials', JSON.stringify({
|
||||
user: email,
|
||||
email,
|
||||
password,
|
||||
host,
|
||||
port,
|
||||
port: parseInt(port),
|
||||
}));
|
||||
|
||||
// Redirect to mail page
|
||||
|
||||
Loading…
Reference in New Issue
Block a user