Send emails with nodemailer. Support localized templates.
- 30+ pre-configures services (Gmail, Hotmail, Mailgun Sendgrid...etc)
- HTML and Text messages with attachments
- html-to-text conversion
- e-mail templates with localization
- [multiple HTML template engines: handlebars, pug, nunjuck
$ npm install moleculer-mail --save
or
$ yarn add moleculer-mail
Send an HTML e-mail with Gmail
"use strict";
const { ServiceBroker } = require("moleculer");
const broker = new ServiceBroker();
// Load service
broker.createService(require("moleculer-mail"), {
settings: {
from: "[email protected]",
transport: {
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourpass'
}
}
}
});
// Send an e-mail
broker.call("mail.send", {
to: "[email protected]",
subject: "Hello Friends!",
html: "This is the <b>content</b>!"
}).then(console.log);
Send an e-mail with mailgun with Cc & Bcc
// Load service
broker.createService(require("moleculer-mail"), {
settings: {
transport: {
service: "mailgun",
auth: {
api_key: 'api12345',
domain: 'domain.com'
}
}
}
});
// Send an e-mail to some people
broker.call("mail.send", {
to: "[email protected]",
cc: "[email protected]",
bcc: "[email protected]",
subject: "Hello Friends!",
text: "This is a text only message!"
}).then(console.log);
Send an e-mail from template
// Load service
broker.createService(require("moleculer-mail"), {
settings: {
transport: {
type: "sendmail"
},
templateFolder: "./email-templates",
// Global data for templates
data: {
siteName: "My app"
}
}
});
// Send a welcome template
broker.call("mail.send", {
to: "[email protected]",
template: "welcome",
language: "de",
data: {
name: "John Doe",
username: "john_doe",
verifyToken: "123456"
}
});
Property | Type | Description |
---|---|---|
from |
String |
Sender's default email address. Use it if missing from ctx.params |
transport |
Object |
Transport settings. Send to nodemailer.createTransporter |
htmlToText |
Boolean |
Enable html-to-text conversion |
templateFolder |
String |
Path to template folder |
fallbackLanguage |
String |
Fallback language folder in template |
data |
Object |
Global data for templates |
Read more from transport options
The service support templates. You should follow the given folder structure:
<templateFolder>
... en (language)
...... welcome (template name)
.........subject.hbs (subject template for "en" language)
.........html.pub (html template for "en" language)
... hu (language)
...... welcome (template name)
.........subject.hbs (subject template for "hu" language)
.........html.pub (html template for "hu" language)
Name | Params | Result | Description |
---|---|---|---|
mail.send |
Any field from here | Object |
Send an email. |
mail.render |
template ,language ,data |
Object |
Render a template. |
$ npm test
In development with watching
$ npm run ci
The project is available under the MIT license.
Copyright (c) 2016-2022 MoleculerJS