-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace nestjs mailer with nodemailer (#197)
* Replace nestjs mailer with nodemailer * Fix format date package * Remove @nestjs-modules/mailer in package.json
- Loading branch information
1 parent
fe430a7
commit 951a91d
Showing
8 changed files
with
125 additions
and
1,052 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 |
---|---|---|
@@ -1,43 +1,9 @@ | ||
import { MailerModule as MailModule } from '@nestjs-modules/mailer' | ||
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter' | ||
import { Module } from '@nestjs/common' | ||
import { ConfigModule } from '@nestjs/config' | ||
import { MailerService } from './mailer.service' | ||
import { ConfigModule, ConfigService } from '@nestjs/config' | ||
import { config as dotenvConfig } from 'dotenv' | ||
|
||
dotenvConfig({ path: '.env' }) | ||
|
||
@Module({ | ||
imports: [ | ||
MailModule.forRootAsync({ | ||
imports: [ConfigModule], | ||
inject: [ConfigService], | ||
useFactory: async (configService: ConfigService) => ({ | ||
transport: { | ||
host: configService.get('MAIL_HOST', 'localhost'), | ||
port: configService.get('MAIL_PORT', 1025), | ||
secure: false, | ||
...(configService.get('MAIL_USER') && { | ||
auth: { | ||
type: 'login', | ||
user: configService.get('MAIL_USER'), | ||
pass: configService.get('MAIL_PASSWORD') | ||
} | ||
}) | ||
}, | ||
defaults: { | ||
from: '"No Reply" <[email protected]>' | ||
}, | ||
template: { | ||
dir: __dirname + '/templates', | ||
adapter: new HandlebarsAdapter(), | ||
options: { | ||
strict: true | ||
} | ||
} | ||
}) | ||
}) | ||
], | ||
imports: [ConfigModule.forRoot()], | ||
providers: [MailerService], | ||
exports: [MailerService] | ||
}) | ||
|
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 |
---|---|---|
@@ -1,20 +1,86 @@ | ||
import { MailerService as Mailer } from '@nestjs-modules/mailer' | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common' | ||
import { UserEntity } from '../entities/user.entity' | ||
import * as nodemailer from 'nodemailer' | ||
import * as handlebars from 'handlebars' | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import { HandlebarsTemplate } from './types/mailer.types' | ||
import { ConfigService } from '@nestjs/config' | ||
|
||
@Injectable() | ||
export class MailerService { | ||
constructor(private mailerService: Mailer) {} | ||
private transporter: nodemailer.Transporter | ||
private templateCache: Map<HandlebarsTemplate, handlebars.TemplateDelegate> | ||
|
||
constructor(protected readonly configService: ConfigService) { | ||
this.templateCache = new Map() | ||
|
||
const host = this.configService.get<string>('MAIL_HOST', 'localhost') | ||
const port = this.configService.get<number>('MAIL_PORT', 1025) | ||
const secure = this.configService.get<boolean>('MAILER_SECURE', false) | ||
const user = this.configService.get<string>('MAIL_USER') | ||
const pass = this.configService.get<string>('MAIL_PASSWORD') | ||
const fromName = this.configService.get<string>( | ||
'MAIL_FROM_NAME', | ||
'No-reply' | ||
) | ||
const fromAddress = this.configService.get<string>( | ||
'MAIL_FROM_ADDRESS', | ||
'[email protected]' | ||
) | ||
|
||
this.transporter = nodemailer.createTransport( | ||
{ | ||
host, | ||
port, | ||
secure, | ||
auth: { | ||
user, | ||
pass | ||
} | ||
}, | ||
{ | ||
from: { | ||
name: fromName, | ||
address: fromAddress | ||
} | ||
} | ||
) | ||
} | ||
|
||
private loadTemplate(templateName: HandlebarsTemplate, data: object): string { | ||
if (this.templateCache.has(templateName)) { | ||
const templateRenderFunction = this.templateCache.get(templateName) | ||
|
||
return templateRenderFunction(data) | ||
} | ||
|
||
const templatesFolderPath = path.join(__dirname, './templates') | ||
const templatePath = path.join(templatesFolderPath, templateName) | ||
|
||
const templateSource = fs.readFileSync(templatePath, 'utf8') | ||
|
||
const templateRenderFunction = handlebars.compile(templateSource) | ||
this.templateCache.set(templateName, templateRenderFunction) | ||
|
||
const finalHtml = templateRenderFunction(data) | ||
|
||
return finalHtml | ||
} | ||
|
||
async sendEmail( | ||
user: UserEntity, | ||
subject: string, | ||
template: HandlebarsTemplate, | ||
data?: object | ||
) { | ||
const html = this.loadTemplate(template, data) | ||
|
||
async sendEmail(user: UserEntity, subject: string, template: string, data) { | ||
try { | ||
return await this.mailerService.sendMail({ | ||
await this.transporter.sendMail({ | ||
to: user.email, | ||
subject: subject, | ||
template: template, | ||
context: { | ||
...data | ||
} | ||
html: html | ||
}) | ||
} catch (err) { | ||
console.error(err) | ||
|
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,5 @@ | ||
export enum HandlebarsTemplate { | ||
PASSWORD_RESET_CODE = 'password-reset-code', | ||
WELCOME = 'welcome', | ||
EMAIL_CONFIRMATION = 'email-confirmation' | ||
} |
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
Oops, something went wrong.