From 5bc1fba9e0d76ec39415c5157b3c4cd85a9f1e88 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 29 Nov 2016 21:04:44 +0100 Subject: [PATCH] Added support for development SMTP servers like MailHog --- examples/developmentMode.js | 13 ++++++++++ sendmail.js | 51 ++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 examples/developmentMode.js diff --git a/examples/developmentMode.js b/examples/developmentMode.js new file mode 100644 index 0000000..de2eaa2 --- /dev/null +++ b/examples/developmentMode.js @@ -0,0 +1,13 @@ +var sendmail = require('../sendmail')({silent: true, devPort: 1025}); + +sendmail({ + from: 'test@yourdomain.com', + to: 'info@yourdomain.com', + replyTo: 'jason@yourdomain.com', + subject: 'MailComposer sendmail', + html: 'Mail of test sendmail' + }, function(err, reply) { + console.log(err && err.stack); + console.dir(reply); +}); + diff --git a/sendmail.js b/sendmail.js index 6731344..473bfe6 100644 --- a/sendmail.js +++ b/sendmail.js @@ -16,6 +16,7 @@ var exports = module.exports = function(options) { warn: console.warn, error: console.error }) + var devPort = options.devPort || -1; /* * 邮件服务返回代码含义 Mail service return code Meaning @@ -63,37 +64,51 @@ var exports = module.exports = function(options) { * connect to domain by Mx record */ function connectMx(domain, callback) { - dns.resolveMx(domain, function(err, data) { - if (err) - return callback(err); + if (devPort === -1) { // not in development mode -> search the MX + dns.resolveMx(domain, function(err, data) { + if (err) + return callback(err); + + data.sort(function(a, b) {return a.priority < b. priority}); + logger.debug('mx resolved: ', data); + + if (!data || data.length == 0) + return callback(new Error('can not resolve Mx of <' + domain + '>')); + + function tryConnect(i) { + + if (i >= data.length) return callback(new Error('can not connect to any SMTP server')); - data.sort(function(a, b) {return a.priority < b. priority}); - logger.debug('mx resolved: ', data); + var sock = tcp.createConnection(25, data[i].exchange); - if (!data || data.length == 0) - return callback(new Error('can not resolve Mx of <' + domain + '>')); + sock.on('error', function(err) { + logger.error('Error on connectMx for: ', data[i], err); + tryConnect(++i); + }); - function tryConnect(i) { + sock.on('connect', function() { + logger.debug("MX connection created: ", data[i].exchange); + sock.removeAllListeners('error'); + callback(null, sock); + }); - if (i >= data.length) return callback(new Error('can not connect to any SMTP server')); + }; - var sock = tcp.createConnection(25, data[i].exchange); + tryConnect(0); + }); + } else { // development mode -> connect to the specified devPort on localhost + var sock = tcp.createConnection(devPort); sock.on('error', function(err) { - logger.error('Error on connectMx for: ', data[i], err); - tryConnect(++i); + callback(new Error('Error on connectMx (development) for \"localhost:' + devPort + '\": '+ err)); }); sock.on('connect', function() { - logger.debug("MX connection created: ", data[i].exchange); + logger.debug("MX (development) connection created: localhost:"+devPort); sock.removeAllListeners('error'); callback(null, sock); }); - - }; - - tryConnect(0); - }); + } } function sendToSMTP(domain, srcHost, from, recipients, body, cb) {