0.1.0 • Published 11 months ago

@hitchy/plugin-mailer v0.1.0

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
11 months ago

@hitchy/plugin-mailer

integrate NodeMailer with Hitchy

License

MIT

Installation

  1. Install the plugin in a Hitchy-based project:

    npm i @hitchy/plugin-mailer
  2. Set up configuration in a file config/mailer.js with a content similar to this:

    exports.mailer = {
        transport: "smtps://loginUser:loginPassword@smtp.example.com",
        template: {
    			   from: "noreply@my-company.com",
        },
    };

Usage

In your server-side code, sending a mail is as easy as this:

module.exports = function() {
    const api = this;

    return {
        async handleRequest( req, res ) {
            try {
                await api.service.Mail.sendTextMail(
                    "recipient@partner.com",
                    "Test mail subject",
                    "This is my test message."
                );
				
                res.json( { success: true } );
            } catch {
                res.status( 500 ).json( {
                    error: "sending mail failed"
                } );
            }
        }
    };
}

In this example, convenience helper api.service.Mail.sendTextMail() is invoked with a recipient, a subject and the message. It returns a promise to be resolved when sending mail has succeeded.

API

The service provides these methods:

sendTextMail( recipient, subject, message )

Sends a plain-text message with given content and subject to provided recipient.

sendMail( information )

Sends a message composed from provided information which is an object in compliance with Nodemailer's support for describing a mail to compose. The provided information is merged with configured template.

The method returns a promise resolved when sending mail has succeeded.

Configuration

The plugin's configuration is related to Nodemailer configuration:

  • transport is either a string describing an SMTP service to use or some object used to create the transport as supported by Nodemailer.

  • template is an object of properties any outgoing mail is composed with as its defaults. A sender's address must be given in property from at least.

    Including a subject, a message or a recipient might not work as expected there when using convenience helper methods such as Mail.sendTextMail().

0.1.0

11 months ago