Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zepochs/nestjs-mailer
🌈 A simple implementation example with and without email-templates using mailer module for nest js built on top of nodemailer.
https://github.com/zepochs/nestjs-mailer
ejs handlebars kit nestjs nestjs-mailer nestjsmailer nodemailer outlook pug sender smtp
Last synced: 1 day ago
JSON representation
🌈 A simple implementation example with and without email-templates using mailer module for nest js built on top of nodemailer.
- Host: GitHub
- URL: https://github.com/zepochs/nestjs-mailer
- Owner: zepochs
- License: mit
- Created: 2020-02-28T19:20:16.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T01:30:48.000Z (almost 2 years ago)
- Last Synced: 2024-10-30T02:28:24.550Z (15 days ago)
- Topics: ejs, handlebars, kit, nestjs, nestjs-mailer, nestjsmailer, nodemailer, outlook, pug, sender, smtp
- Language: Handlebars
- Homepage: https://github.com/nest-modules/mailer
- Size: 1.16 MB
- Stars: 120
- Watchers: 4
- Forks: 30
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Demo implementation on the mailer modules for Nest framework (node.js) using Nodemailer library## Nestjs-mailer starter kit / project for your NestJs project.
### Goals
The main goal of this kit is to quickly get you started on your project with Nestjs Mailer, bringing a solid layout foundation to work upon.
### Usage
Import the MailerModule into the root AppModule
Synchronous import
```javascript
//app.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';@Module({
imports: [
MailerModule.forRoot({
transport: {
host: 'smtp.example.com',
port: 587,
secure: false, // upgrade later with STARTTLS
auth: {
user: "username",
pass: "password",
},
},
defaults: {
from:'"nest-modules" ',
},
template: {
dir: process.cwd() + '/templates/',
adapter: new HandlebarsAdapter(), // or new PugAdapter()
options: {
strict: true,
},
},
}),
],
})
export class AppModule {}
```Asynchronous import
```typescript
//app.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';@Module({
imports: [
MailerModule.forRootAsync({
useFactory: () => ({
transport: {
host: 'smtp.example.com',
port: 587,
secure: false, // upgrade later with STARTTLS
auth: {
user: "username",
pass: "password",
},
},
defaults: {
from:'"nest-modules" ',
},
template: {
dir: process.cwd() + '/templates/',
adapter: new HandlebarsAdapter(), // or new PugAdapter() or new EjsAdapter()
options: {
strict: true,
},
},
}),
}),
],
})
export class AppModule {}
```* We have used Handlebars in above example, for EJS and Pug use below mentioned example of adapter import
Pug
```javascript
//app.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { PugAdapter } from '@nestjs-modules/mailer/dist/adapters/pug.adapter';```
EJS
```javascript
//app.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { EjsAdapter } from '@nestjs-modules/mailer/dist/adapters/ejs.adapter';```
After this, MailerService will be available to inject across entire project, for example in this way :
```typescript
import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';@Injectable()
export class ExampleService {
constructor(private readonly mailerService: MailerService) {}
}
```MailerProvider exports the `sendMail()` function to which you can pass the message options (sender, email subject, recipient, body content, etc)
`sendMail()` accepts the same fields as [nodemailer email message](https://nodemailer.com/message/)
```typescript
import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';@Injectable()
export class ExampleService {
constructor(private readonly mailerService: MailerService) {}
public example(): void {
this
.mailerService
.sendMail({
to: '[email protected]', // list of receivers
from: '[email protected]', // sender address
subject: 'Testing Nest MailerModule ✔', // Subject line
text: 'welcome', // plaintext body
html: 'welcome', // HTML body content
})
.then((success) => {
console.log(success)
})
.catch((err) => {
console.log(err)
});
}
public example2(): void {
this
.mailerService
.sendMail({
to: '[email protected]',
from: '[email protected]',
subject: 'Testing Nest Mailermodule with template ✔',
template: 'index', // The `.pug` or `.hbs` extension is appended automatically.
context: { // Data to be sent to template engine.
code: 'cf1a3f828287',
username: 'john doe',
},
})
.then((success) => {
console.log(success)
})
.catch((err) => {
console.log(err)
});
}
public example3(): void {
this
.mailerService
.sendMail({
to: '[email protected]',
from: '[email protected]',
subject: 'Testing Nest Mailermodule with template ✔',
template: '/index', // The `.pug` or `.hbs` extension is appended automatically.
context: { // Data to be sent to template engine.
code: 'cf1a3f828287',
username: 'john doe',
},
})
.then((success) => {
console.log(success)
})
.catch((err) => {
console.log(err)
});
}
}
```Make a template named folder at the root level of the project and keep all the email-templates in the that folder with `.hbs` extension.
This implementation uses handlebars as a view-engine and outlook as the smtp.### Configuration
Dotenv 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 Nodemailer / smtp.
*Special thanks to https://github.com/leemunroe/responsive-html-email-template for providing email-templates*