Some checks failed
Build & Deploy KLZ Cables / build-and-deploy (push) Failing after 1m46s
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import nodemailer from "nodemailer";
|
|
import { render } from "@react-email/components";
|
|
import { ReactElement } from "react";
|
|
import { getServerAppServices } from "@/lib/services/create-services.server";
|
|
import { config } from "../config";
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: config.mail.host,
|
|
port: config.mail.port,
|
|
secure: config.mail.port === 465,
|
|
auth: {
|
|
user: config.mail.user,
|
|
pass: config.mail.pass,
|
|
},
|
|
});
|
|
|
|
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 || config.mail.recipients;
|
|
|
|
const mailOptions = {
|
|
from: config.mail.from,
|
|
to: recipients,
|
|
subject,
|
|
html,
|
|
};
|
|
|
|
const logger = getServerAppServices().logger.child({ component: 'mailer' });
|
|
|
|
try {
|
|
const info = await transporter.sendMail(mailOptions);
|
|
logger.info("Email sent successfully", { messageId: info.messageId, subject, recipients });
|
|
return { success: true, messageId: info.messageId };
|
|
} catch (error) {
|
|
logger.error("Error sending email", { error, subject, recipients });
|
|
return { success: false, error };
|
|
}
|
|
}
|