Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for development SMTP servers like MailHog #21

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/developmentMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var sendmail = require('../sendmail')({silent: true, devPort: 1025});

sendmail({
from: '[email protected]',
to: '[email protected]',
replyTo: '[email protected]',
subject: 'MailComposer sendmail',
html: 'Mail of test sendmail'
}, function(err, reply) {
console.log(err && err.stack);
console.dir(reply);
});

51 changes: 33 additions & 18 deletions sendmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down