1
|
|
|
#!/usr/bin/env node |
2
|
|
|
|
3
|
|
|
// Import node libs |
4
|
|
|
import express from 'express' |
5
|
|
|
import bodyParser from 'body-parser' |
6
|
|
|
import path from 'path' |
7
|
|
|
import chalk from 'chalk' |
8
|
|
|
import fs from 'fs' |
9
|
|
|
// Import local libs |
10
|
|
|
import mkdir from './library/mkdir' |
11
|
|
|
import options from './library/options' |
12
|
|
|
import rmdir from './library/rmdir' |
13
|
|
|
|
14
|
|
|
const app = express() |
15
|
|
|
const log = console.log |
16
|
|
|
const successTemplate = fs.readFileSync(`${__dirname}/templates/success.xml`, { encoding: 'utf-8' }) |
17
|
|
|
const errorTemplate = fs.readFileSync(`${__dirname}/templates/error.xml`, { encoding: 'utf-8' }) |
18
|
|
|
|
19
|
|
|
app.use(bodyParser.json()) |
20
|
|
|
app.use(bodyParser.urlencoded({ extended: true })) |
21
|
|
|
|
22
|
|
|
log(` |
23
|
|
|
${chalk.inverse(' AWS Simple Email Service Local 📪 ')} |
24
|
|
|
${chalk.green('Listening on port:')} ${options.port}`) |
25
|
|
|
|
26
|
|
|
if (options.clean !== undefined) { |
27
|
|
|
log(` ${chalk.red('Cleaning directory:')} ${options.outputDir}`) |
28
|
|
|
rmdir(options.outputDir) |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
log(` ${chalk.green('Creating output directory:')} ${options.outputDir}`) |
32
|
|
|
mkdir(path.join(options.outputDir)) |
33
|
|
|
|
34
|
|
|
app.post('/', (req, res) => { |
35
|
|
|
const dateTime = new Date().toISOString() |
36
|
|
|
if (req.body.Action === 'SendEmail') { |
37
|
|
|
const dateDir = `${options.outputDir}/${dateTime.slice(0, 10)}` |
38
|
|
|
const fullDir = `${dateDir}/${dateTime.slice(11, 22).replace(/:\s*/g, '.')}` |
39
|
|
|
const headers = `Subject: ${req.body['Message.Subject.Data']}\nDestination: ${req.body['Destination.ToAddresses.member.1']}\nSource: ${req.body.Source}` |
40
|
|
|
try { |
41
|
|
|
if (req.body.Source && req.body['Message.Subject.Data'] && req.body['Message.Body.Html.Data'] && req.body['Message.Body.Text.Data'] && req.body['Destination.ToAddresses.member.1']) { |
42
|
|
|
mkdir(path.join(dateDir)) |
43
|
|
|
mkdir(path.join(fullDir)) |
44
|
|
|
log(` 📬 ${chalk.green('Email Recieved')} |
45
|
|
|
${chalk.blue('From:')} ${req.body.Source} |
46
|
|
|
${chalk.blue('To:')} ${req.body['Destination.ToAddresses.member.1']} |
47
|
|
|
${chalk.blue('Subject:')} ${req.body['Message.Subject.Data']} |
48
|
|
|
${chalk.blue('Html Email:')} ${process.cwd()}/${path.join(fullDir)}/body.html |
49
|
|
|
${chalk.blue('Text Email:')} ${process.cwd()}/${path.join(fullDir)}/body.txt |
50
|
|
|
`) |
51
|
|
|
fs.writeFileSync(`${fullDir}/body.html`, req.body['Message.Body.Html.Data']) |
52
|
|
|
fs.writeFileSync(`${fullDir}/body.txt`, req.body['Message.Body.Text.Data']) |
53
|
|
|
fs.writeFileSync(`${fullDir}/headers.txt`, headers) |
54
|
|
|
res.status(200).send( |
55
|
|
|
successTemplate.replace('{{message}}', `${process.cwd()}/${path.join(fullDir)}/body.html`) |
56
|
|
|
) |
57
|
|
|
} else { |
58
|
|
|
throw new Error('One or more required fields was not sent') |
59
|
|
|
} |
60
|
|
|
} catch (err) { |
61
|
|
|
log(` ${chalk.red('Error Occured:')} ${err}`) |
62
|
|
|
res.status(500).send( |
63
|
|
|
errorTemplate.replace('{{code}}', 'MessageRejected').replace('{{message}}', err.message) |
64
|
|
|
) |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
}) |
68
|
|
|
|
69
|
|
|
app.listen(options.port) |
70
|
|
|
|
71
|
|
|
export default app |