-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (54 loc) · 2 KB
/
app.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
const SMTPServer = require('smtp-server').SMTPServer;
const _ = require('lodash');
const sendmail = require('sendmail')();
const userDao = require('./userDao');
const server = new SMTPServer({
logger: true,
hideSTARTTLS: true,
onAuth(auth, session, callback) {
if (!userDao.findUser(auth)) {
return callback(new Error('Invalid username or password'));
}
callback(null, {
user: {
username: 'wangbowen'
}
});
},
onData(stream, session, callback) {
let str = '';
stream.on('data', buffer => {
str += buffer.toString('utf8');
});
stream.on('end', () => {
// 邮件头之间用换行符分隔,最后一个邮件头和邮件的
// 具体内容之间包含一个空行,所以用两个换行符分隔
// 邮件头和邮件内容(注意:邮件正文同样有可能包含\r\n\r\n)
let mail = str.split('\r\n\r\n');
let headers = _.first(mail)
.split('\r\n')
.reduce((prev, v) => {
prev[v.split(': ').shift()] = _.trim(v.split(': ').pop());
return prev;
}, {});
// 包含非ASCII码的正文会被base64编码,包含Content-Transfer-Encoding消息头
let content = '';
if (headers['Content-Transfer-Encoding'] && headers['Content-Transfer-Encoding'] == 'base64') {
content = new Buffer(_.tail(mail).join('\r\n\r\n'), 'base64').toString('utf8');
} else {
content = _.tail(mail).join('\r\n\r\n');
}
sendmail({
from: headers.From,
to: headers.To,
subject: headers.Subject,
html: content,
}, function (err, reply) {
console.log(err && err.stack);
console.dir(reply);
});
callback();
});
}
});
server.listen(25);