-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (75 loc) · 1.97 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
const SMTPServer = require('smtp-server').SMTPServer;
const simpleParser = require('mailparser').simpleParser;
const previewEmail = require('preview-email');
const pino = require('pino');
const yargs = require('yargs');
const pretty = pino.pretty();
pretty.pipe(process.stdout)
const logger = pino({
name: 'smtp-tester',
safe: true
}, pretty);
const server = new SMTPServer({
disabledCommands: [ 'AUTH', 'STARTTLS' ],
onAuth(auth, session, callback) {
callback(null);
},
onMailFrom(address, session, callback) {
callback(null);
},
onData(stream, session, callback) {
const chunks = [];
stream.on('data', buffer => {
chunks.push(buffer);
});
stream.on('error', err => {
logger.error('Error while recieve email', err);
})
stream.on('end', () => {
const data = Buffer.concat(chunks).toString().replace(/\r\n$/, '');
logger.info('Email recieved successfully');
simpleParser(data)
.then(mail => {
logger.info(`Email ${mail.subject} from ${mail.from.text} to ${ mail.to.text} parsed`);
return previewEmail({
from: mail.from.text,
to: mail.to.text,
subject: mail.subject,
html: mail.html,
text: mail.text,
})
})
.then(() => {
logger.info('Email recieve finished');
callback(null);
})
.catch(err => {
logger.error('Error while parsing email', err);
callback(err);
});
});
}
});
server.on('error', err => {
logger.error('SMTP server error', err);
});
let args = ['--port'];
if (process.env.npm_config_argv) {
args = args.concat(JSON.parse(process.env.npm_config_argv).remain);
}
const argv = yargs(args).argv;
let port;
if (Number.isInteger(argv.port)) {
port = argv.port;
} else if (process.env.npm_package_config_port) {
port = process.env.npm_package_config_port;
} else {
port = 32284
}
server.listen(port, err => {
if (err) {
logger.error('Error while starting SMTP server', err);
} else {
logger.info(`SMTP server started successfully on port ${port}`);
}
});