43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import nodemailer from 'nodemailer';
|
|
import { getServerAppServices } from '@/lib/services/create-services.server';
|
|
import { config } from '../config';
|
|
import { ReactElement } from 'react';
|
|
|
|
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;
|
|
html: string;
|
|
}
|
|
|
|
export async function sendEmail({ to, subject, html }: SendEmailOptions) {
|
|
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 };
|
|
}
|
|
}
|