@jamton/nodemailer-mailgun-transport-ts
v1.0.0
Published
Mailgun transport for the nodemailer library
Downloads
74
Readme
Installation
To use nodemailer-mailgun-transport, all you need to do is install the
@jamton/nodemailer-mailgun-transport-ts
package
$ yarn add @jamton/nodemailer-mailgun-transport-ts
# or
$ npm i @jamton/nodemailer-mailgun-transport-ts
Usage
import nodemailer from 'nodemailer';
import { MailgunTransport } from '@jamton/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: '[email protected]',
to: '[email protected]', // An array if you have multiple recipients.
cc:'[email protected]',
bcc:'[email protected]',
subject: 'Hey you, awesome!',
'replyTo': '[email protected]',
//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: '[email protected]'},
to: [{name:'Mary', address:'[email protected]'}, {address:'[email protected]'}]
Is converted to:
from: 'Sales <[email protected]>',
to: 'Mary <[email protected]>,[email protected]'
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: '[email protected]',
to: '[email protected]', // 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}`);
}
});