Hopefully worked, switched email method

pull/1/head
Esther 2020-07-01 15:00:52 +01:00
parent 111724c8e7
commit 02ae139566
No known key found for this signature in database
GPG Key ID: 162A307C5EBD40EA
4 changed files with 101 additions and 37 deletions

65
package-lock.json generated
View File

@ -34,6 +34,61 @@
"kuler": "^2.0.0"
}
},
"@sendgrid/client": {
"version": "7.2.1",
"resolved": "https://registry.npmjs.org/@sendgrid/client/-/client-7.2.1.tgz",
"integrity": "sha512-QizSa+qKnq5xquMyeB2x0l2JNH3jvg3zHmGt0Ghz+DLUGpW8i1m/zvrqSZ0wdcGVO4/2lW3WFN0drv1+jOxVgA==",
"requires": {
"@sendgrid/helpers": "^7.2.0",
"axios": "^0.19.2"
}
},
"@sendgrid/helpers": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/@sendgrid/helpers/-/helpers-7.2.0.tgz",
"integrity": "sha512-Ps8rc3xFpaQvIWoW1zlWi9G7QnxYwXFOeq0915DzXf5knoiOgXdFk/XBXbCFjr2lf/JIzjgFUtAV/dN8Z8jzHw==",
"requires": {
"chalk": "^2.0.1",
"deepmerge": "^4.2.2"
},
"dependencies": {
"ansi-styles": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
"integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
"requires": {
"color-convert": "^1.9.0"
}
},
"chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
"integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
"requires": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
}
},
"supports-color": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
"integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==",
"requires": {
"has-flag": "^3.0.0"
}
}
}
},
"@sendgrid/mail": {
"version": "7.2.1",
"resolved": "https://registry.npmjs.org/@sendgrid/mail/-/mail-7.2.1.tgz",
"integrity": "sha512-LBAdwL+c6HmN2B/Q24LFy2do/IzBFeFoF05xBqgELxp8lMUwis4fbqgo09Zyj1CY0qHN+CfWcUfLNvHfVCh8oQ==",
"requires": {
"@sendgrid/client": "^7.2.1",
"@sendgrid/helpers": "^7.2.0"
}
},
"abbrev": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -515,6 +570,11 @@
"resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
"integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA="
},
"deepmerge": {
"version": "4.2.2",
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz",
"integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg=="
},
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
@ -897,6 +957,11 @@
"ansi-regex": "^2.0.0"
}
},
"has-flag": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0="
},
"has-symbols": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz",

View File

@ -7,6 +7,7 @@
"start": "npx nodemon ./bin/www"
},
"dependencies": {
"@sendgrid/mail": "^7.2.1",
"app-root-path": "^3.0.0",
"axios": "^0.19.2",
"cookie-parser": "1.4.4",

View File

@ -8,9 +8,9 @@ Environment=PORT=7000
Environment=NODE_ENV=production
Environment=GHOST_KEY=key
Environment=HCAPTCHA_KEY=key
Environment=TO_GMAIL_USER=user
Environment=FROM_GMAIL_USER=user
Environment=GMAIL_PASS=pass
Environment=SENDGRID_API_KEY=key
Environment=TO_MAIL_USER=user
Environment=FROM_MAIL_USER=user
StandardOutput=syslog
SyslogIdentifier=pastel-codes
User=web

View File

@ -4,20 +4,28 @@ const {verify} = require('hcaptcha');
const nodemailer = require('nodemailer')
var router = express.Router();
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const contact_rate_limit = rate_limit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 5, // limit each IP to 10 requests per windowMs
message: "Too many contact requests, try again later.",
handler: function(req, res /*, next*/) {
res.render('error', {title:"Error", message: "Too many contact requests, try again later.", error: {status: null}})
handler: function (req, res /*, next*/) {
res.render('error', {
title: "Error",
message: "Too many contact requests, try again later.",
error: {status: null}
})
},
});
// POST route from contact form
router.post('/', contact_rate_limit,(req, res) => {
router.post('/', contact_rate_limit, (req, res) => {
const TO_GMAIL_USER = process.env.TO_GMAIL_USER
const FROM_GMAIL_USER = process.env.FROM_GMAIL_USER
const GMAIL_PASS = process.env.GMAIL_PASS
console.log(GMAIL_PASS)
const HCAPTCHA_KEY = process.env.HCAPTCHA_KEY
const token = req.body["g-recaptcha-response"];
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
@ -25,51 +33,41 @@ router.post('/', contact_rate_limit,(req, res) => {
verify(HCAPTCHA_KEY, token)
.then((data) => {
if (data.success === true) {
// Instantiate the SMTP server
const smtpTrans = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: FROM_GMAIL_USER,
pass: GMAIL_PASS
}
})
// Specify what the email will look like
const mailOpts = {
from: 'Pastel.codes Contact Notifications', // This is ignored by Gmail
const msg = {
to: TO_GMAIL_USER,
from: FROM_GMAIL_USER,
subject: 'New message from contact form at pastel.codes',
text: `${req.body.firstname} ${req.body.lastname} (${req.body.email})\nsays: ${req.body.message}\n\nip: ${ip}`
}
};
// maybe send conformation email?
// Attempt to send the email
smtpTrans.sendMail(mailOpts, (error, response) => {
if (error) {
sgMail
.send(msg)
.then(() => {
res.render('contact', {
message: "I will get back to you soon!",
success: "Make sure the email is from ",
email: TO_GMAIL_USER
})
})
.catch(error => {
console.log(error)
res.render('error', { message: "Email did not send" }) // Show a page indicating failure
}
else {
res.render('contact', { message: "I will get back to you soon!", success: "Make sure the email is from ", email: TO_GMAIL_USER }) // Show a page indicating success
}
})
res.render('error', {message: "Email did not send"})
});
} else {
// rerender with same info in the text box and show error message
res.render('contact', { message: "Captcha failed, try again" });
// // rerender with same info in the text box and show error message
res.render('contact', {message: "Captcha failed, try again"});
}
})
.catch(error => {
console.log(error);
res.render('contact', { message: "Something wrong happened, try again later" });
res.render('contact', {message: "Something wrong happened, try again later"});
});
})
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('contact', { title: 'Contact', description: "Contact me!"});
router.get('/', function (req, res, next) {
res.render('contact', {title: 'Contact', description: "Contact me!"});
});
module.exports = router;