Skip to content

Commit

Permalink
Merge pull request #85 from sendgrid/pr/84
Browse files Browse the repository at this point in the history
Pr/84
  • Loading branch information
motdotla committed Sep 3, 2013
2 parents 59b1ba7 + 8e3f7a7 commit 862518a
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 112 deletions.
31 changes: 9 additions & 22 deletions README.md
Expand Up @@ -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"
}
}
```
Expand Down Expand Up @@ -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
Expand All @@ -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:

Expand Down Expand Up @@ -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"
Expand Down
54 changes: 32 additions & 22 deletions lib/sendgrid.js
Expand Up @@ -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 );
}

/*
Expand All @@ -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);
}
Expand All @@ -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") {
Expand Down Expand Up @@ -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,
Expand All @@ -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();

Expand All @@ -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;
89 changes: 54 additions & 35 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 862518a

Please sign in to comment.