forked from ReachFive/fake-smtp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·125 lines (101 loc) · 3.1 KB
/
index.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
#!/usr/bin/env node
const SMTPServer = require('smtp-server').SMTPServer;
const simpleParser = require('mailparser').simpleParser;
const express = require("express");
const basicAuth = require('express-basic-auth');
const path = require("path");
const _ = require("lodash");
const moment = require("moment");
const cli = require('cli').enable('catchall').enable('status');
const config = cli.parse({
'smtp-port': ['s', 'SMTP port to listen on', 'number', 1025],
'http-port': ['h', 'HTTP port to listen on', 'number', 1080],
whitelist: ['w', 'Only accept e-mails from these adresses. Accepts multiple e-mails comma-separated', 'string'],
max: ['m', 'Max number of e-mails to keep', 'number', 100],
auth: ['a', 'Enable Authentication', 'string']
});
const whitelist = config.whitelist ? config.whitelist.split(',') : [];
let users = null;
if (config.auth && !/.+:.+/.test(config.auth)) {
cli.error("Please provide authentication details in USERNAME:PASSWORD format");
console.log(process.exit(1))
}
if (config.auth) {
let authConfig = config.auth.split(":");
users = {};
users[authConfig[0]] = authConfig[1];
}
const mails = [];
const server = new SMTPServer({
authOptional: true,
onMailFrom(address, session, cb) {
if (whitelist.length == 0 || whitelist.indexOf(address.address) !== -1) {
cb();
} else {
cb(new Error('Invalid email from: ' + address.address));
}
},
onData(stream, session, callback) {
simpleParser(stream).then(
mail => {
cli.debug(JSON.stringify(mail, null, 2));
mails.unshift(mail);
//trim list of emails if necessary
while (mails.length > config.max) {
mails.pop();
}
callback();
},
callback
);
}
});
server.on('error', err => {
cli.error(err);
});
server.listen(config['smtp-port']);
const app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
if (users) {
app.use(basicAuth({
users: users,
challenge: true
}));
}
const buildDir = path.join(__dirname, 'build');
app.use(express.static(buildDir));
function emailFilter(filter) {
return email => {
if (filter.since || filter.until) {
const date = moment(email.date);
if (filter.since && date.isBefore(filter.since)) {
return false;
}
if (filter.until && date.isAfter(filter.until)) {
return false;
}
}
if (filter.to && _.every(email.to.value, to => to.address !== filter.to)) {
return false;
}
if (filter.from && _.every(email.from.value, from => from.address !== filter.from)) {
return false;
}
return true;
}
}
app.get('/api/emails', (req, res) => {
res.json(mails.filter(emailFilter(req.query)));
});
app.delete('/api/emails', (req, res) => {
mails.length = 0;
res.send();
});
app.listen(config['http-port'], () => {
cli.info("HTTP server listening on port " + config['http-port']);
});
cli.info("SMTP server listening on port " + config['smtp-port']);