feat: Simple nodemailer integration
This commit is contained in:
8
core/mail/mail.module.ts
Normal file
8
core/mail/mail.module.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { MailService } from "./mail.service";
|
||||
|
||||
@Module({
|
||||
providers: [MailService],
|
||||
exports: [MailService]
|
||||
})
|
||||
export class MailModule { }
|
||||
45
core/mail/mail.service.ts
Normal file
45
core/mail/mail.service.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import * as nodemailer from "nodemailer";
|
||||
|
||||
@Injectable()
|
||||
export class MailService {
|
||||
private transporter: nodemailer.Transporter;
|
||||
private mailServiceAvailable = false;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
const mailId = this.configService.get<string | undefined>("MAIL_ID");
|
||||
const mailPass = this.configService.get<string | undefined>("MAIL_PASS");
|
||||
|
||||
if (!mailId || !mailPass)
|
||||
throw new Error("Make sure MAIL_ID and MAIL_PASS environment variables are set")
|
||||
|
||||
// Use secure in prod
|
||||
// TODO: A table for failed emails to retry later(actually bullmq)
|
||||
this.transporter = nodemailer.createTransport({
|
||||
host: "smtp.gmail.com",
|
||||
port: 587,
|
||||
secure: false, // Use true for port 465, false for port 587
|
||||
auth: {
|
||||
user: mailId,
|
||||
pass: mailPass,
|
||||
},
|
||||
from: mailId
|
||||
});
|
||||
|
||||
this.mailServiceAvailable = true;
|
||||
}
|
||||
|
||||
sendMail({ to, subject, body }: { to: string, subject: string, body: string }) {
|
||||
if (!this.mailServiceAvailable)
|
||||
throw new Error("Mail service not available")
|
||||
|
||||
this.transporter.sendMail(
|
||||
{
|
||||
to,
|
||||
subject,
|
||||
html: body
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user