/ASMC_mailer

Primary LanguageTypeScript

Email sender

Module for email sending with email template rendering from pug + sass

Example rendering

Input Source:

index.pug

body
  style=style

  table
    th
      td.left

      td.title
        h1 Hello
		
      td.right
        p Description

style.sass

.left
  width: 25px

.title h1
  font-size: 25px

.right
  width: 100px
  padding: 15px 5px

  p
    color: grey

Output Result:

<body><table><th></th><td class="left" style="width: 25px;" width="25"></td><td class="title"><h1 style="font-size: 25px;">Hello</h1></td><td class="right" style="width: 100px; padding: 15px 5px;" width="100"><p style="color: grey;">Description</p></td></table></body>

How it use

1. Structure directory with email templates

\---templates
	mailer.config.json
	+---first
		index.pug
		style.sass
	\---second
		index.pug
		style.sass

mailer.config.json must contain parameters for smtp connection which used by nodemailer inside "connect" key. The presence of a configuration file is not necessary, as will be described in the following paragraphs of the instruction.

{
  "connect": {
    // Nodemailer smtp parameters
  }
}

The source files of the templates are located inside the templates directory according to the following principles:

  • source files for each letter template must be located in separate folders
  • template folder contains:
    • index.pug - (required) - which be used as entry point for email markup
      • for append additional stylesheets index.pug must contains style=style definition.
    • style.sass - (optional) - entry point for stylesheets

2. Configuration for smtp client

There are two ways to configure the smtp connection:

  • configuration file templates/mailer.config.json - in this case the connection will be made automatically.
  • call the .connect() method - in this case, a method call must occur at least once.
import Mailer from "render-mail-template";

Mailer.connect({
  host: "smtp.gmail.com",
  port: 465,
  secure: true, // true for 465, false for other ports
  auth: {
    user: "", // generated ethereal user
    pass: "" // generated ethereal password
  }
});

As arguments for method .connect() are supported nodemailer parameters.

3. Send email from template

import Mailer from "render-mail-template";

const param = {
  // A set of parameters for passing to pug
};

Mailer.send({
  template: "first",
  from: "Happy Panda",
  subject: "Put title for email here",
  param
});

Method argument parameters:

  • template - (required) - folder name of letter template inside templates directory
  • from - (options) -
  • subject - (options) - letter subject
  • param - (options) - A set of parameters for passing to pug

Method .send() is returns a Promise, so it could be handled with .then().catch() expression or async/await.

4. Send email directly from nodemailer

import Mailer from "render-mail-template";

const options = {
  // A set of parameters for passing to pug
};

Mailer.sendMail(options);

As like previous method, .sendMail() is returns a Promise too.

Advanced usage

Sending from between few mailboxes

Regardless of whether the configuration file was used, each call to the .connect() method will apply the settings for connecting smtp for subsequent mailings.

Thus, email can be sent from different mailboxes

import Mailer from "render-mail-template";

Mailer.connect({
  host: "smtp.gmail.com",
  port: 465,
  secure: true,
  auth: {
    user: "user1@gmail.com",
    pass: "qwerty"
  }
});

Mailer.send(...) // Send email from user1@gmail.com

Mailer.connect({
  auth: {
    user: "user2@gmail.com",
    pass: "qwerty"
  }
});

Mailer.send(...) // Send email from user2@gmail.com

Fields that remain unchanged for the current and new connection can be skipped.

Because both mailboxes, from the example above, use the same mail service (gmail), connection parameters: host, port, secure; were not duplicated.

Typescript supported

This module includes .d.ts, additional @types definition module installation not needed.