summaryrefslogtreecommitdiffstats
path: root/lib/sendgrid.js
diff options
context:
space:
mode:
authorTim Haines <tmhaines@gmail.com>2013-08-15 20:41:29 -0700
committerTim Haines <tmhaines@gmail.com>2013-08-15 20:51:54 -0700
commitd003330b6621801d18d698f0df27150e0ccf12b2 (patch)
tree318d4add47461003370484e9b2607fc1c69af6fa /lib/sendgrid.js
parentb14506188de4b807ce784de734831aa4ac9a39b1 (diff)
downloadsendgrid-nodejs-d003330b6621801d18d698f0df27150e0ccf12b2.zip
sendgrid-nodejs-d003330b6621801d18d698f0df27150e0ccf12b2.tar.gz
sendgrid-nodejs-d003330b6621801d18d698f0df27150e0ccf12b2.tar.bz2
Add optional nodeMailerOptions argument to sendgrid.smtp
Diffstat (limited to 'lib/sendgrid.js')
-rw-r--r--lib/sendgrid.js36
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,