All checks were successful
Build & Deploy KLZ Cables / deploy (push) Successful in 5m8s
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import nodemailer from "nodemailer";
|
|
import { render } from "@react-email/components";
|
|
import { ReactElement } from "react";
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.MAIL_HOST,
|
|
port: Number(process.env.MAIL_PORT),
|
|
secure: Number(process.env.MAIL_PORT) === 465,
|
|
auth: {
|
|
user: process.env.MAIL_USERNAME,
|
|
pass: process.env.MAIL_PASSWORD,
|
|
},
|
|
});
|
|
|
|
interface SendEmailOptions {
|
|
to?: string | string[];
|
|
subject: string;
|
|
template: ReactElement;
|
|
}
|
|
|
|
export async function sendEmail({ to, subject, template }: SendEmailOptions) {
|
|
const html = await render(template);
|
|
|
|
const recipients = to || process.env.MAIL_RECIPIENTS?.split(",") || [];
|
|
|
|
const mailOptions = {
|
|
from: process.env.MAIL_FROM,
|
|
to: recipients,
|
|
subject,
|
|
html,
|
|
};
|
|
|
|
try {
|
|
const info = await transporter.sendMail(mailOptions);
|
|
console.log("Email sent: %s", info.messageId);
|
|
return { success: true, messageId: info.messageId };
|
|
} catch (error) {
|
|
console.error("Error sending email:", error);
|
|
return { success: false, error };
|
|
}
|
|
}
|