From c483e8f0792cf65900ae3ec4a2848b8cf5ea9ee2 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 16 Sep 2024 08:07:36 -0400 Subject: [PATCH] Fix contact form --- app.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/app.js b/app.js index 39ebc4a..be4dcbc 100644 --- a/app.js +++ b/app.js @@ -91,6 +91,63 @@ app.get('/contact', (req, res) => { res.render('contact', { title: 'Contact Raven Scott', msg: undefined }); }); +// Handle contact form submission +app.post('/contact', (req, res) => { + const { name, email, subject, message } = req.body; + + // Validate form inputs (basic example) + if (!name || !email || !subject || !message) { + return res.render('contact', { title: 'Contact Raven Scott', msg: 'All fields are required.' }); + } + + // Create email content + const output = ` +

You have a new contact request from ${name}.

+

Contact Details

+ +

Message

+

${message}

+ `; + + // Set up Nodemailer transporter + let transporter = nodemailer.createTransport({ + host: process.env.SMTP_HOST, + port: process.env.SMTP_PORT, + secure: false, // true for 465, false for other ports + auth: { + user: process.env.EMAIL_USER, // Email user from environment variables + pass: process.env.EMAIL_PASS, // Email password from environment variables + }, + tls: { + rejectUnauthorized: false, + }, + }); + + // Set up email options + let mailOptions = { + from: `"${name}" `, + to: process.env.RECEIVER_EMAIL, // Your email address to receive contact form submissions + subject: subject, + html: output, + }; + + // Send email + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + console.error(error); + return res.render('contact', { title: 'Contact Raven Scott', msg: 'An error occurred. Please try again.' }); + } else { + console.log('Email sent: ' + info.response); + return res.render('contact', { title: 'Contact Raven Scott', msg: 'Your message has been sent successfully!' }); + } + }); +}); + + // Blog Post Route app.get('/blog/:slug', (req, res) => { const slug = req.params.slug;