{"id":19898896,"url":"https://github.com/zepochs/nestjs-mailer","last_synced_at":"2025-05-02T22:31:17.605Z","repository":{"id":39946369,"uuid":"243837821","full_name":"zepochs/nestjs-mailer","owner":"zepochs","description":"🌈  A simple implementation example with and without email-templates using mailer module for nest js built on top of nodemailer.","archived":false,"fork":false,"pushed_at":"2023-01-24T01:30:48.000Z","size":1221,"stargazers_count":125,"open_issues_count":14,"forks_count":30,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-07T07:43:05.989Z","etag":null,"topics":["ejs","handlebars","kit","nestjs","nestjs-mailer","nestjsmailer","nodemailer","outlook","pug","sender","smtp"],"latest_commit_sha":null,"homepage":"https://github.com/nest-modules/mailer","language":"Handlebars","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zepochs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-28T19:20:16.000Z","updated_at":"2025-03-04T23:36:10.000Z","dependencies_parsed_at":"2023-02-13T09:00:26.669Z","dependency_job_id":null,"html_url":"https://github.com/zepochs/nestjs-mailer","commit_stats":null,"previous_names":["zepochs/nestjs-mailer"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zepochs%2Fnestjs-mailer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zepochs%2Fnestjs-mailer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zepochs%2Fnestjs-mailer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zepochs%2Fnestjs-mailer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zepochs","download_url":"https://codeload.github.com/zepochs/nestjs-mailer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252116210,"owners_count":21697333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ejs","handlebars","kit","nestjs","nestjs-mailer","nestjsmailer","nodemailer","outlook","pug","sender","smtp"],"created_at":"2024-11-12T20:04:32.597Z","updated_at":"2025-05-02T22:31:12.592Z","avatar_url":"https://github.com/zepochs.png","language":"Handlebars","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\n    \u003cimg src=\"https://nestjs.com/img/logo_text.svg\" width=\"320\" alt=\"Nest Logo\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  Demo implementation on the mailer modules for Nest framework (node.js) using \u003ca href=\"https://nodemailer.com/\"\u003eNodemailer\u003c/a\u003e library\n\u003c/p\u003e\n\n## Nestjs-mailer starter kit / project for your NestJs project.\n\n### Goals\n\nThe main goal of this kit is to \u003cstrong\u003equickly get you started on your project\u003c/strong\u003e with \u003ca href=\"https://github.com/nest-modules/mailer/\" target=\"_blank\"\u003eNestjs Mailer\u003c/a\u003e, bringing a \u003cstrong\u003esolid layout foundation\u003c/strong\u003e to work upon.\n\n### Usage\n\nImport the MailerModule into the root AppModule\n\nSynchronous import\n\n```javascript\n//app.module.ts\nimport { Module } from '@nestjs/common';\nimport { MailerModule } from '@nestjs-modules/mailer';  \nimport { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';\n\n@Module({\n  imports: [\n    MailerModule.forRoot({\n      transport: {\n        host: 'smtp.example.com',\n        port: 587,\n        secure: false, // upgrade later with STARTTLS\n        auth: {\n          user: \"username\",\n          pass: \"password\",\n        },\n      },\n      defaults: {\n        from:'\"nest-modules\" \u003cmodules@nestjs.com\u003e',\n      },\n      template: {\n        dir: process.cwd() + '/templates/',\n        adapter: new HandlebarsAdapter(), // or new PugAdapter()\n        options: {\n          strict: true,\n        },\n      },\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\nAsynchronous import\n\n```typescript\n//app.module.ts\nimport { Module } from '@nestjs/common';\nimport { MailerModule } from '@nestjs-modules/mailer';  \nimport { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';\n\n@Module({\n  imports: [\n    MailerModule.forRootAsync({\n      useFactory: () =\u003e ({\n        transport: {\n          host: 'smtp.example.com',\n          port: 587,\n          secure: false, // upgrade later with STARTTLS\n          auth: {\n            user: \"username\",\n            pass: \"password\",\n          },\n        },\n        defaults: {\n          from:'\"nest-modules\" \u003cmodules@nestjs.com\u003e',\n        },\n        template: {\n          dir: process.cwd() + '/templates/',\n          adapter: new HandlebarsAdapter(), // or new PugAdapter() or new EjsAdapter()\n          options: {\n            strict: true,\n          },\n        },\n      }),\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n* We have used Handlebars in above example, for EJS and Pug use below mentioned example of adapter import\n\nPug\n\n```javascript\n//app.module.ts\nimport { Module } from '@nestjs/common';\nimport { MailerModule } from '@nestjs-modules/mailer';  \nimport { PugAdapter } from '@nestjs-modules/mailer/dist/adapters/pug.adapter';\n\n```\n\nEJS\n\n```javascript\n//app.module.ts\nimport { Module } from '@nestjs/common';\nimport { MailerModule } from '@nestjs-modules/mailer';  \nimport { EjsAdapter } from '@nestjs-modules/mailer/dist/adapters/ejs.adapter';\n\n```\n\n\nAfter this, MailerService will be available to inject across entire project, for example in this way : \n\n```typescript\nimport { Injectable } from '@nestjs/common';\nimport { MailerService } from '@nestjs-modules/mailer';\n\n@Injectable()\nexport class ExampleService {\n  constructor(private readonly mailerService: MailerService) {}\n}\n```\n\nMailerProvider exports the `sendMail()` function to which you can pass the message options (sender, email subject, recipient, body content, etc)\n\n`sendMail()` accepts the same fields as [nodemailer email message](https://nodemailer.com/message/)\n\n```typescript\nimport { Injectable } from '@nestjs/common';\nimport { MailerService } from '@nestjs-modules/mailer';\n\n@Injectable()\nexport class ExampleService {\n  constructor(private readonly mailerService: MailerService) {}\n  \n  public example(): void {\n    this\n      .mailerService\n      .sendMail({\n        to: 'test@nestjs.com', // list of receivers\n        from: 'noreply@nestjs.com', // sender address\n        subject: 'Testing Nest MailerModule ✔', // Subject line\n        text: 'welcome', // plaintext body\n        html: '\u003cb\u003ewelcome\u003c/b\u003e', // HTML body content\n      })\n      .then((success) =\u003e {\n        console.log(success)\n      })\n      .catch((err) =\u003e {\n        console.log(err)\n      });\n  }\n  \n  public example2(): void {\n    this\n      .mailerService\n      .sendMail({\n        to: 'test@nestjs.com',\n        from: 'noreply@nestjs.com',\n        subject: 'Testing Nest Mailermodule with template ✔',\n        template: 'index', // The `.pug` or `.hbs` extension is appended automatically.\n        context: {  // Data to be sent to template engine.\n          code: 'cf1a3f828287',\n          username: 'john doe',\n        },\n      })\n       .then((success) =\u003e {\n        console.log(success)\n      })\n      .catch((err) =\u003e {\n        console.log(err)\n      });\n  }\n  \n  public example3(): void {\n    this\n      .mailerService\n      .sendMail({\n        to: 'test@nestjs.com',\n        from: 'noreply@nestjs.com',\n        subject: 'Testing Nest Mailermodule with template ✔',\n        template: '/index', // The `.pug` or `.hbs` extension is appended automatically.\n        context: {  // Data to be sent to template engine.\n          code: 'cf1a3f828287',\n          username: 'john doe',\n        },\n      })\n      .then((success) =\u003e {\n        console.log(success)\n      })\n      .catch((err) =\u003e {\n        console.log(err)\n      });\n  }\n}\n```\n\nMake a template named folder at the root level of the project and keep all the email-templates in the that folder with `.hbs` extension.\nThis implementation uses handlebars as a view-engine and outlook as the smtp.\n\n\n### Configuration\n\nDotenv module has been used for sender's email and password keep. This kit is implemented with outlook smtp, while we can make changes in the `app.module.ts` configurations for other services. As the nestjs-mailer is built on top of nodemailer, the required configurations can be found here \u003ca href=\"https://nodemailer.com/smtp/\"\u003eNodemailer / smtp\u003c/a\u003e. \n\n*Special thanks to https://github.com/leemunroe/responsive-html-email-template for providing email-templates*\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzepochs%2Fnestjs-mailer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzepochs%2Fnestjs-mailer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzepochs%2Fnestjs-mailer/lists"}