Skip to content

Commit bf02882

Browse files
authored
Update sendmail.js
1 parent 51dada2 commit bf02882

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

sendmail.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function (options) {
2626
const smtpHost = options.smtpHost || -1;
2727
const rejectUnauthorized = options.rejectUnauthorized;
2828
const autoEHLO = options.autoEHLO;
29-
29+
3030
/*
3131
* 邮件服务返回代码含义 Mail service return code Meaning
3232
* 500 格式错误,命令不可识别(此错误也包括命令行过长)format error, command unrecognized (This error also includes command line too long)
@@ -88,12 +88,12 @@ module.exports = function (options) {
8888
return callback(new Error('can not resolve Mx of <' + domain + '>'));
8989
}
9090
if(smtpHost !== -1) data.push({exchange:smtpHost});
91-
91+
9292
function tryConnect (i) {
9393
if (i >= data.length) return callback(new Error('can not connect to any SMTP server'));
9494

9595
const sock = createConnection(smtpPort, data[i].exchange);
96-
96+
9797
sock.on('error', function (err) {
9898
logger.error('Error on connectMx for: ', data[i], err);
9999
tryConnect(++i)
@@ -123,7 +123,7 @@ module.exports = function (options) {
123123
}
124124
}
125125

126-
function sendToSMTP (domain, srcHost, from, recipients, body, sender, cb) {
126+
function sendToSMTP (domain, srcHost, from, recipients, body, cb) {
127127
const callback = (typeof cb === 'function') ? cb : function () {};
128128
connectMx(domain, function (err, sock) {
129129
if (err) {
@@ -170,11 +170,8 @@ module.exports = function (options) {
170170
}
171171
*/
172172

173-
if(sender){
174-
queue.push('MAIL FROM: ' + sender + ' <' + from + '>');
175-
} else {
176-
queue.push('MAIL FROM:<' + from + '>');
177-
}
173+
174+
queue.push('MAIL FROM:<' + from + '>');
178175
const recipients_length = recipients.length;
179176
for (let i = 0; i < recipients_length; i++) {
180177
queue.push('RCPT TO:<' + recipients[i] + '>');
@@ -212,7 +209,7 @@ module.exports = function (options) {
212209
}
213210
data = parts[parts.length - 1]
214211
});
215-
212+
216213
sock.removeAllListeners('close');
217214
sock.removeAllListeners('end');
218215

@@ -239,7 +236,7 @@ module.exports = function (options) {
239236
}
240237
w(cmd + ' ' + srcHost);
241238
break;
242-
}
239+
}
243240

244241
case 221: // bye
245242
sock.end();
@@ -254,7 +251,7 @@ module.exports = function (options) {
254251
} else {
255252
upgraded = true;
256253
}
257-
254+
258255
break;
259256
}
260257

@@ -352,11 +349,13 @@ module.exports = function (options) {
352349
*/
353350
function sendmail (mail, callback) {
354351
const mailcomposer = require('mailcomposer');
355-
const mailMe = mailcomposer(mail);
352+
const mailMe = mailcomposer({
353+
...mail,
354+
from: mail.sender? `${mail.sender} <${mail.from}>` : mail.from,
355+
});
356356
let recipients = [];
357357
let groups;
358358
let srcHost;
359-
let sender;
360359
if (mail.to) {
361360
recipients = recipients.concat(getAddresses(mail.to));
362361
}
@@ -368,10 +367,6 @@ module.exports = function (options) {
368367
if (mail.bcc) {
369368
recipients = recipients.concat(getAddresses(mail.bcc));
370369
}
371-
372-
if (mail.sender) {
373-
sender = mail.sender
374-
}
375370

376371
groups = groupRecipients(recipients);
377372

@@ -384,6 +379,7 @@ module.exports = function (options) {
384379
callback(err, null);
385380
return
386381
}
382+
387383
if (dkimPrivateKey) {
388384
const signature = DKIMSign(message, {
389385
privateKey: dkimPrivateKey,
@@ -393,7 +389,7 @@ module.exports = function (options) {
393389
message = signature + '\r\n' + message;
394390
}
395391
for (let domain in groups) {
396-
sendToSMTP(domain, srcHost, from, groups[domain], message, sender, callback);
392+
sendToSMTP(domain, srcHost, from, groups[domain], message, callback);
397393
}
398394
});
399395
}

0 commit comments

Comments
 (0)