diff --git a/app/api/mail/send/route.ts b/app/api/mail/send/route.ts new file mode 100644 index 0000000..f0c24ec --- /dev/null +++ b/app/api/mail/send/route.ts @@ -0,0 +1,45 @@ +import { NextResponse } from 'next/server'; +import { createTransport } from 'nodemailer'; + +export async function POST(request: Request) { + try { + const { to, cc, bcc, subject, body, attachments } = await request.json(); + + // Create a transporter using SMTP + const transporter = createTransport({ + host: process.env.SMTP_HOST, + port: Number(process.env.SMTP_PORT), + secure: process.env.SMTP_SECURE === 'true', + auth: { + user: process.env.SMTP_USER, + pass: process.env.SMTP_PASSWORD, + }, + }); + + // Prepare email options + const mailOptions = { + from: process.env.SMTP_FROM || process.env.SMTP_USER, + to, + cc, + bcc, + subject, + text: body, + attachments: attachments?.map((attachment: any) => ({ + filename: attachment.name, + content: attachment.content, + encoding: attachment.encoding, + })), + }; + + // Send the email + await transporter.sendMail(mailOptions); + + return NextResponse.json({ success: true }); + } catch (error) { + console.error('Error sending email:', error); + return NextResponse.json( + { error: 'Failed to send email' }, + { status: 500 } + ); + } +} \ No newline at end of file