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

Connection close and relay #87

Merged
merged 6 commits into from
Nov 7, 2015
Merged
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
52 changes: 46 additions & 6 deletions lib/mailserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,16 @@ function relayMail(idOrMailObject, autoRelay, done) {

var mailSendCallback = function(err, info) {

var recipients = mail.envelope.to.map(function(toItem) { return toItem.address; });
var get_address = function(address) {
email = address.address;
if (email === undefined) {
email = address;
}
return email;
}

var recipients = mail.envelope.to.map(get_address);
var sender = get_address(mail.envelope.from);

if (autoRelay && mailServer.autoRelayRules) {
recipients = getAutoRelayableRecipients(recipients);
Expand All @@ -267,18 +276,19 @@ function relayMail(idOrMailObject, autoRelay, done) {
}

mailServer.client.send({
from: mail.envelope.from.address,
from: sender,
to: recipients
}, rawEmailStream, function (err, info) {
mailServer.client.quit();
resetRelayConfiguration();
if (err) {
console.error('Mail Delivery Error: ', err);
return done(err);
}

logger.log('Mail Delivered: ', mail.subject);
return done();
});
};
});
};

if (mailServer.client.options.auth) {
mailServer.client.login(mailServer.client.options.auth, mailSendCallback);
Expand All @@ -291,6 +301,30 @@ function relayMail(idOrMailObject, autoRelay, done) {
});
}

function resetRelayConfiguration(){
try {
mailServer.client = new SMTPConnection({
port: config.port,
host: config.host,
secure: config.secure,
auth: (config.pass & config.user) ? {user: config.user, pass: config.pass} : false,
tls: {rejectUnauthorized: false},
debug: true
});

logger.info(
'MailDev outgoing SMTP Server %s:%d (user:%s, pass:%s, secure:%s)',
config.host,
config.port,
config.user,
config.pass ? '####' : config.pass,
config.secure ? 'yes' : 'no'
);
} catch (err){
console.error('Error during reconfiguration of SMTP Server for outgoing email', err);
}
}

function getAutoRelayableRecipients(recipients) {
return recipients.filter(function(email) {
return validateAutoRelayRules(email);
Expand Down Expand Up @@ -530,14 +564,20 @@ mailServer.getEmailAttachment = function(id, filename, done){
/**
* Setup outgoing
*/

var config = {};
mailServer.setupOutgoing = function(host, port, user, pass, secure){

//defaults
port = port || (secure ? 465 : 25);
mailServer.outgoingHost = host = host || 'localhost';
secure = secure || false;

config.host = host;
config.port = port;
config.user = user;
config.pass = pass;
config.secure = secure;

try {
mailServer.client = new SMTPConnection({
port: port,
Expand Down