Skip to content

Commit

Permalink
cleaned code minor bug fixes version release 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
killshot13 committed Feb 6, 2021
1 parent c3d03e0 commit b3fa2b9
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 58 deletions.
11 changes: 4 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
SMTP_FROM_EMAIL="[email protected]"
SMTP_TO_EMAIL="[email protected]"
SMTP_TO_PASSWORD="***************"

SMTP_DEV_FROM="[email protected]"
SMTP_DEV_EMAIL="[email protected]"
SMTP_TO_PASSWORD="****************"

SMTP_DEV_FROM="[email protected]"
SMTP_DEV_EMAIL="[email protected]"
SMTP_DEV_PASSWORD="***************"

STARTTLS=true
SMTP_DEV_PASSWORD="****************"
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)
<br>

## 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)
Expand All @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-smtp-mailer",
"version": "1.0.2",
"version": "1.1.0",
"scripts": {
"test": "mocha ./test",
"start": "node server"
Expand Down
9 changes: 4 additions & 5 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
76 changes: 38 additions & 38 deletions server.js
Original file line number Diff line number Diff line change
@@ -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}`
);
});
}

0 comments on commit b3fa2b9

Please sign in to comment.