feathers-sendgrid
So far this only supports sending email. If you'd like additional functionality PRs are welcome! 😄
Installation
npm install feathers-sendgrid --save
Documentation
feathers-sendgrid
is used just like any other service. In order to send an email simply call create
with a payload that conforms to the Sendgrid V3 REST API. You can see an example payload here.
For usage with some of the bundled hooks see the example below.
Bundled Hooks
This module comes with a couple bundled hooks that make it a bit easier to send email. These are entirely optional.
renderTemplate
,normalizeEmail
,validateEmail
,sendEmail
Rendering Email Templates
This hook renders a specific email template
based on your express view engine
with your hook
object. It is meant to be used as a before
hook on the a create
method.
const Handlebars = ;const hooks = hooks; app; { return { return { hookshook }; };} app;
Options
engine
(required) - the view engine instance.template
(required) - the name of your template.path
[optional] - path to your email template directory. Defaults to your express view engine path + 'email' (ie. path/to/views/email/).
Validating Email Params
This hook validates that the following fields exist inside hook.data
:
from
to
orpersonalizations
subject
content
It is really loose validation since Sendgrid does it's own validation. This is more for ensuring that the absolute minimum fields are included in order to send an email.
const hooks = hooks; app;
Normalizing Email Params
This hook makes it a bit less tedious to send simple emails. It takes a simple flat format and turns it into the format that Sendgrid expects. It only effects the following fields in hook.data
:
from
to
subject
content
const hooks = hooks; app;
Complete Example
Here's an example of a Feathers server with a mailer
Sendgrid service.
;;;;; // Create a feathers instance.var app = // Enable REST services // Enable hooks // Turn on JSON parser for REST services // Turn on URL-encoded parser for REST services ; // Register the Sendgrid serviceapp; app; // Use the servicevar email = from: 'FROM_EMAIL' to: 'TO_EMAIL' subject: 'Sendgrid test' content: 'This is the email body'; app; // Start the server.var port = 3030;app;
You can run this example by using npm start
. Make sure you've added your Sendgrid API token.
License
Copyright (c) 2016
Licensed under the MIT license.