52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { cookies } from 'next/headers';
|
|
import { ImapFlow } from 'imapflow';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const cookieStore = cookies();
|
|
const credentials = cookieStore.get('imap_credentials');
|
|
|
|
if (!credentials) {
|
|
return NextResponse.json(
|
|
{ error: 'No credentials found' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const { email, password, host, port } = JSON.parse(credentials.value);
|
|
|
|
const client = new ImapFlow({
|
|
host,
|
|
port: parseInt(port),
|
|
secure: true,
|
|
auth: {
|
|
user: email,
|
|
pass: password,
|
|
},
|
|
});
|
|
|
|
await client.connect();
|
|
const mailbox = await client.mailboxOpen('INBOX');
|
|
const messages = await client.fetch('1:10', { envelope: true });
|
|
|
|
const result = [];
|
|
for await (const message of messages) {
|
|
result.push({
|
|
id: message.uid,
|
|
subject: message.envelope.subject,
|
|
from: message.envelope.from[0].address,
|
|
date: message.envelope.date,
|
|
});
|
|
}
|
|
|
|
await client.logout();
|
|
return NextResponse.json(result);
|
|
} catch (error) {
|
|
console.error('Mail API error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch emails' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |