Skip to content

Commit

Permalink
PDT24-34 | Configure nodemailer service (#12)
Browse files Browse the repository at this point in the history
* feat: configure nodemailer servie for sending email

* feat: create a basic email template for sending signup otp code

* chore: rename param names and add function documentation

* fix: use secure mailing port

* refactor: remove sendEmail call from main and improve sendEmail method
  • Loading branch information
noxiousghost authored Dec 31, 2024
1 parent 809a8d5 commit a34fcab
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/mailer/dto/mailer.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Address } from 'nodemailer/lib/mailer';
export class MailerDto {
sender?: Address;
recipients: Address[];
subject: string;
html: string;
text?: string;
}
8 changes: 8 additions & 0 deletions src/mailer/mailer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { MailerService } from './mailer.service';

@Module({
providers: [MailerService],
exports: [MailerService],
})
export class MailerModule {}
36 changes: 36 additions & 0 deletions src/mailer/mailer.service.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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 });
}
}
}
14 changes: 14 additions & 0 deletions src/template/email.template.ts
Original file line number Diff line number Diff line change
@@ -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 => `
<div>
<p>Welcome ${userName}</p>
<p>Use this code to verify your account:<br><b>${otpCode}</b>
</div>
`,
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a34fcab

Please sign in to comment.