npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yops/nest-mailer

v0.1.21

Published

A mailer module for Nest framework (node.js) inspired from https://github.com/nest-modules/mailer.git

Downloads

12

Readme

Nest mailer module

A mailer module for Nest framework (node.js)

Nest MailerModule provide a wrapper around nodemailer used for send email with support for Lodash template files.

Installation

npm install --save @yops/nest-mailer

Usage

Import the MailerModule into the root ApplicationModule.

//app.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@yops/nest-mailer';

@Module({
  imports: [
    MailerModule.forRoot({
      transport: 'smtps://user%40gmail.com:[email protected]',
      defaults: {
        from:'"nest-modules" <[email protected]>',
      },
      templateDir: './src/common/email-templates'
    }),
  ],
})
export class ApplicationModule {}

The forRoot() method accepts a configuration JSON object with the following attributes:

transport is the transport configuration object, connection url or a transport plugin instance

defaults is an optional object of message data fields that are set for every message sent

templateDir is the path to directory where you have put your templates; the default value is /public/templates if not specified.

For more details about transporters and defaults values please visit: nodemailer

Futhermore, instead of passing anything to the forRoot(), we can create an mailerconfig.js file in the project root directory.

// mailerconfig.js
module.exports = {
  transport: {
    host: 'smtp.example.com',
    port: 587,
    secure: false,
    auth: {
        user: 'username',
        pass: 'password'
    }
  },
  defaults: {
    forceEmbeddedImages: true,
    from:'"nest-modules" <[email protected]>',
  },
  templateDir: './src/common/email-templates'
}

Now we can simply leave the parenthesis empty:

// app.module.ts
app.module.ts JavaScript TypeScript

import { Module } from '@nestjs/common';
import { MailerModule } from '@nest-modules/mailer';

@Module({
  imports: [MailerModule.forRoot()],
})
export class ApplicationModule {}

Afterwards, MailerProvider will be available to inject across entire project (without importing any module elsewhere), for example in this way:

@Inject('MailerProvider') private readonly mailerProvider

Sending messages:

MailerProvider exports the sendMail() function to which you can pass the message options (sender, email subject, recipient, body content, etc)

sendMail() acept the same fields of an nodemailer email message

ex:

this.mailerProvider.sendMail({
  to: '[email protected]', // sender address
  from: '[email protected]', // list of receivers
  subject: 'Testing Nest MailerModule ✔', // Subject line
  text: 'welcome', // plaintext body
  html: '<b>welcome</b>' // HTML body content
})

This method returns a Promise object

Templating:

MailerModule renders lodash templates using the data specified in the context message object

ex:

this.mailerProvider.sendMail({
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Testing Nest Mailermodule with template ✔',
  template: 'welcome.html',
  context: {  // Data to be sent to template files.
    username: 'john doe',
    code: 'cf1a3f828287'
  }
})

where:

template is a name from template file (without extension)

context is an object with dynamic content which will be bing to templates

Now create a html template in your templateDir, on this case:

<templateDir>/welcome.html

Put this code in your template:

<p>Welcome <%= username %>, your activation code is <%= code %></p>

Pug will compile the template to html code and return the body of message

The result is:

<p>Welcome john doe, your activation code is cf1a3f828287</p>

Using a transport plugin instance:

In some cases you will want to use a nodemailer transport plugin, such as mandrill, sendgrid, mailgun, etc.

You must only create the instance and send it to the transport value.

ex:

npm install --save nodemailer-mandrill-transport
//mailerconfig.js
import * as mandrillTransport from 'nodemailer-mandrill-transport'

module.exports = {
  transport: mandrillTransport({
    auth: {
      api_key: 'key'
    }
  }),
  defaults: {
    from:'"nest-mailer" <[email protected]>',
  },
  templateDir: './src/common/email-templates'
}

OR an example for testing

ex:

npm install --save nodemailer-mock-transport
//mailerconfig.js
import * as mockMail from 'nodemailer-mock-transport'

module.exports = {
  transport: mockMail(),
  defaults: {
    from:'"nest-mailer" <[email protected]>',
  },
  templateDir: './src/common/email-templates'
}