Two environment varialbes are required to create a Sendgrid
instance:
username
/THEHELP_SENDGRID_USERNAME
- this is your raw Sendgrid account usernamepassword
/THEHELP_SENDGRID_PASSWORD
- unfortunately, this is the same password as
your account. Not an account API access token. I heartily recommend that you create
additional sub-accounts to use for your programs:
https://sendgrid.com/docs/User_Guide/multiple_credentials.html.send
checks that all four required keys are included on the provided
options
object:
to
from
,text
or html
subject
More documentation on the endpoint we hit: https://sendgrid.com/docs/API_Reference/Web_API/mail.html. We pass your payload directly to it, after adding your sendgrid credentials.
_sendFinish
handles the payload returned to us from the call to Sendgrid.
To help you deal with requests coming from Sendgrid, described here: https://sendgrid.com/docs/API_Reference/Webhooks/parse.html
Note: Don't forget to disable CSRF checking for your incoming options endpoint!
It's kind of a pain to parse incoming Sendgrid optionss because their content type is
'multipart/form-data'. This middleware function pulls all non-file data out of the
Sendgrid request, and then makes that available at req.body
.
Use it like this:
app.post('/sendgrid/options', sendgrid.parse, function(req, res) {
console.log(req.body)
res.status(200);
res.end();
});
Note: You'll need to supply Busboy
to this class
on construction if you want to use this middleware method.Note
But we don't want to accept incoming emails from just anyone. We want to be sure we're really dealing with Sendgrid. On the inbound dashboard you can add a querystring to the URL Sendgrid will hit.
This method assumes that you've set up a verify
querystring and the correct value of
that querystring is either set in the SENDGRID_VERIFY
environment variable or
directly passed to this class on construction.
We just add one new middleware function:
app.post('/sendgrid/options', sendgrid.validate, sendgrid.parse, function(req, res) {
console.log(req.body)
res.status(200);
res.end();
})
Sendgrid
This class is a thin wrappper around the Sendgrid Send mail REST endpoint: https://sendgrid.com/docs/API_Reference/Web_API/mail.html.