0.2.1 • Published 2 years ago

@sempervirens/emailer v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Sempervirens Emailer

A wrapper for Nodemailer, it provides a simplified interface for sending emails.

Tests badge Version badge

Features

  • Checks if the body parameter is HTML or plain text, and if plain text, it adds the plain text to the email in case the recipient client is not HTML compatible.
  • Enables creating an Emailer instance that can be pre-configured and used across the process.
  • Enables calling only send without pre-configuring.
  • Provides Gmail as the default service.

Installation

npm i @sempervirens/emailer

Usage

Quick Start

  1. Setup a Gmail account with an app password.

  2. Import Emailer.

  3. (Optional) Initialize emailer with name, from, and password so those parameters may be omitted when calling send.

  4. Call send, with or without name, from, and password.

Note: If initialized, it is only necessary to pass name, from, and password into emailer once. Then importing emailer anywhere else, it is only necessary to call send.

import emailer from '@sempervirens/emailer';

(async () => {

  // Without initializing

  const data1 = await Emailer.send({
    name: 'From name',
    from: 'From email',
    password: 'Gmail app password',
    to: 'To email',
    subject: 'Email subject',
    body: 'Email text or HTML'
  });

  // With initializing

  const emailer = new Emailer({
    name: 'From name',
    from: 'From email',
    password: 'Gmail app password'
  });

  // In a file somewhere else
  const data2 = await emailer.send({
    to: 'To email',
    subject: 'Email subject',
    body: 'Email text or HTML'
  });

})();

Advanced

const data3 = await Emailer.send({
  options: {
    service: 'gmail',
    auth: {
      user: 'From email',
      auth: 'Gmail app password'
    }
  },
  to: 'To email',
  subject: 'Email subject',
  body: 'Email text or HTML'
});

API

constructor

ParamTypeDescription
namestringRequired if options not given. From name to be displayed with from email.
fromstringRequired if options not given. From email.
passwordstringRequired if options not given. From email password or app password.
optionsobjectRequired if name, from, password not given. Nodemailer options.

send (static or instance)

Note: If using the static function, name, from, password, etc. are required.

ParamTypeDescription
namestringRequired if static and options not given. From name to be displayed with from email.
fromstringRequired if static and options not given. From email.
passwordstringRequired if static and options not given. From email password or app password.
optionsobjectRequired if static and name, from, password not given. Nodemailer options.
tostringRequired. The to email address.
subjectstringRecommended. The email subject.
bodystringRecommended. HTML or plain text email body.