-
Notifications
You must be signed in to change notification settings - Fork 1
/
smtp-dump.js
49 lines (43 loc) · 1.36 KB
/
smtp-dump.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
// imports
var argv = require('minimist')(process.argv.slice(2)),
SMTPServer = require('smtp-server').SMTPServer,
fs = require('fs'),
dateFormat = require('dateformat');
// default values
var config = {
output: 'output',
port: 25,
logger: false
};
// overwrite default config values with arguments
Object.assign(config, argv);
// create new STMP Server instance with given configuration
var server = new SMTPServer({
secure: false,
name: 'localhost',
authOptional: true,
logger: config.logger,
onData: function(stream, session, callback) {
// create output directory if it does not exist yet
try {
fs.mkdirSync(config.output);
}
catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
}
// filename will consist of current DateTime ...
var filename = dateFormat(new Date(), 'yyyy-mm-dd HH-MM-ss');
if(session.envelope.rcptTo.length >= 1) {
// ... and first recipient E-Mail-Adress if existent
filename += ' ' + session.envelope.rcptTo[0].address;
}
// write mail to file
var dump = fs.createWriteStream(config.output + '/' + filename + '.txt');
stream.pipe(dump);
stream.on('end', callback);
}
});
// Server listens on localhost:25
server.listen(config.port);