Permalink
Browse files

Merge pull request #85 from sendgrid/pr/84

Pr/84
  • Loading branch information...
2 parents 59b1ba7 + 8e3f7a7 commit 862518a60b0e7125b31b68539f4a3e2a6c6b9ebe @motdotla motdotla committed Sep 3, 2013
Showing with 136 additions and 112 deletions.
  1. +9 −22 README.md
  2. +32 −22 lib/sendgrid.js
  3. +54 −35 npm-shrinkwrap.json
  4. +1 −1 package.json
  5. +28 −30 test/integration/sendgrid.test.js
  6. +12 −2 test/lib/sendgrid.test.js
View
@@ -31,7 +31,7 @@ Add the following to your `package.json` file:
...
"dependencies": {
...
- "sendgrid": "0.3.0-rc.1.6"
+ "sendgrid": "0.3.0-rc.1.7"
}
}
```
@@ -78,22 +78,10 @@ sendgrid.send(payload, function(err, json) {
});
```
-**Alternatively you can send it explicitly via Web or SMTP.**
+**Alternatively you can opt to send via SMTP rather than via the WEB API. Just initialize with the `api: 'smtp'` option.**
```javascript
-sendgrid.web(payload, function(err, json) {
- if (err) { console.error(err); }
- console.log(json);
-});
-```
-
-Or
-
-```javascript
-sendgrid.smtp(payload, function(err, json) {
- if (err) { console.error(err); }
- console.log(json);
-});
+var sendgrid = require('sendgrid')(api_user, api_key, {api: 'smtp'});
```
## Power Usage
@@ -107,7 +95,7 @@ There are two additioanl objects built into this library that will help you use
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.
+NOTE: anything that is available in the Email constructor is available for use in the `sendgrid.send` function.
To get started create an Email object:
@@ -271,22 +259,21 @@ 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`
+You can change the port to 465 if you prefer. When initializing with the smtp api, also initialize with the port.
```javascript
-var sendgrid = require('sendgrid')('username', 'password');
-sendgrid.port = 465;
+var sendgrid = require('sendgrid')('username', 'password', {api: 'smtp', port: 465});
var payload = {...};
-sendgrid.smtp(payload, function(err, json) {
+sendgrid.send(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](https://github.com/andris9/Nodemailer#e-mail-message-fields).
+You can also pass some additional fields through the smtp to the underlying nodemailer. The list of these fields are [here](https://github.com/andris9/Nodemailer#e-mail-message-fields). To do this, you have to use the underlying `.smtp` method. This is really for power users.
```javascript
-var sendgrid = require('sendgrid')('username', 'password');
+var sendgrid = require('sendgrid')('username', 'password', {api: 'smtp'});
var payload = {...};
var nodeMailerOptions = {
messageId: "some-message-id"
View
@@ -6,12 +6,23 @@ var _ = require('underscore');
var request = require('request');
var Email = require('./email');
var SmtpapiHeaders = require('./smtpapi_headers');
+var Sendgrid = function(api_user, api_key, options) {
-module.exports = function(api_user, api_key) {
- var self;
+ if( !(this instanceof Sendgrid) ) {
+ return new Sendgrid(api_user, api_key, options);
+ }
+
+ var _this = this;
+ this.options = options || {};
+ this.options.port = this.options.port || 587;
+
+ var send = function() {
+ if ( _this.options.api === 'smtp') {
+ smtp.apply(this, arguments);
+ return true;
+ }
- var send = function(email, callback) {
- web(email, callback);
+ web.apply( this, arguments );
}
/*
@@ -24,9 +35,7 @@ module.exports = function(api_user, api_key) {
* This parameter is optional.
*/
var web = function(email, callback) {
- self = this;
var callback = callback || function() { };
-
if (email.constructor !== Email) {
email = new Email(email);
}
@@ -46,7 +55,6 @@ module.exports = function(api_user, api_key) {
* This parameter is optional.
*/
var smtp = function(email, nodeMailerOptions, callback) {
- self = this;
// Support a callback without nodeMailerOptions
if (! callback && typeof nodeMailerOptions === "function") {
@@ -113,7 +121,7 @@ module.exports = function(api_user, api_key) {
// SMTP settings
var smtp_settings = {
host: "smtp.sendgrid.net",
- port: parseInt(self.port),
+ port: parseInt(_this.port),
requiresAuth: true,
auth: {
user: api_user,
@@ -124,7 +132,7 @@ module.exports = function(api_user, api_key) {
smtp_settings['secureConnection'] = true;
}
- var smtpTransport = nodemailer.createTransport(self.SMTP, smtp_settings);
+ var smtpTransport = nodemailer.createTransport(_this.SMTP, smtp_settings);
var smtpParams = email.toSmtpFormat();
@@ -144,16 +152,18 @@ module.exports = function(api_user, api_key) {
/*
* Expose public API calls
*/
- return {
- version : package_json.version,
- port : 587,
- SMTP : "SMTP",
- Email : Email,
- SmtpapiHeaders : SmtpapiHeaders,
- api_user : api_user,
- api_key : api_key,
- web : web,
- smtp : smtp,
- send : send
- };
-}
+ this.version = package_json.version;
+ this.SMTP = "SMTP";
+ this.Email = Email;
+ this.SmtpapiHeaders = SmtpapiHeaders;
+ this.api_user = api_user;
+ this.api_key = api_key;
+ this.web = web;
+ this.smtp = smtp;
+ this.send = send;
+ this.options = this.options;
+ this.port = this.options.port;
+ return this;
+};
+
+module.exports = Sendgrid;
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 862518a

Please sign in to comment.