sendgrid-nodejs
This nodejs module allows you to quickly and easily send emails through SendGrid using nodejs.
License
Licensed under the MIT License.
Install
npm install sendgrid-nodejs
Usage
It can be this easy
var SendGrid = SendGrid;var sendgrid = user key;sendgrid;
And you're done!
Digging in
There are two objects that you really need to know to get started:
- SendGrid
Email is the object that will help you easily perpare your message to be sent. To get started create an Email object
var email = optionalParams;
You can pass in as much or as litle to optionalParams as you want, as the email object has methods for manipulating all of the data.
params structure
var default_mail_params = to: from: '' smtpapi: subject: '' text: '' html: '' bcc: replyto: '' date: files: {} file_data: {} headers: {};
Sample for using it:
var email = to: 'sample@sample.com' from: 'sample@sample.com' subject: 'Hey' text: 'Did you see that ludicrous display last night?';
Setting data
In general setting a value will override it while adding that value will add it to the existing values but will override existing keys with the new value.
var email = to: 'sample@sample.com' from: 'sample@sample.com' subject: 'Listen' text: 'Haved you tried turning it off and on again'; // Add other to addressesemail;// addTo also takes an arrayemail; // setHeaders will override any header values already setemail;// addHeaders will add to existing headers, overriding existing keysemail; // Adding substitution valuesemail; // Setting unique args will override any values already setemail;// Adding unique args will add to existing values, overriding existingkeysemail; // Setting/Adding a categoryemail;email; // Setting/Adding a Sectionemail;email; // Setting a Filter, takes an object literalemail;// Adding a filter via addFilterSettingemail;email; // Adding a fileemail;
More examples can be find in the test
Tests
If you're interested in seeing some sample code or just want to run the test then here's what you need to know. Test written in the test/lib folder can be ran as in and should all pass. Test written in test/intergration need the values in test/test.setup to be set in order to run and will require a SendGrid account, as these test will send actual emails.
Running
make test
will run all tests, otherwise you can run individual tests by running
mocha /path/to/test.test.js
For information on how to use Sendgrid see: SendGrid API Docs