diff --git a/app/api/mail/route.ts b/app/api/mail/route.ts index b2b1a85..214498a 100644 --- a/app/api/mail/route.ts +++ b/app/api/mail/route.ts @@ -176,4 +176,56 @@ export async function GET() { { status } ); } +} + +export async function POST(request: Request) { + try { + const credentials = getStoredCredentials(); + if (!credentials) { + return NextResponse.json( + { error: 'No stored credentials found' }, + { status: 401 } + ); + } + + const { to, subject, body, attachments } = await request.json(); + + if (!to || !subject || !body) { + return NextResponse.json( + { error: 'Missing required fields' }, + { status: 400 } + ); + } + + const nodemailer = require('nodemailer'); + + const transporter = nodemailer.createTransport({ + host: credentials.host, + port: credentials.port, + secure: true, + auth: { + user: credentials.email, + pass: credentials.password, + }, + }); + + const mailOptions = { + from: credentials.email, + to, + subject, + text: body, + attachments: attachments || [], + }; + + const info = await transporter.sendMail(mailOptions); + console.log('Email sent:', info.messageId); + + return NextResponse.json({ success: true, messageId: info.messageId }); + } catch (error) { + console.error('Error sending email:', error); + return NextResponse.json( + { error: error instanceof Error ? error.message : 'Failed to send email' }, + { status: 500 } + ); + } } \ No newline at end of file