2024-08-26 13:11:47 +02:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
import nodemailer from "nodemailer";
|
|
|
|
|
|
|
|
const transporter = nodemailer.createTransport({
|
2024-09-07 09:12:48 +02:00
|
|
|
host: "smtp.ethereal.email", // Replace with your SMTP host
|
|
|
|
// service: "gmail", // Uncomment if using Gmail
|
|
|
|
port: 587,
|
|
|
|
auth: {
|
|
|
|
user: process.env.EMAIL,
|
|
|
|
pass: process.env.EMAIL_PASS
|
|
|
|
}
|
2024-08-26 13:11:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const sendEmail = async (to: string[], subject: string, html: string) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
await transporter.sendMail({
|
|
|
|
from: process.env.EMAIL_USER,
|
|
|
|
to: to.join(", "),
|
|
|
|
subject: subject,
|
|
|
|
html: html
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error sending email:", error);
|
|
|
|
throw new Error("Failed to send email");
|
|
|
|
}
|
2024-08-26 13:11:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function POST(req: NextRequest) {
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
const { subject, html } = await req.json();
|
2024-08-26 13:11:47 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
// add ur email here
|
|
|
|
const testEmails = [
|
|
|
|
"abhijitbhattacharjee333@gmail.com",
|
|
|
|
"test2@example.com"
|
|
|
|
];
|
2024-08-26 13:11:47 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (testEmails.length === 0) {
|
|
|
|
console.error("No email addresses provided.");
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "No email addresses provided." },
|
|
|
|
{ status: 404 }
|
|
|
|
);
|
|
|
|
}
|
2024-08-26 13:11:47 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
await sendEmail(testEmails, subject, html);
|
|
|
|
return NextResponse.json({ message: "Emails sent successfully" });
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error handling POST request:", error);
|
|
|
|
return NextResponse.error();
|
|
|
|
}
|
2024-08-26 13:11:47 +02:00
|
|
|
}
|