Skip to content

Latest commit

 

History

History

moleculer-mail

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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: "[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"
    }
});

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