This commit is contained in:
41
lib/mail/mailer.ts
Normal file
41
lib/mail/mailer.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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 };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user