@b-stable/nodemailer-mailgun-transport-ts

1.0.0 • Public • Published

Installation

To use nodemailer-mailgun-transport, all you need to do is install the @b-stable/nodemailer-mailgun-transport-ts package

$ yarn add @b-stable/nodemailer-mailgun-transport-ts

# or

$ npm i @b-stable/nodemailer-mailgun-transport-ts

Usage

import nodemailer from 'nodemailer';
import { MailgunTransport } from '@b-stable/nodemailer-mailgun-transport-ts';

// This is your options that you retrieve from www.mailgun.com
const options = {
    auth: {
        domain: 'domain-name',
        api_key: '123456789',
    },
    host: 'api.mailgun.net',
};

const nodemailerMailgun = nodemailer.createTransport(new MailgunTransport(options));

nodemailerMailgun.sendMail({
    from: 'myemail@example.com',
    to: 'recipient@domain.com', // An array if you have multiple recipients.
    cc:'second@domain.com',
    bcc:'secretagent@company.gov',
    subject: 'Hey you, awesome!',
    'replyTo': 'reply2this@company.com',
    //You can use "html:" to send HTML email content. It's magic!
    html: '<b>Wow Big powerful letters</b>',
    //You can use "text:" to send plain-text content. It's oldschool!
    text: 'Mailgun rocks, pow pow!'
}, (err, info) => {
    if (err) {
        console.log(`Error: ${err}`);
    }
    else {
        console.log(`Response: ${info}`);
    }
});

The "from", "to", "cc", and "bcc" fields support an address object or array of address objects. Each "name" and "address" are converted to "name <address>" format. "name" is optional, "address" is required. Missing or null address in object is skipped.

Example:

 from: {name: 'Sales', address: 'sales@example.com'},
 to: [{name:'Mary', address:'mary@differentexample.com'}, {address:'john@anotherexample.com'}]

Is converted to:

 from: 'Sales <sales@example.com>',
 to: 'Mary <mary@differentexample.com>,john@anotherexample.com'

To use consolidate.js templates locally, give the template key an object instead that contains a name key, an engine key and, optionally, a context object. For example, you can use Handlebars templates to generate the HTML for your message like so:

const contextObject = {
  variable1: 'value1',
  variable2: 'value2'
};

nodemailerMailgun.sendMail({
  from: 'myemail@example.com',
  to: 'recipient@domain.com', // An array if you have multiple recipients.
  subject: 'Hey you, awesome!',
  template: {
    name: 'email.hbs',
    engine: 'handlebars',
    context: contextObject
  }
}, (err, info) => {
  if (err) {
    console.log(`Error: ${err}`);
  }
  else {
    console.log(`Response: ${info}`);
  }
});

Readme

Keywords

none

Package Sidebar

Install

npm i @b-stable/nodemailer-mailgun-transport-ts

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

30.5 kB

Total Files

45

Last publish

Collaborators

  • kaptn
  • ivonin
  • b-stable-team