-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (164 loc) · 5.72 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const express = require('express');
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
const chalk = require('chalk');
const fs = require('fs');
const {
template, keys, set, compact, cond, isArray, mapValues, isEmpty,
isPlainObject, stubTrue, identity, join, flow, get, curry, escapeRegExp,
} = require('lodash/fp');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { log } = console;
const options = yargs(hideBin(process.argv))
.option('port', {
alias: 'p',
type: 'number',
description: 'endpoint port',
default: 9323,
})
.option('transport-config', {
alias: 'c',
type: 'string',
description: 'nodemailer configuration',
default: null,
})
.argv;
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const errorTemplate = template(fs.readFileSync(`${__dirname}/templates/error.xml`, { encoding: 'utf-8' }));
const successTemplate = template(fs.readFileSync(`${__dirname}/templates/success.xml`, { encoding: 'utf-8' }));
const transportConfigPath = options['transport-config'];
let transportConfig = null;
if (transportConfigPath) {
try {
transportConfig = JSON.parse(fs.readFileSync(transportConfigPath, { encoding: 'utf-8' }));
} catch (e) {
log('Can not read config file, skipping');
}
}
async function createEtherealTransport() {
const testAccount = await nodemailer.createTestAccount();
return nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false,
auth: {
user: testAccount.user,
pass: testAccount.pass,
},
});
}
function createTransport(config) {
return nodemailer.createTransport(config);
}
let transporter;
if (isEmpty(transportConfig)) {
log(`${chalk.green('Creating Ethereal transport')}`);
transporter = createEtherealTransport();
} else {
log(`${chalk.green('Creating transport from config:')}\n${JSON.stringify(transportConfig, null, 2)}`);
transporter = Promise.resolve(createTransport(transportConfig));
}
const compactObjectArrays = (object) => cond([
[isArray, flow(compact, join(', '))],
[isPlainObject, mapValues(compactObjectArrays)],
[stubTrue, identity],
])(object);
const parseSesArgs = (body) => compactObjectArrays(
keys(body)
.reduce((acc, key) => set(key, body[key])(acc), {}),
);
const getRawField = curry((field, rawString) => (
get(1, rawString.match(new RegExp(`(?:[\n\r]|^)${escapeRegExp(field)}: ([^\n\r]*)`)))
));
async function sendEmail(req, res) {
const body = parseSesArgs(req.body);
const mailConfig = {
from: req.body.Source,
to: get('Destination.ToAddresses.member', body),
cc: get('Destination.CcAddresses.member', body),
bcc: get('Destination.BccAddresses.member', body),
replyTo: get('ReplyToAddresses.member', body),
subject: get('Message.Subject.Data', body),
text: get('Message.Body.Text.Data', body),
html: get('Message.Body.Html.Data', body),
};
if (!(mailConfig.from && mailConfig.subject
&& (mailConfig.html || mailConfig.text) && mailConfig.to)) {
throw new Error('One or more required fields are missing');
}
log(` 📬 ${chalk.green('Email received')}
${chalk.blue('From:')} ${mailConfig.from}
${chalk.blue('To:')} ${mailConfig.to}
${chalk.blue('Subject:')} ${mailConfig.subject}
`);
const info = await (await transporter).sendMail(mailConfig);
log(` ${chalk.green(`Email sent: ${info.messageId}`)}`);
log(` ${chalk.green(`Preview URL: ${nodemailer.getTestMessageUrl(info)}`)}`);
res.status(200).send(successTemplate({ message: 'OK' }));
}
async function sendRawEmail(req, res) {
const body = parseSesArgs(req.body);
const decodedBody = Buffer.from(get('RawMessage.Data', body), 'base64').toString();
const mailConfig = {
from: getRawField('From', decodedBody),
to: getRawField('To', decodedBody),
subject: getRawField('Subject', decodedBody),
raw: decodedBody,
};
if (!mailConfig.raw) {
throw new Error('RawMessage.Data is required and was not sent');
}
log(` 🍣 ${chalk.green('Raw email received')}
${chalk.blue('From:')} ${mailConfig.from}
${chalk.blue('To:')} ${mailConfig.to}
${chalk.blue('Subject:')} ${mailConfig.subject}
`);
const info = await (await transporter).sendMail(mailConfig);
log(` ${chalk.green(`Email sent: ${info.messageId}`)}`);
log(` ${chalk.green(`Preview URL: ${nodemailer.getTestMessageUrl(info)}`)}`);
res.status(200).send(successTemplate({ message: 'OK' }));
}
log(`${chalk.inverse(' AWS SES simulator 📪 ')}\n${chalk.green('Listening on port:')} ${options.port}`);
app.post('/', async (req, res) => {
try {
switch (req.body.Action) {
case 'SendEmail':
await sendEmail(req, res, log);
break;
case 'SendRawEmail':
await sendRawEmail(req, res, log);
break;
default:
throw new Error(`Unsupported action ${req.body.Action}`);
}
} catch (err) {
log(` ${chalk.red('Error Occured:')} ${err}`);
res.status(500).send(
errorTemplate({
code: 'MessageRejected',
message: err.message,
}),
);
}
});
app.post('/transport-config', async (req, res) => {
try {
const config = req.body;
console.log(config);
if (isEmpty(config)) {
transporter = createEtherealTransport();
log(`${chalk.green('Updating transport config to Etheral')}`);
} else {
log(`${chalk.green('Updating transport config:')}\n${JSON.stringify(config, null, 2)}`);
transporter = Promise.resolve(createTransport(config));
}
res.status(200).json(config);
} catch (err) {
log(` ${chalk.red('Error Occured:')} ${err}`);
res.status(500).json({ error: err.message });
}
});
app.listen(options.port);