Skip to content

Commit

Permalink
feat(mail): @nestjs-modules/mailer integration
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
ZenSoftware committed Aug 8, 2022
1 parent b85bb80 commit b91dedc
Show file tree
Hide file tree
Showing 17 changed files with 3,781 additions and 2,050 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ npm start
- [x] Example [Kubernetes](https://kubernetes.io/) deployment scripts.
- [x] Enforced code formatting via [Prettier](https://prettier.io/) & [import-sort](https://www.npmjs.com/package/prettier-plugin-import-sort), guaranteeing all contributions to the project are standardized with predictable structure, and clear legibility.
- [x] [Commitizen](https://github.com/commitizen/cz-cli) for standardizing git commits.
- [x] [@nest-modules/mailer](https://www.npmjs.com/package/@nest-modules/mailer) for automated emailing, and pre-configured [Handlebars e-mail templates](https://handlebarsjs.com/guide/#what-is-handlebars) for the various web portal's emailing needs. Handlebars has similar double bracket `{{contextField}}` template interpolation, akin to Angular templates. Custom HTML e-mail triggers within Nest are made very simple by simply passing a `JSON context` and the template's `file name` as parameters to the class injectable `MailService`. The [apps/api/src/app/mail/templates](https://github.com/ZenSoftware/Zen/tree/main/apps/api/src/app/mail/templates) directory is a **conventions first approach**.
- [x] [@nestjs-modules/mailer](hhttps://github.com/nest-modules/mailer) for automated emailing, and pre-configured [Handlebars e-mail templates](https://handlebarsjs.com/guide/#what-is-handlebars) for the various web portal's emailing needs. Handlebars has similar double bracket `{{contextField}}` template interpolation, akin to Angular templates. Custom HTML e-mail triggers within Nest are made very simple by simply passing a `JSON context` and the template's `file name` as parameters to the class injectable `MailService`.
- [x] [Compodoc](https://compodoc.app/) to generate Nest & Angular documentation.
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/api/src/app/mail/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './mail.module';
export * from './mail.service';
export * from './templates';
export * from './contexts';
5 changes: 3 additions & 2 deletions apps/api/src/app/mail/mail.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';

import { HandlebarsAdapter, MailerModule } from '@nest-modules/mailer';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { Module } from '@nestjs/common';

import { ConfigModule, ConfigService } from '../config';
Expand All @@ -15,7 +16,7 @@ import { MailService } from './mail.service';
...config.mail,

template: {
dir: path.join(__dirname, 'mail/templates'),
dir: path.join(__dirname, 'assets/mail'),
adapter: new HandlebarsAdapter(),
options: {
strict: true,
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/mail/mail.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ISendMailOptions, MailerService } from '@nest-modules/mailer';
import { ISendMailOptions, MailerService } from '@nestjs-modules/mailer';
import { Injectable, Logger } from '@nestjs/common';
import { User } from '@prisma/client';

import { ConfigService } from '../config';
import { JwtService } from '../jwt';
import { GeneralContext, PasswordResetContext } from './templates';
import { GeneralContext, PasswordResetContext } from './contexts';

type MailOptions = ISendMailOptions & { template?: string };

Expand Down
Empty file removed apps/api/src/assets/.gitkeep
Empty file.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/api/src/environments/environment.base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailerOptions } from '@nest-modules/mailer';
import { MailerOptions } from '@nestjs-modules/mailer';
import { NestApplicationOptions } from '@nestjs/common';
import { JwtModuleOptions } from '@nestjs/jwt';
import { ThrottlerModuleOptions } from '@nestjs/throttler';
Expand Down
11 changes: 10 additions & 1 deletion apps/api/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ export const environment: EnvironmentBase = {
},
expiresInRememberMe: 2592000, // 30 days (in seconds)
mail: {
transport: `smtps://${process.env.SMTP_LOGIN}:${process.env.SMTP_PASSWORD}@${process.env.SMTP_SERVER}`,
// Docs: https://nodemailer.com/smtp/
transport: {
host: process.env.SMTP_SERVER,
port: 587,
secure: false, // true for port 465, false for other ports
auth: {
user: process.env.SMTP_LOGIN,
pass: process.env.SMTP_PASSWORD,
},
},
defaults: {
from: `"${process.env.SMTP_FROM_NAME}" <${process.env.SMTP_FROM_EMAIL}>`,
},
Expand Down
11 changes: 10 additions & 1 deletion apps/api/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ export const environment: EnvironmentBase = {
},
expiresInRememberMe: 2592000, // 30 days (in seconds)
mail: {
transport: `smtps://${process.env.SMTP_LOGIN}:${process.env.SMTP_PASSWORD}@${process.env.SMTP_SERVER}`,
// Docs: https://nodemailer.com/smtp/
transport: {
host: process.env.SMTP_SERVER,
port: 587,
secure: false, // true for port 465, false for other ports
auth: {
user: process.env.SMTP_LOGIN,
pass: process.env.SMTP_PASSWORD,
},
},
defaults: {
from: `"${process.env.SMTP_FROM_NAME}" <${process.env.SMTP_FROM_EMAIL}>`,
},
Expand Down
1 change: 0 additions & 1 deletion deploy/deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function Invoke-Call {
Invoke-Call -ScriptBlock { npx gulp clean } -ErrorAction Stop
Invoke-Call -ScriptBlock { npm run prod:apps } -ErrorAction Stop
Invoke-Call -ScriptBlock { npx nx build api --prod } -ErrorAction Stop
Invoke-Call -ScriptBlock { npx gulp handlebars:copy } -ErrorAction Stop
Invoke-Call -ScriptBlock { npx gulp create-deploy-package } -ErrorAction Stop
Invoke-Call -ScriptBlock { docker build -f deploy/api/Dockerfile -t zen-api . } -ErrorAction Stop
Invoke-Call -ScriptBlock { azcopy cp 'dist/apps/portal/*' 'https://hot.blob.core.windows.net/$web' --recursive } -ErrorAction Stop
Expand Down
Loading

0 comments on commit b91dedc

Please sign in to comment.