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 = require('sendgrid')(api_user, api_key);
sendgrid.send({
to: 'example@example.com',
from: 'other@example.com',
subject: 'Hello World',
text: 'My first email through SendGrid.'
}, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
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:
{
...
"dependencies": {
...
"sendgrid": "0.3.0-rc.1.5"
}
}
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 = require('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.send(payload, function(err, json) {
if (err) { console.error(err); }
console.log(json);
});
Alternatively you can send it explicitly via Web or SMTP.
sendgrid.web(payload, function(err, json) {
if (err) { console.error(err); }
console.log(json);
});
Or
sendgrid.smtp(payload, function(err, json) {
if (err) { console.error(err); }
console.log(json);
});
Power Usage
There are two additioanl objects built into this library that will help you use this library as a power user.
Email
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 = require('sendgrid')(api_user, api_key);
var Email = sendgrid.Email;
var email = new 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: new SmtpapiHeaders(),
subject: '',
text: '',
html: '',
bcc: [],
replyto: '',
date: new Date(),
files: [
{
filename: '',
contentType: '',
cid: '',
path: '',
url: '',
content: ('' | Buffer)
}
],
file_data: {},
headers: {}
};
Here is a sample for using it:
var email = new 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 = require('sendgrid')(api_user, api_key);
var Email = sendgrid.Email;
var email = new Email({
to: 'denim@sample.com',
from: 'roy@sample.com',
subject: 'Listen',
text: 'Have you tried turning it off and on again'
});
email.replyto = "noreply@sample.com";
email.subject = "This is a subject";
email.addTo('moo@cow.com');
email.addTo(['solid@snake.com',
'liquid@snake.com']);
email.setHeaders({full: 'hearts'});
email.addHeaders({spin: 'attack'});
email.setHeaders({mask: 'salesman'});
email.addSubVal('keep', 'secret');
email.addSubVal('keep', 'safe');
email.setSection({'-charge-': 'This ship is useless.'});
email.addSection({'-bomber-': 'Only for sad vikings.'});
email.setSection({'-beam-': 'The best is for first'});
email.setUniqueArgs({cow: 'chicken'});
email.addUniqueArgs({cat: 'dog'});
email.setUniqueArgs({dad: 'proud'});
email.setCategory('tactics');
email.addCategory('advanced');
email.setCategory('snowball-fight');
email.setFilterSetting({
'footer': {
'setting': {
'enable': 1,
'text/plain': 'You can haz footers!'
}
}
});
email.addFilterSetting('footer', 'enable', 1);
email.addFilterSetting('footer', 'text/html', '<strong>boo</strong>');
email.addFile({
filename: 'secret.txt',
content: new Buffer('You will never know....')
});
email.addFile({
filename: 'icon.jpg',
url: 'http://i.imgur.com/2fDh8.jpg'
});
email.addFile({
path: '../files/resume.txt'
});
email.addFile({
cid: 'the_logo',
path: '../files/logo.png'
});
email.addHtml('<div>Our logo:<img src="cid:the_logo"></div>');
SMTP options
You can change the port to 465 if you prefer. After initializing simply code sendgrid.port = 465
var sendgrid = require('sendgrid')('username', 'password');
sendgrid.port = 465;
var payload = {...};
sendgrid.smtp(payload, function(err, json) {
if (err) { console.error(err); }
console.log(json);
});
You can also pass some additional fields through the smtp to the underlying nodemailer. The list of these fields are here.
var sendgrid = require('sendgrid')('username', 'password');
var payload = {...};
var nodeMailerOptions = {
messageId: "some-message-id"
}
sendgrid.smtp(payload, nodeMailerOptions, function(err, json) {
if (err) { console.error(err); }
console.log(json);
}
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.