Sendgrid-nodejs
This nodejs module allows you to quickly and easily send emails through SendGrid using nodejs.
Note: This module was recently upgraded from 0.2.x to 0.3.x. There were API breaking changes. For documentation on 0.2.x, please go here.
var sendgrid = api_user api_key;sendgrid;
Installation
The following recommended installation requires npm. If you are unfamiliar with npm, see the npm docs. Npm comes installed with Node.js since node version 0.8.x therefore you likely already have it.
Add the following to your package.json
file:
Install sendgrid-nodejs and its dependencies:
npm install
Alternative Installation
You can also install sendgrid locally with the following command:
npm install sendgrid
Usage
To begin using this library, initialize the SendGrid object with your SendGrid credentials.
var sendgrid = api_user api_key;
Create a new JavaScript object with your message details.
var payload = to : 'to@example.com' from : 'from@other.com' subject : 'Saying Hi' text : 'This is my first email through SendGrid'
Send it.
sendgrid;
Alternatively you can send it explicitly via Web or SMTP.
sendgrid;
Or
sendgrid;
Power Usage
There are two additioanl objects built into this library that will help you use this library as a power user.
- SmtpapiHeaders
Email helps you more powerfully prepare your message to be sent.
NOTE: anything that is available in the Email constructor is available for use in the sendgrid.send
, sendgrid.web
, and sendgrid.smtp
functions.
To get started create an Email object:
var sendgrid = api_user api_key;var Email = sendgridEmail;var email = params;
You can pass in as much or as little to params
as you want, as
the email object has methods for manipulating all of the data.
params structure
var params = to: toname: from: '' fromname: '' smtpapi: subject: '' text: '' html: '' bcc: replyto: '' date: files: filename: '' // required only if file.content is used. contentType: '' // optional cid: '' // optional, used to specify cid for inline content path: '' // url: '' // == One of these three options is required content: '' | Buffer // file_data: {} headers: {};
Here is a sample for using it:
var email = to: 'walks.it.in@sample.com' from: 'arsenal@sample.com' subject: 'What was Wenger thinking sending Walcott on that early?' text: 'Did you see that ludicrous display last night?';
Setting data
Here is an example of all of the functions available on the email object. The comments to the right show the current state of the variables as the functions are called. If you have a specific question, see the SendGrid API Docs. Please open a GitHub issue if you find bugs or missing features.
var sendgrid = api_user api_key;var Email = sendgridEmail;var email = to: 'denim@sample.com' from: 'roy@sample.com' subject: 'Listen' text: 'Have you tried turning it off and on again'; /* Setting various params */emailreplyto = "noreply@sample.com";emailsubject = "This is a subject"; /** The following examples update the 'x-smtpapi' headers **/ /* To Addresses */email; // to = ['moo@cow.com']email; // to = ['moo@cow.com', 'solid@snake.com', 'liquid@snake.com'] /* Custom Email Headers */email; // headers = {full: 'hearts'}email; // headers = {full: 'hearts', spin: 'attack'}email; // headers = {mask: 'salesman'} /* Substitution */email; // sub = {keep: ['secret']}email; // sub = {keep: ['secret', 'safe']} /* Section */email; // section = {'-charge-': 'This ship is useless.'}email; // section = {'-charge-': 'This ship is useless.', // '-bomber-': 'Only for sad vikings.'}email; // section = {'-beam-': 'The best is for first'} /* Unique Args */email; // unique_args = {cow: 'chicken'}email; // unique_args = {cow: 'chicken', cat: 'dog'}email; // unique_args = {dad: 'proud'} /* Category */email; // category = ['tactics']email; // category = ['tactics', 'advanced']email; // category = ['snowball-fight'] /* Filters */// You can set a filter using an object literalemail; // Alternatively, you can add filter settings one at a time.email;email; /* Attachments */ /* * You can add files directly from content in memory. * * It will try to guess the contentType based on the filename. */email; /* * You can add files directly from a url. * * It will try to guess the contentType based on the filename. */email; /* * You can add files from a path on the filesystem. * * It will try to grap the filename and contentType from the path. */email; /* * You can tag files for use as inline HTML content. * * It will mark the file for inline disposition using the specified "cid". */email;email;
SMTP options
You can change the port to 465 if you prefer. After initializing simply code sendgrid.port = 465
var sendgrid = 'username' 'password';sendgridport = 465;var payload = ...;sendgrid;
You can also pass some additional fields through the smtp to the underlying nodemailer. The list of these fields are here.
var sendgrid = 'username' 'password';var payload = ...;var nodeMailerOptions = messageId: "some-message-id" sendgrid
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Running Tests
The existing tests can be run using Mocha with the following command:
npm test
You can run individual tests with the following command:
./node_modules/.bin/mocha [path to test].js
Integration Tests
In order to run the integration tests, you'll need to update the environment file with your valid SendGrid credentials. Start by making a live copy of the example:
cp .env.example .env.test
Next, open up .env.test
and fill it in. After you have updated the environment file with your credentials, you can run the suite using the following command:
npm test
License
Licensed under the MIT License.