Skip to content

Fludd/smtpapi-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

20 Commits

Repository files navigation

smtpapi-nodejs

This node module allows you to quickly and more easily generate SendGrid X-SMTPAPI headers.

BuildStatusNPM version

varsmtpapi=require('smtpapi');varheader=smtpapi.Header();header.addTo('[email protected]');header.setUniqueArgs({cow: 'chicken'});varsmtpapi_header_string=header.jsonString();

See this for more information on the available X-SMTPAPI custom handling instructions.

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":{..."smtpapi": "0.0.1" } }

Install smtpapi-nodejs and its dependencies:

npm install

Usage

Initializing

varsmtpapi=require('smtpapi');varheader=newsmtpapi.Header();

jsonString

This gives you back the stringified json formatted X-SMTPAPI header. Use this with your smtp client of choice.

varsmtpapi=require('smtpapi');varheader=newsmtpapi.Header();header.jsonString();

addTo

varheader=newsmtpapi.Header();header.addTo('[email protected]');header.addTo('[email protected]');

setTos

varheader=newsmtpapi.Header();header.setTos(['[email protected]','[email protected]');

addSubstitution

varheader=newsmtpapi.Header();header.addSubstitution('keep','secret');// sub ={keep: ['secret']}header.addSubstitution('other',['one','two']);// sub ={keep: ['secret'], other: ['one', 'two']}

setSubstitutions

varheader=newsmtpapi.Header();header.setSubstitution({'-charge-': 'This ship is useless.'});// section ={'-charge-': 'This ship is useless.'}

addUniqueArg

varheader=newsmtpapi.Header();header.addUniqueArg({cow: 'chicken'});// unique_args ={cow: 'chicken'}header.addUniqueArg({cat: 'dog'});// unique_args ={cow: 'chicken', cat: 'dog'}

setUniqueArgs

varheader=newsmtpapi.Header();header.setUniqueArgs({cow: 'chicken'});// unique_args ={cow: 'chicken'}header.setUniqueArgs({dad: 'proud'});// unique_args ={dad: 'proud'}

addCategory

varheader=newsmtpapi.Header();header.addCategory('tactics');// category = ['tactics']header.addCategory('advanced');// category = ['tactics', 'advanced']

setCategories

varheader=newsmtpapi.Header();header.setCategories(['snowball-fight','tactics']);// category = ['snowball-fight', 'tactics']

addSection

varheader=newsmtpapi.Header();header.addSection({'-charge-': 'This ship is useless.'});// section ={'-charge-': 'This ship is useless.'}

setSections

varheader=newsmtpapi.Header();header.setSections({'-charge-': 'This ship is useless.','-other': 'Another section here'});// section ={'-charge-': 'This ship is useless.', '-other': 'Another section here'}

addFilter

You can add filter settings one at a time.

varheader=newsmtpapi.Header();header.addFilter('footer','enable',1);header.addFilter('footer','text/html','<strong>boo</strong>');

setFilters

You can set a filter using an object literal.

varheader=newsmtpapi.Header();header.setFilters({'footer': {'setting': {'enable': 1,'text/plain': 'You can haz footers!'}}});

SendGrid SMTP Example

The following example builds the X-SMTPAPI headers and adds them to nodemailer. Nodemailer then sends the email through SendGrid. You can use this same code in your application or optionally you can use sendgrid-nodejs.

varnodemailer=require('nodemailer');varsmtpapi=require('smtpapi');// Build the smtpapi headervarheader=newsmtpapi.Header();header.addTo('[email protected]');header.setUniqueArgs({cow: 'chicken'});// Add the smtpapi header to the general headersvarheaders={'x-smtpapi': header.jsonString()};// Use nodemailer to send the emailvarsettings={host: "smtp.sendgrid.net",port: parseInt(587,10),requiresAuth: true,auth: {user: "sendgrid_username",pass: "sendgrid_password"}};varsmtpTransport=nodemailer.createTransport("SMTP",settings);varmailOptions={from: "Fred Foo <[email protected]>",to: "[email protected]",subject: "Hello",text: "Hello world",html: "<b>Hello world</b>",headers: headers}smtpTransport.sendMail(mailOptions,function(error,response){smtpTransport.close();if(error){console.log(error);}else{console.log("Message sent: "+response.message);}});

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Running Tests

npm test```

About

Build SendGrid X-SMTPAPI headers in nodejs.

Resources

Stars

Watchers

Forks

Packages

No packages published