moleculer-mail

2.0.0 • Public • Published

Moleculer logo

moleculer-mail NPM version

Send emails with nodemailer. Support localized templates.

Features

Install

$ npm install moleculer-mail --save

or

$ yarn add moleculer-mail

Usage

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: "sender@moleculer.services",
        transport: {
            service: 'gmail',
            auth: {
                user: 'gmail.user@gmail.com',
                pass: 'yourpass'
            }
        }
    }
});

// Send an e-mail
broker.call("mail.send", { 
    to: "john.doe@example.org", 
    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: "john.doe@example.org", 
    cc: "jane.doe@example.org",
    bcc: "boss@example.org",
    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: "john.doe@example.org", 
    template: "welcome",
    language: "de",
    data: {
        name: "John Doe",
        username: "john_doe",
        verifyToken: "123456"
    }
});

Settings

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

Transport options

Read more from transport options

Localized templates

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)

Actions

Name Params Result Description
mail.send Any field from here Object Send an email.
mail.render template,language,data Object Render a template.

Test

$ npm test

In development with watching

$ npm run ci

License

The project is available under the MIT license.

Contact

Copyright (c) 2016-2022 MoleculerJS

@moleculerjs @MoleculerJS

Readme

Keywords

Package Sidebar

Install

npm i moleculer-mail

Weekly Downloads

328

Version

2.0.0

License

MIT

Unpacked Size

25.2 kB

Total Files

21

Last publish

Collaborators

  • icebob