diff options
author | Scott Motte <scott@scottmotte.com> | 2013-08-22 13:34:09 -0700 |
---|---|---|
committer | Scott Motte <scott@scottmotte.com> | 2013-08-22 13:34:09 -0700 |
commit | 0e52ccbc57826665bf240ec2f5935f7b3ca15c1d (patch) | |
tree | 71601acd3347b0bd1f64f7a69955dffdfe983250 /lib | |
parent | 53dc5d36241892370a6b6dea341004c33bd5ac34 (diff) | |
parent | d003330b6621801d18d698f0df27150e0ccf12b2 (diff) | |
download | sendgrid-nodejs-0e52ccbc57826665bf240ec2f5935f7b3ca15c1d.zip sendgrid-nodejs-0e52ccbc57826665bf240ec2f5935f7b3ca15c1d.tar.gz sendgrid-nodejs-0e52ccbc57826665bf240ec2f5935f7b3ca15c1d.tar.bz2 |
Merge pull request #75 from timhaines/add-message-id
Add optional nodeMailerOptions argument to sendgrid.smtp
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sendgrid.js | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/lib/sendgrid.js b/lib/sendgrid.js index 6ae564b..bb3dc0e 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -2,6 +2,7 @@ var package_json = require('./../package.json'); var nodemailer = require('nodemailer'); +var _ = require('underscore'); var request = require('request'); var Email = require('./email'); var SmtpapiHeaders = require('./smtpapi_headers'); @@ -37,20 +38,29 @@ module.exports = function(api_user, api_key) { * Sends an email via SMTP and returns true if the * message was sent successfully. * - * @param {Email|Object} email An email object or a hash that has - * the values for the email to be sent. - * @param {Function} callback A function to call when the processing is done. - * This parameter is optional. + * @param {Email|Object} email An email object or a hash that has + * the values for the email to be sent. + * @param {Object} nodeMailerOptions Extra options for nodeMailer. i.e. Message-Id + * This parameter is optional. + * @param {Function} callback A function to call when the processing is done. + * This parameter is optional. */ - var smtp = function(email, callback) { + var smtp = function(email, nodeMailerOptions, callback) { self = this; - var callback = callback || function() { }; + + // Support a callback without nodeMailerOptions + if (! callback && typeof nodeMailerOptions === "function") { + callback = nodeMailerOptions; + nodeMailerOptions = null; + } + + var callback = callback || function() { }; if (email.constructor !== Email) { email = new Email(email); } - _sendSmtp(email, callback); + _sendSmtp(email, nodeMailerOptions, callback); }; /* @@ -93,7 +103,7 @@ module.exports = function(api_user, api_key) { } } - var _sendSmtp = function(email, callback) { + var _sendSmtp = function(email, nodeMailerOptions, callback) { // SMTP settings var smtp_settings = { host: "smtp.sendgrid.net", @@ -110,7 +120,13 @@ module.exports = function(api_user, api_key) { var smtpTransport = nodemailer.createTransport(self.SMTP, smtp_settings); - smtpTransport.sendMail(email.toSmtpFormat(), function(error, response) { + var smtpParams = email.toSmtpFormat(); + + if (_.isObject(nodeMailerOptions)) { + _.extend(smtpParams, nodeMailerOptions); + } + + smtpTransport.sendMail(smtpParams, function(error, response) { smtpTransport.close(); if(error) { return callback(new Error(error.data), null);} @@ -127,7 +143,7 @@ module.exports = function(api_user, api_key) { port : 587, SMTP : "SMTP", Email : Email, - SmtpapiHeaders : SmtpapiHeaders, + SmtpapiHeaders : SmtpapiHeaders, api_user : api_user, api_key : api_key, web : web, |