|
@@ -3,75 +3,52 @@ const app = express()
|
|
app.use(express.json())
|
|
app.use(express.json())
|
|
const port = 3103
|
|
const port = 3103
|
|
|
|
|
|
-var nodemailer = require("nodemailer");
|
|
|
|
var chalk = require("chalk");
|
|
var chalk = require("chalk");
|
|
|
|
|
|
-check_env_variables();
|
|
|
|
|
|
+var helpers = require('./helpers');
|
|
|
|
+
|
|
|
|
+helpers.check_env_variables();
|
|
|
|
+helpers.start_database();
|
|
|
|
+
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
app.get('/', (req, res) => {
|
|
res.send("Mail Delivery Service");
|
|
res.send("Mail Delivery Service");
|
|
})
|
|
})
|
|
|
|
|
|
app.post('/send', (req, res) => {
|
|
app.post('/send', (req, res) => {
|
|
- console.log(req.body);
|
|
|
|
|
|
+ if(req.body == undefined){
|
|
|
|
+ console.log(`Received delivery request for message with no data. Aborting`);
|
|
|
|
+ res.status(400).json({ error: 'No data'}).end();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(req.body.from == "" || req.body.from == undefined){
|
|
|
|
+ console.log(`Received delivery request for message with no from email. Aborting`);
|
|
|
|
+ res.status(400).json({ error: 'Missing \'from\' field'}).end();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(req.body.to == "" || req.body.to == undefined){
|
|
|
|
+ console.log(`Received delivery request for message with no to email. Aborting`);
|
|
|
|
+ res.status(400).json({ error: 'Missing \'to\' field'}).end();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(req.body.message == "" || req.body.message == undefined){
|
|
|
|
+ console.log(`Received delivery request for message with no to message. Aborting`);
|
|
|
|
+ res.status(400).json({ error: 'Missing \'message\' field'}).end();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(req.body.subject == "" || req.body.subject == undefined){
|
|
|
|
+ console.log(`Received delivery request for message with empty subject. Aborting`);
|
|
|
|
+ res.status(400).json({ error: 'Missing \'subject\' field'}).end();
|
|
|
|
+ return;
|
|
|
|
+ } else {
|
|
|
|
+ var subject = chalk.italic(`\'${req.body.subject}\'`);
|
|
|
|
+ console.log(`Received delivery request for message ${subject}`);
|
|
|
|
+ }
|
|
|
|
+ //console.log(req.body);
|
|
res.status(200).end();
|
|
res.status(200).end();
|
|
- send_mail(req.body);
|
|
|
|
|
|
+ helpers.send_mail(req.body);
|
|
})
|
|
})
|
|
|
|
|
|
app.listen(port, () => {
|
|
app.listen(port, () => {
|
|
console.log(`Mail Delivery Service listening at ${chalk.cyan(`http://localhost:${port}`)}`);
|
|
console.log(`Mail Delivery Service listening at ${chalk.cyan(`http://localhost:${port}`)}`);
|
|
-})
|
|
|
|
-
|
|
|
|
-var send_mail = function(msg_data) {
|
|
|
|
- var mail_msg = {
|
|
|
|
- to: msg_data.to,
|
|
|
|
- from: msg_data.from,
|
|
|
|
- subject: msg_data.subject,
|
|
|
|
- html: msg_data.message,
|
|
|
|
- }
|
|
|
|
- // create Nodemailer SES transporter
|
|
|
|
- var transporter = nodemailer.createTransport({
|
|
|
|
- host: process.env.AWS_SES_URL,
|
|
|
|
- port: 587,
|
|
|
|
- secure: false, // upgrade later with STARTTLS
|
|
|
|
- auth: {
|
|
|
|
- user: process.env.AWS_SES_SMTP_USERNAME,
|
|
|
|
- pass: process.env.AWS_SES_SMTP_PASSWORD
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- transporter.sendMail(mail_msg, function (err, info) {
|
|
|
|
- if (err) {
|
|
|
|
- console.log("Error sending email");
|
|
|
|
- console.log(err);
|
|
|
|
- } else {
|
|
|
|
- console.log("Email sent successfully");
|
|
|
|
- console.log(info);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// Some checking to see if environment variables are set on statup and show errors if not
|
|
|
|
-var check_env_variables = function() {
|
|
|
|
- var env = process.env;
|
|
|
|
- var e = `${chalk.red(`ERROR:`)}`;
|
|
|
|
- if(env.AWS_SES_URL != "" && env.AWS_SES_URL != undefined){
|
|
|
|
- console.log(`Delivery server URL: ${chalk.green(`\'${env.AWS_SES_URL}\'`)}`);
|
|
|
|
- } else {
|
|
|
|
- var v = `${chalk.yellow(`\'AWS_SES_URL\'`)}`;
|
|
|
|
- console.log(`${e} AWS SES URL is missing. To fix this, add ${v} to the environment variables.`);
|
|
|
|
- }
|
|
|
|
- var has_username = env.AWS_SES_SMTP_USERNAME != "" && env.AWS_SES_SMTP_USERNAME != undefined;
|
|
|
|
- var has_password = env.AWS_SES_SMTP_PASSWORD != "" && env.AWS_SES_SMTP_PASSWORD != undefined;
|
|
|
|
- if(has_username && has_password){
|
|
|
|
- console.log(`AWS SES authentication is set with username: ${chalk.italic(env.AWS_SES_SMTP_USERNAME)}`);
|
|
|
|
- } else {
|
|
|
|
- if(!has_username){
|
|
|
|
- var v = `${chalk.yellow(`\'AWS_SES_SMTP_USERNAME\'`)}`;
|
|
|
|
- console.log(`${e} AWS SES needs a username. Add ${v} to the environment variables.`);
|
|
|
|
- }
|
|
|
|
- if(!has_password){
|
|
|
|
- var v = `${chalk.yellow(`\'AWS_SES_SMTP_PASSWORD\'`)}`;
|
|
|
|
- console.log(`${e} AWS SES needs a password. Add ${v} to the environment variables.`);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+})
|