Skip to content

Commit

Permalink
Merged in pr #21
Browse files Browse the repository at this point in the history
Merged in pr #21 added devmode
  • Loading branch information
GreenPioneer committed Dec 5, 2016
2 parents 8e4f235 + a45ce29 commit bad183c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
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: '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);
});

12 changes: 12 additions & 0 deletions examples/dkim-private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-----BEGIN RSA PRIVATE KEY-----
MIIBywIBAAJhANCx7ncKUfQ8wBUYmMqq6ky8rBB0NL8knBf3+uA7q/CSxpX6sQ8N
dFNtEeEd7gu7BWEM7+PkO1P0M78eZOvVmput8BP9R44ARpgHY4V0qSCdUt4rD32n
wfjlGbh8p5ua5wIDAQABAmAm+uUQpQPTu7kg95wqVqw2sxLsa9giT6M8MtxQH7Uo
1TF0eAO0TQ4KOxgY1S9OT5sGPVKnag258m3qX7o5imawcuyStb68DQgAUg6xv7Af
AqAEDfYN5HW6xK+X81jfOUECMQDr7XAS4PERATvgb1B3vRu5UEbuXcenHDYgdoyT
3qJFViTbep4qeaflF0uF9eFveMcCMQDic10rJ8fopGD7/a45O4VJb0+lRXVdqZxJ
QzAp+zVKWqDqPfX7L93SQLzOGhdd7OECMQDeQyD7WBkjSQNMy/GF7I1qxrscIxNN
VqGTcbu8Lti285Hjhx/sqhHHHGwU9vB7oM8CMQDKTS3Kw/s/xrot5O+kiZwFgr+w
cmDrj/7jJHb+ykFNb7GaEkiSYqzUjKkfpweBDYECMFJUyzuuFJAjq3BXmGJlyykQ
TweUw+zMVdSXjO+FCPcYNi6CP1t1KoESzGKBVoqA/g==
-----END RSA PRIVATE KEY-----
3 changes: 2 additions & 1 deletion examples/dkim.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var fs = require('fs');

var sendmail = require('../sendmail')({
silent:true,
dkim: {
privateKey: fs.readFileSync('dkim-private.pem', 'utf-8'),
privateKey: fs.readFileSync('./dkim-private.pem', 'utf8'),
keySelector: 'mydomainkey'
}
});
Expand Down
51 changes: 33 additions & 18 deletions sendmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var exports = module.exports = function(options) {
})
var dkimPrivateKey = (options.dkim || {}).privateKey
var dkimKeySelector = (options.dkim || {}).keySelector || 'dkim'
var devPort = options.devPort || -1;

/*
* 邮件服务返回代码含义 Mail service return code Meaning
Expand Down Expand Up @@ -66,37 +67,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

0 comments on commit bad183c

Please sign in to comment.