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

45 lines
1 KiB
TypeScript
Raw Normal View History

2024-05-25 06:02:26 +00:00
import nodemailer from 'nodemailer';
import { injectable } from 'tsyringe';
2024-08-05 12:22:46 +00:00
import type { Email } from '../interfaces/email.interface';
import { config } from '../common/config';
2024-05-25 06:02:26 +00:00
2024-08-05 12:22:46 +00:00
type SendProps = {
2024-05-25 06:02:26 +00:00
to: string | string[];
2024-08-05 12:22:46 +00:00
email: Email;
}
2024-05-25 06:02:26 +00:00
@injectable()
export class MailerService {
2024-08-05 12:22:46 +00:00
private async sendDev({ to, email }: SendProps) {
const message = await 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'
}
}).sendMail({
from: '"Example" <example@ethereal.email>',
2024-05-25 06:02:26 +00:00
bcc: to,
2024-08-05 12:22:46 +00:00
subject: email.subject(),
text: email.html(),
html: email.html()
2024-05-25 06:02:26 +00:00
});
console.log(nodemailer.getTestMessageUrl(message));
}
2024-08-05 12:22:46 +00:00
private async sendProd({ to, email }: SendProps) {
// CONFIGURE MAILER
}
async send({ to, email }: SendProps) {
if (config.isProduction) {
await this.sendProd({ to, email });
} else {
await this.sendDev({ to, email });
}
2024-05-25 06:02:26 +00:00
}
}