Nemo's Parental Misguidance
    Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    moleculer-mailpublic

    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",
        locale: "de-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
    data Object Global data for templates

    Transport options

    Read more from transport options

    Localized templates

    The service support templates. It uses email-templates library. The templates is rendered by consolidate.js, so you can use many template engines.

    Read more about template files.

    Read more about localized templates or check the examples folder.

    Actions

    Name Params Result Description
    mail.send Any field from here Object Send an email.

    Test

    $ npm test
    

    In development with watching

    $ npm run ci
    

    License

    The project is available under the MIT license.

    Contact

    Copyright (c) 2016-2017 MoleculerJS

    @moleculerjs @MoleculerJS

    install

    npm i moleculer-mail

    Downloadsweekly downloads

    28

    version

    1.2.0

    license

    MIT

    repository

    github.com

    last publish

    collaborators

    • avatar