Fix contact form
This commit is contained in:
parent
0b38e1fa07
commit
c483e8f079
57
app.js
57
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 = `
|
||||
<p>You have a new contact request from <strong>${name}</strong>.</p>
|
||||
<h3>Contact Details</h3>
|
||||
<ul>
|
||||
<li><strong>Name:</strong> ${name}</li>
|
||||
<li><strong>Email:</strong> ${email}</li>
|
||||
<li><strong>Subject:</strong> ${subject}</li>
|
||||
</ul>
|
||||
<h3>Message</h3>
|
||||
<p>${message}</p>
|
||||
`;
|
||||
|
||||
// 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}" <quote@node-geeks.com>`,
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user