-
Notifications
You must be signed in to change notification settings - Fork 2
/
smtpSrv.js
122 lines (75 loc) · 2.13 KB
/
smtpSrv.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
"use strict";
import { SMTPServer as ssrv } from 'smtp-server'
import { simpleParser } from 'mailparser'
import fs from 'fs'
import path from 'path'
import h from './helper.js'
let mod = {
start: function(db , port){
let opt = {
async onData(stream, _session, callback) {
try {
const mail = await simpleParser(stream);
let sender = mail.from.value[0].address || mail.from.value[0].name;
let subject = mail.subject;
let content;
if(mail.html){
content = mail.html;
}else{
content = mail.textAsHtml;
}
try {
for (let recipient of mail.to.value){
var recipientName = recipient.address.substring(0, recipient.address.lastIndexOf("@"));
let res = db.prepare("SELECT COUNT(*) as count FROM address WHERE addr = ?").all(recipientName);
if (res[0].count > 0) {
let id = h.randomID();
db.prepare("INSERT INTO mail (id, recipient, sender, subject, content) VALUES (?, ?, ?, ?, ?)").run(id, recipientName, sender, subject, content);
break;
}
}
} catch (err) {
console.log("Inbound email error");
console.log(err);
}
} catch (err) {
console.log("Processing email error");
console.log(err);
}
callback(null);
},
authOptional: true,
onConnect(_session, callback) {
return callback();
},
}
try {
//automatically detect public / private key
const files = fs.readdirSync("./data");
for(let fileName of files){
let ext = path.extname(fileName);
if(ext != ".db" && ext != ".json"){
let content = fs.readFileSync("./data/" + fileName, 'utf8');
if(content.includes("PRIVATE KEY")){
opt.key = content;
}
if(content.includes("BEGIN CERTIFICATE")){
opt.cert = content;
}
}
}
} catch (err) {
console.log("read directory fail");
console.log(err);
}
const server = new ssrv(opt);
server.on('error', (err) => {
console.log("SMTP server error");
console.log(err);
});
server.listen(port, () => {
console.log('smtp server running at port: ' + port);
});
}
}
export default mod;