TofuStack/src/lib/server/api/services/mailer.service.ts

61 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-05-25 06:02:26 +00:00
import nodemailer from 'nodemailer';
import { injectable } from 'tsyringe';
import handlebars from 'handlebars';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
type SendMail = {
to: string | string[];
subject: string;
html: string;
};
type SendTemplate<T> = {
to: string | string[];
props: T;
};
@injectable()
export class MailerService {
private nodemailer = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // Use `true` for port 465, `false` for all other ports
auth: {
user: 'adella.hoppe@ethereal.email',
pass: 'dshNQZYhATsdJ3ENke'
}
});
sendEmailVerification(data: SendTemplate<{ token: string }>) {
const template = handlebars.compile(this.getTemplate('email-verification'));
return this.send({
to: data.to,
subject: 'Email Verification',
html: template({ token: data.props.token })
});
}
private async send({ to, subject, html }: SendMail) {
const message = await this.nodemailer.sendMail({
from: '"Example" <example@ethereal.email>', // sender address
bcc: to,
subject, // Subject line
text: html,
html
});
console.log(nodemailer.getTestMessageUrl(message));
}
private getTemplate(template: string) {
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
return fs.readFileSync(
2024-05-25 06:02:33 +00:00
path.join(__dirname, `../infrastructure/email-templates/${template}.handlebars`),
2024-05-25 06:02:26 +00:00
'utf-8'
);
}
}