summaryrefslogtreecommitdiffstats
path: root/lib/sendgrid.js
diff options
context:
space:
mode:
authorKyle Partridge <partkyle@gmail.com>2012-01-13 15:10:12 -0800
committerKyle Partridge <partkyle@gmail.com>2012-01-13 15:10:12 -0800
commit42dc25e7179559909f87721fbe418e51196d7020 (patch)
treee96964848468d29481e727724df51b025def4688 /lib/sendgrid.js
parent8799d80378c921f457449548e4d357a15ee23677 (diff)
downloadsendgrid-nodejs-42dc25e7179559909f87721fbe418e51196d7020.zip
sendgrid-nodejs-42dc25e7179559909f87721fbe418e51196d7020.tar.gz
sendgrid-nodejs-42dc25e7179559909f87721fbe418e51196d7020.tar.bz2
I gave that email a file attachment. Emails love file attachments
Diffstat (limited to 'lib/sendgrid.js')
-rw-r--r--lib/sendgrid.js132
1 files changed, 108 insertions, 24 deletions
diff --git a/lib/sendgrid.js b/lib/sendgrid.js
index 5f22e68..050ba70 100644
--- a/lib/sendgrid.js
+++ b/lib/sendgrid.js
@@ -4,6 +4,9 @@ var querystring = require('querystring');
var https = require('https');
var nodemailer = require('nodemailer');
var _ = require('underscore');
+var fs = require('fs');
+var path = require('path');
+var mime = require('mime');
var Email = require('./email');
@@ -12,6 +15,8 @@ function SendGrid(credentials) {
this.api_key = credentials.api_key;
}
+var boundary = Math.random();
+
/*
* Sends an email and returns true if the
* message was sent successfully.
@@ -19,35 +24,68 @@ function SendGrid(credentials) {
* @returns {Boolean}
*/
SendGrid.prototype.send = function(email, callback) {
+ var self = this;
+
if (email.constructor !== Email) {
email = new Email(email);
}
- var post_data = this.getPostData(email);
- var options = {
- host: 'sendgrid.com',
- path: '/api/mail.send.json',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Content-Length': post_data.length
+
+ function send_rest() {
+ var post_data;
+ var options = {
+ host: 'sendgrid.com',
+ path: '/api/mail.send.json',
+ method: 'POST',
+ };
+ if (email.hasFiles()) {
+ post_data = self.getMultipartData(email);
+ var length = 0;
+ for (var buf in post_data) {
+ length += post_data[buf].length;
+ }
+ options.headers = {
+ 'Content-Type': 'multipart/form-data; boundary=' + boundary,
+ 'Content-Length': length
+ };
+ } else {
+ post_data = self.getPostData(email);
+ options.headers = {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Content-Length': post_data.length
+ };
}
- };
- var request = https.request(options, function(res) {
- res.setEncoding('utf8');
- res.on('data', function(chunk) {
- var json = JSON.parse(chunk);
- callback.call(null, json.message == 'success', json.errors);
+ var request = https.request(options, function(res) {
+ res.setEncoding('utf8');
+ res.on('data', function(chunk) {
+ var json = JSON.parse(chunk);
+ callback.call(null, json.message == 'success', json.errors);
+ });
});
- });
- request.write(post_data);
- request.end();
+ for (var key in post_data) {
+ request.write(post_data[key]);
+ }
+ request.end();
+ }
+
+ if (email.hasFiles()) {
+ email.processFiles(function(success, message) {
+ if (success) {
+ send_rest();
+ } else {
+ callback(false, message);
+ }
+ });
+ } else {
+ send_rest();
+ }
};
SendGrid.prototype.smtp = function(email, callback) {
var self = this;
+ // SMTP settings
nodemailer.SMTP = {
host: 'smtp.sendgrid.net',
use_authentication: true,
@@ -56,24 +94,70 @@ SendGrid.prototype.smtp = function(email, callback) {
pass: this.api_key
};
+ function send_smtp() {
+ nodemailer.send_mail(email.toSmtpFormat(), function(error, success) {
+ callback.call(self, success, error);
+ });
+ }
+
if (email.constructor !== Email) {
email = new Email(email);
}
- nodemailer.send_mail(email.toSmtpFormat(), function(error, success) {
- callback.call(self, success, error);
- });
-}
+ if (_.size(email.files) > 0) {
+ email.processFiles(function(success, message) {
+ if (success) {
+ send_smtp();
+ } else {
+ callback(false, message);
+ }
+ });
+ } else {
+ send_smtp();
+ }
+};
-SendGrid.prototype.getPostData = function(params) {
+SendGrid.prototype.getPostData = function(email) {
var data = {
api_user: this.api_user,
api_key: this.api_key
}
- _.extend(data, params.toWebFormat());
+
+ _.extend(data, email.toWebFormat());
return querystring.stringify(data);
};
-module.exports = SendGrid;
+SendGrid.prototype.getMultipartData = function(email) {
+ var data = [];
+ data.push(new Buffer(encodeField(boundary, 'api_user', this.api_user)));
+ data.push(new Buffer(encodeField(boundary, 'api_key', this.api_key)));
+
+ _(email.toWebFormat()).each(function(v, k) {
+ data.push(new Buffer(encodeField(boundary, k, v)));
+ });
+
+ _(email.files).each(function(filepath, filename) {
+ data.push(encodeFile(boundary, mime.lookup(filepath), 'files[' + filename + ']', path.basename(filepath)));
+ data.push(new Buffer(email.file_data[filename]));
+ data.push(new Buffer('\r\n'));
+ });
+
+ return data;
+};
+
+function encodeField(boundary,name,value) {
+ var return_part = "--" + boundary + "\r\n";
+ return_part += "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
+ return_part += value + "\r\n";
+ return return_part;
+}
+
+function encodeFile(boundary,type,name,filename) {
+ var return_part = "--" + boundary + "\r\n";
+ return_part += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n";
+ return_part += "Content-Type: " + type + "\r\n";
+ return return_part;
+}
+module.exports = SendGrid;