-
Notifications
You must be signed in to change notification settings - Fork 96
/
mail.js
131 lines (118 loc) · 3.66 KB
/
mail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
var child_process = require('child_process');
var conf = require('./conf.js');
var DNS;
if (conf.smtpTransport === 'direct' && !(typeof window !== 'undefined' && window && window.cordova)) {
DNS = require('dns');
DNS.setServers(["8.8.8.8", "114.114.114.114"]); // google public DNS, China 114dns
}
if (conf.smtpTransport === 'relay' && !conf.smtpRelay)
throw Error("please set smtpRelay in conf");
function sendmail(params, cb){
if (!cb)
cb = function(){};
switch (conf.smtpTransport){
case 'relay':
return sendMailThroughRelay(params, cb);
case 'direct':
return sendMailDirectly(params, cb);
case 'local':
default:
sendMailThroughUnixSendmail(params, cb);
}
}
function sendMailThroughUnixSendmail(params, cb){
try {
var child = child_process.spawn('/usr/sbin/sendmail', ['-t', params.to]);
}
catch (e) {
console.error("failed to spawn /usr/sbin/sendmail while trying to send", params.subject, e);
throw e;
}
child.stdin.on('error', function(err){
console.log("Error when sending mail through Mail Transfer Agent: " + err);
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.stdin.write("Return-Path: <"+params.from+">\r\nTo: "+params.to+"\r\nFrom: "+params.from+"\r\nSubject: "+params.subject+"\r\n\r\n"+params.body);
child.stdin.end();
cb();
}
function sendMailDirectly(params, cb) {
var nodemailer = require('nodemailer');
var hostname = params.to.slice(params.to.indexOf("@")+1);
DNS.resolveMx(hostname, function(err, exchanges){
var exchange = hostname;
if (exchanges && exchanges.length)
exchange = exchanges[0].exchange;
var transporter = nodemailer.createTransport({
host: exchange,
port: conf.smtpPort || null, // custom port
secure: conf.smtpSsl || false, // secure=true is port 465
requireTLS: false,
tls: {
rejectUnauthorized: true
}
});
var mailOptions = {
from: params.from,
to: params.to,
subject: params.subject,
text: params.body, // plain text body
html: params.htmlBody // html body
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.error("failed to send mail to "+params.to+": "+error);
return cb(error);
}
console.log('Message sent: %s', info.messageId);
cb(null, info);
});
});
}
function sendMailThroughRelay(params, cb){
var nodemailer = require('nodemailer');
var transportOpts = {
host: conf.smtpRelay,
port: conf.smtpPort || null, // custom port
secure: conf.smtpSsl || false, // secure=true is port 465
requireTLS: false,
tls: {
rejectUnauthorized: true
}
};
if (conf.smtpUser && conf.smtpPassword)
transportOpts.auth = {
user: conf.smtpUser,
pass: conf.smtpPassword
}
var transporter = nodemailer.createTransport(transportOpts);
var mailOptions = {
from: params.from,
to: params.to,
subject: params.subject,
text: params.body, // plain text body
html: params.htmlBody // html body
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.error("failed to send mail to "+params.to+": "+error+"\n", error);
return cb(error);
}
console.log('Message sent: %s', info.messageId);
cb(null, info);
});
}
function sendBugEmail(error_message, exception){
if (!conf.bug_sink_email || !conf.bugs_from_email)
return console.log("not sending bug email " + error_message.substr(0, 50).replace(/\s/g, ' '));
sendmail({
to: conf.bug_sink_email,
from: conf.bugs_from_email,
subject: 'BUG '+error_message.substr(0, 200).replace(/\s/g, ' '),
body: error_message + "\n\n" + ((typeof exception === 'string') ? exception : JSON.stringify(exception, null, '\t'))
});
}
exports.sendmail = sendmail;
exports.sendBugEmail = sendBugEmail;