From b3fa2b95ca3f902fef436089384a52edb0aa0a4f Mon Sep 17 00:00:00 2001 From: Michael Rehnert Date: Sat, 6 Feb 2021 05:42:18 -0600 Subject: [PATCH] cleaned code minor bug fixes version release 1.1.0 --- .env.example | 11 +++---- README.md | 17 ++++++++--- SECURITY.md | 4 +-- package-lock.json | 2 +- package.json | 2 +- routes/routes.js | 9 +++--- server.js | 76 +++++++++++++++++++++++------------------------ 7 files changed, 63 insertions(+), 58 deletions(-) diff --git a/.env.example b/.env.example index 72dd2b0..b08779d 100644 --- a/.env.example +++ b/.env.example @@ -1,10 +1,7 @@ -SMTP_FROM_EMAIL="me.real@gmail.com" -SMTP_TO_EMAIL="you.real@gmail.com" -SMTP_TO_PASSWORD="***************" - +SMTP_DEV_FROM="me.real@gmail.com" +SMTP_DEV_EMAIL="you.real@gmail.com" +SMTP_TO_PASSWORD="****************" SMTP_DEV_FROM="me.dev@ethereal.email" SMTP_DEV_EMAIL="you.dev@ethereal.email" -SMTP_DEV_PASSWORD="***************" - -STARTTLS=true +SMTP_DEV_PASSWORD="****************" diff --git a/README.md b/README.md index c63ea4e..bb62d7c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ # express-smtp-mailer + [![Build Status](https://travis-ci.com/killshot13/express-smtp-mailer.svg?branch=main)](https://travis-ci.com/killshot13/express-smtp-mailer) [![Maintained](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/killshot13/express-smtp-mailer/graphs/traffic) [![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/) -
+ ## Efficient & Secure Node Server -- Built with Express, Nodemailer, and Gmail ### Multi-Process Design, HTML FileType Priority, Middleware AJAX Parsing + ### Integrated SMTP Mail Delivery For Contact Forms, Subscriptions, etc + [![Open Source? Yes!](https://badgen.net/badge/Open%20Source%20%3F/Yes%21/blue?icon=github)](https://github.com/killshot/13/express-smtp-mailer) ## [VIEW FULL TUTORIAL](https://daily.dev/posts/how-to-build-an-smtp-mail-server-with-express-node-and-gmail) @@ -23,13 +26,19 @@ create a new `.env` file in the root directory -use these `process.env` variables defined in the `routes.js` file to provide account authentication +use the `process.env` variables defined in the `.env.example` and `routes.js` files to setup authentication + +for production, use the credentials of the recipient account _`process.env.SMTP_TO_EMAIL=''`_ _`process.env.SMTP_TO_PASSWORD=''`_ -use the credentials of the recipient account +for development and testing, create an [Ethereal account](https://ethereal.email/create) and enter the credentials of the testing account (if desired) + +_`process.env.SMTP_DEV_EMAIL=''`_ + +_`process.env.SMTP_DEV_PASSWORD=''`_ ### Testing @@ -41,7 +50,7 @@ it should print out the following lines `Ready to send mail!` -verify the functionality by replacing the `'/example/frontend.js'` dirpath in `routes.js` and `server.js` with your frontend route +verify full functionality by replacing the `'/example/frontend.js'` dirpath in `routes.js` and `server.js` with your own API routes ### Success diff --git a/SECURITY.md b/SECURITY.md index 24e6e23..db1ddf2 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -4,8 +4,8 @@ | Version | Supported | | ------- | ------------------ | -| 1.0.0 | :white_check_mark: | -| 1.0.1 | :white_check_mark: | +| 1.0.2 | :white_check_mark: | +| 1.1.0 | :white_check_mark: | ## Reporting a Vulnerability diff --git a/package-lock.json b/package-lock.json index 164638b..023ef5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "express-smtp-mailer", - "version": "1.0.2", + "version": "1.1.0", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 1f7c902..66ff91c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-smtp-mailer", - "version": "1.0.2", + "version": "1.1.0", "scripts": { "test": "mocha ./test", "start": "node server" diff --git a/routes/routes.js b/routes/routes.js index f7ce127..bc6dedb 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -4,9 +4,11 @@ const router = require("express").Router(); const path = require("path"); const nodemailer = require("nodemailer"); +const isDev = process.env.NODE_ENV !== "production"; + //this is the authentication for sending email. -var transport; -if (process.env.NODE_ENV === "production") { +let transport; +if (!isDev) { // all emails are delivered to destination transport = { host: "smtp.gmail.com", @@ -23,7 +25,6 @@ if (process.env.NODE_ENV === "production") { transport = { host: "smtp.ethereal.email", port: 587, - security: process.env.STARTTLS, // start TLS security //create a Ethereal test account @https://ethereal.email/create auth: { user: process.env.SMTP_DEV_EMAIL, @@ -79,8 +80,6 @@ router.post("/", (req, res, next) => { }); }); -// API routes - // Answer API requests. router.use("/api", function (req, res) { res.set("Content-Type", "application/json"); diff --git a/server.js b/server.js index 8eafc67..7776e9e 100644 --- a/server.js +++ b/server.js @@ -1,51 +1,51 @@ -const express = require('express') -const cluster = require('cluster') -const numCPUs = require('os').cpus().length +const express = require("express"); +const cluster = require("cluster"); +const numCPUs = require("os").cpus().length; -const isDev = process.env.NODE_ENV !== 'production' +const isDev = process.env.NODE_ENV !== "production"; // Multi-process to utilize all CPU cores. if (!isDev && cluster.isMaster) { - console.error(`Node cluster master ${process.pid} is running`) - - // Fork workers. - for (let i = 0; i < numCPUs; i++) { - cluster.fork() - } - - cluster.on('exit', (worker, code, signal) => { - console.error( - `Node cluster worker ${worker.process.pid} exited: code ${code}, signal ${signal}` - ) - }) + console.error(`Node cluster master ${process.pid} is running`); + + // Fork workers. + for (let i = 0; i < numCPUs; i++) { + cluster.fork(); + } + + cluster.on("exit", (worker, code, signal) => { + console.error( + `Node cluster worker ${worker.process.pid} exited: code ${code}, signal ${signal}` + ); + }); } else { - const app = express() - const morgan = require('morgan') - const path = require('path') + const app = express(); + const morgan = require("morgan"); + const path = require("path"); - const PORT = process.env.PORT || 5000 + const PORT = process.env.PORT || 5000; - // Priority serve any static files. - // Replace the example to connect to your frontend. - app.use(express.static(path.join(__dirname, '/example/frontend.js'))) + // Priority serve any static files. + // Replace the example to connect to your frontend. + app.use(express.static(path.join(__dirname, "/example/frontend.js"))); - // dev middleware - app.use(morgan('dev')) + // dev middleware + app.use(morgan("dev")); - // configure body parser for AJAX requests - app.use(express.urlencoded({ extended: false })) - app.use(express.json()) + // configure body parser for AJAX requests + app.use(express.urlencoded({ extended: false })); + app.use(express.json()); - const routes = require('./routes/routes') + const routes = require("./routes/routes"); - // after all middleware functions - app.use('/', routes) + // after all middleware functions + app.use("/", routes); - app.listen(PORT, function () { - console.error( - `Node ${ - isDev ? 'dev server' : 'cluster worker ' + process.pid - }: listening on port ${PORT}` - ) - }) + app.listen(PORT, function () { + console.error( + `Node ${ + isDev ? "dev server" : "cluster worker " + process.pid + }: listening on port ${PORT}` + ); + }); }