If you are using Node.js and would like to send email through SendGrid, use the SendGrid nodejs module.
Full documentation for this module can be found on github.
varSendGrid=require('sendgrid').SendGrid;varsendgrid=newSendGrid(user,key);sendgrid.send({to:'example@example.com',from:'other@example.com',subject:'Hello World',text:'My first email through SendGrid'},function(success,message){if(!success){console.log(message);}});
Digging In Deeper
Email is the object that will help you easily prepare your message to be sent.
Info
Anything that is available in the Email constructor is available for use in both the sendgrid.send and sendgrid.smtp functions.
varoptionalParams={to:[],from:'',smtpapi:newSmtpapiHeaders(),subject:'',text:'',html:'',bcc:[],replyto:'',date:newDate(),files:[{filename:'',// required only if file.content is used.contentType:'',// optionalpath:'',//url:'',// == One of these three options is requiredcontent:(''|Buffer)//}],file_data:{},headers:{}};
varemail=newEmail({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.
/* To Addresses */email.addTo('moo@cow.com');// to = ['moo@cow.com']email.addTo(['solid@snake.com','liquid@snake.com']);// to = ['moo@cow.com', 'solid@snake.com', 'liquid@snake.com']/* Custom Email Headers */email.setHeaders({full:'hearts'});// headers = {full: 'hearts'}email.addHeaders({spin:'attack'});// headers = {full: 'hearts', spin: 'attack'}email.setHeaders({mask:'salesman'});// headers = {mask: 'salesman'}/* Substitution */email.addSubVal('keep','secret');// sub = {keep: ['secret']}email.addSubVal('keep','safe');// sub = {keep: ['secret', 'safe']}/* Section */email.setSection({'-charge-':'This ship is useless.'});// section = {'-charge-': 'This ship is useless.'}email.addSection({'-bomber-':'Only for sad vikings.'});// section = {'-charge-': 'This ship is useless.','-bomber-': 'Only for sad vikings.'}email.setSection({'-beam-':'The best is for first'});// section = {'-beam-': 'The best is for first'}
Setting unique arguments allows you to get more mojo out of your statistics:
Tests located in the test/lib folder can be ran as-is and should all pass if your code is valid. Tests that are located in the test/intergration folder require valid SendGrid account credentials inserted into test/test.setup in order to run.
Info
Running tests requires a valid SendGrid account and will consume credits.
When you’re ready, you can run your tests with this command: