import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { sendEmail } from '@/lib/services/email-service'; export async function POST(request: Request) { try { // Authenticate user const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ); } // Parse request body const { to, cc, bcc, subject, body, attachments } = await request.json(); // Validate required fields if (!to) { return NextResponse.json( { error: 'Recipient is required' }, { status: 400 } ); } // Use email service to send the email const result = await sendEmail(session.user.id, { to, cc, bcc, subject, body, attachments }); if (!result.success) { return NextResponse.json( { error: 'Failed to send email', details: result.error }, { status: 500 } ); } return NextResponse.json({ success: true, messageId: result.messageId }); } catch (error) { console.error('Error sending email:', error); return NextResponse.json( { error: 'Failed to send email', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 } ); } }