-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (34 loc) · 1.08 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
/* global require process Buffer */
const SMTPServer = require('smtp-server').SMTPServer
const AWS = require('aws-sdk')
const port = process.env.PORT || 25
const host = process.env.HOST || '127.0.0.1'
const SES = new AWS.SES()
const server = new SMTPServer({
authOptional: true,
async onData(stream, session, callback) {
try {
var params = {
Destinations: session.envelope.rcptTo.map(x => x.address),
FromArn: process.env.FROM_ARN,
RawMessage: {
Data: await readStream(stream)
}
}
console.log({ message: 'Sending email', Destinations: params.Destinations })
await SES.sendRawEmail(params).promise()
callback(null, 'Message sent')
} catch (e) {
console.log({ message: 'Failed to send email', error: e })
callback(e)
}
}
})
server.listen(port, host, () => console.log({ message: 'Server listening', host, port }))
function readStream(stream) {
return new Promise(resolve => {
var bufs = [];
stream.on('data', buf => bufs.push(buf))
stream.on('end', () => resolve(Buffer.concat(bufs)))
})
}