1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- const express = require('express')
- const app = express()
- app.use(express.json())
- const port = 3103
- var nodemailer = require("nodemailer");
- var chalk = require("chalk");
- check_env_variables();
- app.get('/', (req, res) => {
- res.send("Mail Delivery Service");
- })
- app.post('/send', (req, res) => {
- console.log(req.body);
- res.status(200).end();
- send_mail(req.body);
- })
- app.listen(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.`);
- }
- }
- }
|