fix: sent emails now have unsubscription IDs instead of {unsubscribeId}

This commit is contained in:
Dorian Niemiec 2024-09-08 07:08:35 +02:00
parent 5048891b6e
commit 4a281182ec
3 changed files with 38 additions and 38 deletions

View file

@ -15,6 +15,7 @@ EMAIL_SECURE=
EMAIL_USER= EMAIL_USER=
EMAIL_PASS= EMAIL_PASS=
EMAIL_NEWSLETTER_ADDRESS= EMAIL_NEWSLETTER_ADDRESS=
EMAIL_NEWSLETTER_TESTDEST=
EMAIL_CONTACT_ADDRESS= EMAIL_CONTACT_ADDRESS=
EMAIL_CONTACT_DEST= EMAIL_CONTACT_DEST=

View file

@ -12,17 +12,26 @@ const transporter = nodemailer.createTransport({
} }
}); });
const sendEmail = async (to: string[], subject: string, html: string) => { const sendEmail = async (
try { to: { email: string; unsubscribeId: string }[],
await transporter.sendMail({ subject: string,
from: process.env.EMAIL_NEWSLETTER_ADDRESS, html: string
to: to.join(", "), ) => {
subject: subject, for (let i = 0; i < to.length; i++) {
html: html try {
}); await transporter.sendMail({
} catch (error) { from: process.env.EMAIL_NEWSLETTER_ADDRESS,
console.error("Error sending email:", error); to: to[i].email,
throw new Error("Failed to send email"); subject: subject,
html: html.replace(
/\{unsubscribeId\}/g,
encodeURIComponent(to[i].unsubscribeId)
)
});
} catch (error) {
console.error("Error sending email:", error);
throw new Error("Failed to send email");
}
} }
}; };
@ -35,7 +44,7 @@ export async function POST(req: NextRequest) {
const collection = db.collection("subscribers"); const collection = db.collection("subscribers");
const subscribers = await collection const subscribers = await collection
.find({}, { projection: { email: 1 } }) .find({}, { projection: { email: 1, unsubscribeId: 1 } })
.toArray(); .toArray();
if (subscribers.length === 0) { if (subscribers.length === 0) {
@ -46,17 +55,7 @@ export async function POST(req: NextRequest) {
); );
} }
const emails = subscribers.map((subscriber) => subscriber.email); await sendEmail(subscribers as any[], subject, html ?? "No HTML specified");
if (emails.length === 0) {
console.error("No email addresses found.");
return NextResponse.json(
{ message: "No email addresses found." },
{ status: 404 }
);
}
await sendEmail(emails, subject, html);
return NextResponse.json({ message: "Emails sent successfully" }); return NextResponse.json({ message: "Emails sent successfully" });
} catch (error) { } catch (error) {
console.error("Error handling POST request:", error); console.error("Error handling POST request:", error);

View file

@ -12,16 +12,18 @@ const transporter = nodemailer.createTransport({
}); });
const sendEmail = async (to: string[], subject: string, html: string) => { const sendEmail = async (to: string[], subject: string, html: string) => {
try { for (let i = 0; i < to.length; i++) {
await transporter.sendMail({ try {
from: process.env.EMAIL_NEWSLETTER_ADDRESS, await transporter.sendMail({
to: to.join(", "), from: process.env.EMAIL_NEWSLETTER_ADDRESS,
subject: subject, to: to[i],
html: html subject: subject,
}); html: html
} catch (error) { });
console.error("Error sending email:", error); } catch (error) {
throw new Error("Failed to send email"); console.error("Error sending email:", error);
throw new Error("Failed to send email");
}
} }
}; };
@ -29,11 +31,9 @@ export async function POST(req: NextRequest) {
try { try {
const { subject, html } = await req.json(); const { subject, html } = await req.json();
// add ur email here const testEmails = process.env.EMAIL_NEWSLETTER_TESTDEST
const testEmails = [ ? process.env.EMAIL_NEWSLETTER_TESTDEST.split(",")
"abhijitbhattacharjee333@gmail.com", : [];
"test2@example.com"
];
if (testEmails.length === 0) { if (testEmails.length === 0) {
console.error("No email addresses provided."); console.error("No email addresses provided.");