diff --git a/package.json b/package.json index 267b156..0d6f1b0 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "nest-winston": "^1.9.7", + "nodemailer": "^6.9.16", "pg": "^8.13.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1", @@ -51,6 +52,7 @@ "@types/bcrypt": "^5.0.2", "@types/express": "^5.0.0", "@types/node": "^20.3.1", + "@types/nodemailer": "^6.4.17", "@typescript-eslint/eslint-plugin": "^8.0.0", "@typescript-eslint/parser": "^8.0.0", "eslint": "^8.0.0", diff --git a/src/mailer/dto/mailer.dto.ts b/src/mailer/dto/mailer.dto.ts new file mode 100644 index 0000000..55f7fa5 --- /dev/null +++ b/src/mailer/dto/mailer.dto.ts @@ -0,0 +1,8 @@ +import { Address } from 'nodemailer/lib/mailer'; +export class MailerDto { + sender?: Address; + recipients: Address[]; + subject: string; + html: string; + text?: string; +} diff --git a/src/mailer/mailer.module.ts b/src/mailer/mailer.module.ts new file mode 100644 index 0000000..0e7e334 --- /dev/null +++ b/src/mailer/mailer.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common'; +import { MailerService } from './mailer.service'; + +@Module({ + providers: [MailerService], + exports: [MailerService], +}) +export class MailerModule {} diff --git a/src/mailer/mailer.service.ts b/src/mailer/mailer.service.ts new file mode 100644 index 0000000..be0d4c8 --- /dev/null +++ b/src/mailer/mailer.service.ts @@ -0,0 +1,36 @@ +import { createTransport, SendMailOptions, Transporter } from 'nodemailer'; +import { Logger } from '@nestjs/common'; +import { MailerDto } from '@/mailer/dto/mailer.dto'; + +export class MailerService { + private readonly logger = new Logger(); + transporter: Transporter = createTransport({ + host: process.env.EMAIL_HOST, + port: 465, + secure: true, + auth: { + user: process.env.EMAIL_USER, + pass: process.env.EMAIL_PASS, + }, + }); + + async sendEmail(data: MailerDto): Promise { + try { + const { sender, recipients, subject, html, text } = data; + const mailOptions: SendMailOptions = { + from: sender ?? { + name: process.env.EMAIL_SENDER_NAME as string, + address: process.env.EMAIL_USER as string, + }, + to: recipients, + subject, + html, + text, + }; + await this.transporter.sendMail(mailOptions); + this.logger.log('Mail sent successfully'); + } catch (error) { + this.logger.error(`Error sending email:`, { error }); + } + } +} diff --git a/src/template/email.template.ts b/src/template/email.template.ts new file mode 100644 index 0000000..3cb9ecc --- /dev/null +++ b/src/template/email.template.ts @@ -0,0 +1,14 @@ +export const signupOtpMailTemplate = { + subject: 'Verify your account', + /** + * Generates a random OTP of the specified size. + * @param otpCode - code to send to through email. + * @param userName - name of the user to send the email to. + */ + body: (otpCode: number, userName: string): string => ` +
+

Welcome ${userName}

+

Use this code to verify your account:
${otpCode} +

+ `, +}; diff --git a/yarn.lock b/yarn.lock index c8011cd..4d6f9af 100644 --- a/yarn.lock +++ b/yarn.lock @@ -626,6 +626,13 @@ dependencies: undici-types "~6.19.2" +"@types/nodemailer@^6.4.17": + version "6.4.17" + resolved "https://registry.yarnpkg.com/@types/nodemailer/-/nodemailer-6.4.17.tgz#5c82a42aee16a3dd6ea31446a1bd6a447f1ac1a4" + integrity sha512-I9CCaIp6DTldEg7vyUTZi8+9Vo0hi1/T8gv3C89yk1rSAAzoKQ8H8ki/jBYJSFoH/BisgLP8tkZMlQ91CIquww== + dependencies: + "@types/node" "*" + "@types/qs@*": version "6.9.17" resolved "https://registry.npmjs.org/@types/qs/-/qs-6.9.17.tgz" @@ -3165,6 +3172,11 @@ node-releases@^2.0.18: resolved "https://registry.npmjs.org/node-releases/-/node-releases-2.0.19.tgz" integrity sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw== +nodemailer@^6.9.16: + version "6.9.16" + resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.9.16.tgz#3ebdf6c6f477c571c0facb0727b33892635e0b8b" + integrity sha512-psAuZdTIRN08HKVd/E8ObdV6NO7NTBY3KsC30F7M4H1OnmLCUNaS56FpYxyb26zWLSyYF9Ozch9KYHhHegsiOQ== + nopt@^5.0.0: version "5.0.0" resolved "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz"