Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.0 KiB

  1. /**
  2. * @author Jeffery Russell 8-19-18
  3. */
  4. //used for file IO
  5. const utils = require('../utils/utils.js');
  6. //used for static files
  7. const includes = require('../includes/includes');
  8. //for parsing post data
  9. const qs = require('querystring');
  10. const sanitizer = require('sanitizer');
  11. const Request = require('request');
  12. const nodemailer = require("nodemailer");
  13. const smtpTransport = require('nodemailer-smtp-transport');
  14. const CAPTCHA_SECRET = utils.getFileLine("../captcha_secret");
  15. const EMAIL_PASSWORD = utils.getFileLine("../email_password");
  16. const verifyCapcha = function(data)
  17. {
  18. const recaptcha_url = "https://www.google.com/recaptcha/api/siteverify?" +
  19. "secret=" + CAPTCHA_SECRET + "&" +
  20. "response=" + data;
  21. return sync = new Promise(function(resolve, reject)
  22. {
  23. Request(recaptcha_url,
  24. function (error, response, body)
  25. {
  26. if (!error && response.statusCode == 200)
  27. {
  28. const googleAnswer = JSON.parse(body);
  29. if(googleAnswer.success == true)
  30. {
  31. resolve(true);
  32. }
  33. else
  34. {
  35. resolve(false);
  36. }
  37. }
  38. else
  39. {
  40. resolve(false);
  41. }
  42. }
  43. );
  44. });
  45. };
  46. const sendEmail = function(name, email, message)
  47. {
  48. const transporter = nodemailer.createTransport(smtpTransport({
  49. service: 'gmail',
  50. host: 'smtp.gmail.com',
  51. auth: {
  52. user: 'jrtechswebsite@gmail.com',
  53. pass: EMAIL_PASSWORD
  54. }
  55. }));
  56. const mailOptions =
  57. {
  58. to: "jeffery@jrtechs.net", // list of receivers
  59. subject: "Jrtechs.net form submission", // Subject line
  60. text: message, // plaintext body
  61. html: message
  62. };
  63. // send mail with defined transport object
  64. transporter.sendMail(mailOptions, function(error, response)
  65. {
  66. if(error)
  67. {
  68. console.log(error);
  69. }
  70. else
  71. {
  72. console.log("Message sent: " + response);
  73. }
  74. // if you don't want to use this transport object anymore, uncomment following line
  75. transporter.close(); // shut down the connection pool, no more messages
  76. });
  77. };
  78. const processContactPage = function(request)
  79. {
  80. return new Promise(function(resolve, reject)
  81. {
  82. utils.getPostData(request).then(function(postData)
  83. {
  84. const data = qs.parse(postData);
  85. if(data.name && data.email && data["g-recaptcha-response"] && data.message)
  86. {
  87. verifyCapcha(sanitizer.sanitize(data["g-recaptcha-response"])).then(function(valid)
  88. {
  89. if(valid)
  90. {
  91. resolve(utils.include("includes/messageSent.html"));
  92. sendEmail(data.name, data.email, data.message);
  93. }
  94. else
  95. {
  96. resolve(utils.include("includes/invalidCaptcha.html"));
  97. }
  98. });
  99. }
  100. else
  101. {
  102. resolve(utils.include("includes/contact.html"));
  103. }
  104. }).catch(function(err)
  105. {
  106. reject(err);
  107. })
  108. });
  109. };
  110. module.exports =
  111. {
  112. main: function(request, result)
  113. {
  114. result.writeHead(200, {'Content-Type': 'text/html'});
  115. Promise.all([includes.printAdminHeader(),
  116. processContactPage(request),
  117. require("../sidebar/sidebar.js").main(),
  118. includes.printFooter()]).then(function(content)
  119. {
  120. result.write(content.join(''));
  121. result.end();
  122. }).catch(function(err)
  123. {
  124. console.log(err);
  125. });
  126. }
  127. };