@hitchy/plugin-mailer
v0.1.0
Published
easily integrate Nodemailer with Hitchy
Downloads
2
Readme
@hitchy/plugin-mailer
integrate NodeMailer with Hitchy
License
Installation
Install the plugin in a Hitchy-based project:
npm i @hitchy/plugin-mailer
Set up configuration in a file config/mailer.js with a content similar to this:
exports.mailer = { transport: "smtps://loginUser:[email protected]", template: { from: "[email protected]", }, };
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(
"[email protected]",
"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()
.