Forráskód Böngészése

Send endpoint and email delivery

James Peret 2 éve
szülő
commit
58caf0886d
4 módosított fájl, 136 hozzáadás és 2 törlés
  1. 77 0
      index.js
  2. 43 0
      package-lock.json
  3. 3 1
      package.json
  4. 13 1
      readme.md

+ 77 - 0
index.js

@@ -0,0 +1,77 @@
+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.`);
+        }
+    }
+}

+ 43 - 0
package-lock.json

@@ -32,6 +32,14 @@
         "negotiator": "0.6.2"
       }
     },
+    "ansi-styles": {
+      "version": "4.3.0",
+      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
+      "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
+      "requires": {
+        "color-convert": "^2.0.1"
+      }
+    },
     "array-flatten": {
       "version": "1.1.1",
       "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
@@ -81,6 +89,28 @@
       "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
       "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
     },
+    "chalk": {
+      "version": "4.1.2",
+      "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
+      "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
+      "requires": {
+        "ansi-styles": "^4.1.0",
+        "supports-color": "^7.1.0"
+      }
+    },
+    "color-convert": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
+      "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
+      "requires": {
+        "color-name": "~1.1.4"
+      }
+    },
+    "color-name": {
+      "version": "1.1.4",
+      "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
+      "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
+    },
     "content-disposition": {
       "version": "0.5.3",
       "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz",
@@ -208,6 +238,11 @@
       "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
       "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
     },
+    "has-flag": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
+      "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
+    },
     "http-errors": {
       "version": "1.7.2",
       "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz",
@@ -519,6 +554,14 @@
       "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
       "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
     },
+    "supports-color": {
+      "version": "7.2.0",
+      "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
+      "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
+      "requires": {
+        "has-flag": "^4.0.0"
+      }
+    },
     "toidentifier": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",

+ 3 - 1
package.json

@@ -4,7 +4,8 @@
   "description": "API service for sending emails using the AWS Simple Email Service",
   "main": "index.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "server": "node index.js"
   },
   "repository": {
     "type": "git",
@@ -17,6 +18,7 @@
   "author": "James Peret <james.peret@gmail.com> (http://jamesperet.com)",
   "license": "ISC",
   "dependencies": {
+    "chalk": "^4.1.2",
     "express": "^4.17.1",
     "mongoose": "^6.0.8",
     "nodemailer": "^6.6.5"

+ 13 - 1
readme.md

@@ -1,3 +1,15 @@
 # Mail Delivery Service
 
-An API service for sending email using the Amazon Web Services Simple Email Service (AWS SES). It connects to a MongoDB database for storing data.
+An API service for sending email using the Amazon Web Services Simple Email Service (AWS SES). It connects to a MongoDB database for storing data.
+
+### Tasks
+
+- [X] Send mail endpoint
+- [X] Send email thru AWS SES
+- [ ] Save email data on DB
+- [ ] Append Pixel tracking
+- [ ] Change Links to add tracking
+
+
+
+- [ ] Mail queue using [bree](https://jobscheduler.net/#/)