Browse Source

Finished validating the captcha and sending the email with nodemailer.

pull/4/head
jrtechs 6 years ago
parent
commit
793bac2fef
7 changed files with 129 additions and 16 deletions
  1. +3
    -0
      README.md
  2. +1
    -1
      admin.js
  3. +1
    -1
      includes/contact.html
  4. +106
    -8
      includes/contact.js
  5. +4
    -0
      includes/invalidCaptcha.html
  6. +12
    -4
      package.json
  7. +2
    -2
      utils/utils.js

+ 3
- 0
README.md View File

@ -107,6 +107,9 @@ npm install markdown
npm install highlight.js npm install highlight.js
npm install compression npm install compression
npm install memory-cache --save npm install memory-cache --save
npm install request
npm install nodemailer
npm install nodemailer-smtp-transport
``` ```

+ 1
- 1
admin.js View File

@ -29,7 +29,7 @@ const utils = require('./utils/utils.js');
//cache -- only used for static contents //cache -- only used for static contents
const cache = require('memory-cache'); const cache = require('memory-cache');
/** Initializes sessions for login */
//Initializes sessions for login
app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
//port to listen for the admin server on //port to listen for the admin server on

+ 1
- 1
includes/contact.html View File

@ -35,7 +35,7 @@
<div class="input-group-prepend"> <div class="input-group-prepend">
<div class="input-group-text"><i class="fa fa-comment text-info"></i></div> <div class="input-group-text"><i class="fa fa-comment text-info"></i></div>
</div> </div>
<textarea class="form-control" placeholder="Message" required></textarea>
<textarea class="form-control" placeholder="Message" name="message" required></textarea>
</div> </div>
</div> </div>

+ 106
- 8
includes/contact.js View File

@ -13,6 +13,96 @@ const includes = require('../includes/includes');
//for parsing post data //for parsing post data
const qs = require('querystring'); const qs = require('querystring');
const sanitizer = require('sanitizer');
const Request = require('request');
const nodemailer = require("nodemailer");
const smtpTransport = require('nodemailer-smtp-transport');
const CAPTCHA_SECRET = utils.getFileLine("../captcha_secret");
const EMAIL_PASSWORD = utils.getFileLine("../email_password");
const verifyCapcha = function(data)
{
const recaptcha_url = "https://www.google.com/recaptcha/api/siteverify?" +
"secret=" + CAPTCHA_SECRET + "&" +
"response=" + data;
return sync = new Promise(function(resolve, reject)
{
Request(recaptcha_url,
function (error, response, body)
{
if (!error && response.statusCode == 200)
{
const googleAnswer = JSON.parse(body);
if(googleAnswer.success == true)
{
resolve(true);
}
else
{
resolve(false);
}
}
else
{
resolve(false);
}
}
);
});
};
const sendEmail = function(name, email, message)
{
const transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: 'jrtechswebsite@gmail.com',
pass: EMAIL_PASSWORD
}
}));
const mailOptions =
{
to: "jeffery@jrtechs.net", // list of receivers
subject: "Jrtechs.net form submission", // Subject line
text: message, // plaintext body
html: message
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, response)
{
if(error)
{
console.log(error);
}
else
{
console.log("Message sent: " + response);
}
// if you don't want to use this transport object anymore, uncomment following line
transporter.close(); // shut down the connection pool, no more messages
});
};
const processContactPage = function(request) const processContactPage = function(request)
{ {
@ -20,15 +110,24 @@ const processContactPage = function(request)
{ {
utils.getPostData(request).then(function(postData) utils.getPostData(request).then(function(postData)
{ {
const data = qs.parse(postData); const data = qs.parse(postData);
console.log(postData);
console.log(data);
if(data.name && data.email)
if(data.name && data.email && data["g-recaptcha-response"] && data.message)
{ {
//send email
resolve(utils.include("includes/messageSent.html"));
verifyCapcha(sanitizer.sanitize(data["g-recaptcha-response"])).then(function(valid)
{
if(valid)
{
resolve(utils.include("includes/messageSent.html"));
sendEmail(data.name, data.email, data.message);
}
else
{
resolve(utils.include("includes/invalidCaptcha.html"));
}
});
} }
else else
{ {
@ -40,10 +139,9 @@ const processContactPage = function(request)
reject(err); reject(err);
}) })
}); });
}; };
module.exports = module.exports =
{ {
main: function(request, result) main: function(request, result)

+ 4
- 0
includes/invalidCaptcha.html View File

@ -0,0 +1,4 @@
<div class='col-md-8'>
<h1>Invalid Captcha</h1>
<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>

+ 12
- 4
package.json View File

@ -4,15 +4,23 @@
"description": "Personal wordpress replacement", "description": "Personal wordpress replacement",
"main": "server.js", "main": "server.js",
"dependencies": { "dependencies": {
"compression": "^1.7.3",
"crypto": "^1.0.1",
"express": "^4.16.3",
"express-session": "^1.15.6",
"highlight": "^0.2.4", "highlight": "^0.2.4",
"highlight.js": "^9.12.0",
"markdown": "^0.5.0", "markdown": "^0.5.0",
"markdown-to-html": "^0.0.13", "markdown-to-html": "^0.0.13",
"memory-cache": "^0.2.0", "memory-cache": "^0.2.0",
"mysql": "^2.15.0",
"mysql": "^2.16.0",
"nodemailer": "^4.6.8",
"nodemailer-smtp-transport": "^2.7.4",
"promise": "^8.0.1", "promise": "^8.0.1",
"remarkable": "^1.7.1",
"request": "^2.88.0",
"sanitizer": "^0.1.3", "sanitizer": "^0.1.3",
"step": "^1.0.0",
"synchronize": "^2.0.0"
"sendmail": "^1.4.1"
}, },
"devDependencies": {}, "devDependencies": {},
"scripts": { "scripts": {
@ -31,5 +39,5 @@
"bugs": { "bugs": {
"url": "https://github.com/jrtechs/NodeJSBlog/issues" "url": "https://github.com/jrtechs/NodeJSBlog/issues"
}, },
"homepage": "https://github.com/jrtechs/NodeJSBlog#readme"
"homepage": "https://jrtechs.net"
} }

+ 2
- 2
utils/utils.js View File

@ -47,7 +47,7 @@ module.exports=
{ {
console.log("Could not find " + fileName); console.log("Could not find " + fileName);
} }
return 0;
return '';
}, },
@ -66,7 +66,7 @@ module.exports=
{ {
console.log("Could not find " + fileName); console.log("Could not find " + fileName);
} }
return 0;
return '';
}, },

Loading…
Cancel
Save