1.0.0 • Published 8 months ago

@wad-labs/nodemailer-mailgun-transport-ts v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

Installation

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

$ yarn add @wad-labs/nodemailer-mailgun-transport-ts

# or

$ npm i @wad-labs/nodemailer-mailgun-transport-ts

Usage

import nodemailer from 'nodemailer';
import { MailgunTransport } from '@wad-labs/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}`);
  }
});