114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { ImapFlow } from 'imapflow';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const credentials = await prisma.mailCredentials.findUnique({
|
|
where: {
|
|
userId: session.user.id
|
|
}
|
|
});
|
|
|
|
if (!credentials) {
|
|
return NextResponse.json(
|
|
{ error: 'No mail credentials found. Please configure your email account.' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
let client;
|
|
try {
|
|
client = new ImapFlow({
|
|
host: credentials.host,
|
|
port: credentials.port,
|
|
secure: true,
|
|
auth: {
|
|
user: credentials.email,
|
|
pass: credentials.password,
|
|
},
|
|
logger: false,
|
|
emitLogs: false,
|
|
tls: {
|
|
rejectUnauthorized: false // Allow self-signed certificates
|
|
}
|
|
});
|
|
|
|
await client.connect();
|
|
const mailbox = await client.mailboxOpen('INBOX');
|
|
|
|
// Fetch the last 20 messages
|
|
const messages = await client.fetch('1:20', {
|
|
envelope: true,
|
|
bodyStructure: true,
|
|
flags: true
|
|
});
|
|
|
|
const result = [];
|
|
for await (const message of messages) {
|
|
// Get the message body
|
|
const body = await client.download(message.uid.toString(), 'TEXT');
|
|
|
|
result.push({
|
|
id: message.uid.toString(),
|
|
from: message.envelope.from[0].address,
|
|
subject: message.envelope.subject || '(No subject)',
|
|
date: message.envelope.date.toISOString(),
|
|
read: message.flags.has('\\Seen'),
|
|
starred: message.flags.has('\\Flagged'),
|
|
folder: mailbox.path,
|
|
// Additional fields that might be useful
|
|
to: message.envelope.to?.map(addr => addr.address).join(', ') || '',
|
|
cc: message.envelope.cc?.map(addr => addr.address) || [],
|
|
bcc: message.envelope.bcc?.map(addr => addr.address) || [],
|
|
flags: Array.from(message.flags),
|
|
body: body?.content?.toString() || ''
|
|
});
|
|
}
|
|
|
|
return NextResponse.json(result);
|
|
} catch (error) {
|
|
console.error('Mail API error:', error);
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('Invalid login')) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid email credentials. Please update your email settings.' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
return NextResponse.json(
|
|
{ error: `Failed to fetch emails: ${error.message}` },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch emails' },
|
|
{ status: 500 }
|
|
);
|
|
} finally {
|
|
if (client) {
|
|
try {
|
|
await client.logout();
|
|
} catch (e) {
|
|
console.error('Error during logout:', e);
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Unexpected error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'An unexpected error occurred' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |