-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PDT24-34 | Configure nodemailer service (#12)
* 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
1 parent
809a8d5
commit a34fcab
Showing
6 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
`, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters