I am creating a website for a restaurant where there will be multiple contact forms. I know this is probably stupid, but help without judgement would be much appreciated.
I am using node.js with express and nodemailer dependencies to send the forms. I have my main contact form in the footer working and sending perfectly – simple email, name, message inputs.
What I need is an additional form – separate page – for private event bookings/inquiries. This will be more thorough and have more fields. Some will be radio buttons to chose from a set of options, some will be text inputs.
I just need to know if I need to make a server2.js file or if I can handle both forms in one server file. I would think create a separate server file as it would be messy not to, no?
As well, in my main app.js file, I would just create a new XML variable and basically do the same thing correct?
This is my contact form html
<section id="hm-contact">
<div class="form-container">
<form class="contact-form">
<h2>CONTACT</h2>
<input type="text" id="name" placeholder="Full Name"></br>
<input type="email" id="email" placeholder="Email"></br>
<input type="text" id="subject" placeholder="Subject"></br>
<textarea id="message" placeholder="Message..."></textarea></br>
<input type="submit" class="submit" value="Send">
</form>
</div>
<div class="map">
<img src="/img/HmMapImg.png" alt="Map with El Tres location">
</div>
<div class="contact-info">
<p class="hours">Hours</p>
<div class="weekday-hours">
<p class="week-days">Tuesday - Thursday</p>
<p class="week-hrs">5pm - 10pm</p>
</div>
<div class="weekend-hours">
<p class="weekend-days">Friday & Saturday</p>
<p class="frisat-hrs">5pm - 1am</p>
</div>
<div class="tequila-night">
<p class="tequila-day">Half Price Tequila Fridays</p>
<p class="tequila-hrs">9pm - 11pm</p>
</div>
<div class="location">
<p class="address">269 Red River Rd.</p>
<p class="city">Thunder Bay, ON P7B 1A9</p>
<p class="phone">(807) 344-3443</p>
</div>
</div>
This is my server.js so far (of course I will be putting sensitive info in a .env file, but for dev and testing it’s here for now until launch (which speaking of .env, will I need two files or can I use one .env for both emails?)
const express = require('express');
const app = express();
const nodemailer = require('nodemailer');
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.static('public'));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.post('/', (req, res) => {
console.log(req.body);
const transporter = nodemailer.createTransport({
host: 'mail.myntbranding.com',
port: 465,
auth: {
user:'[email protected]',
pass: 'password'
}
})
const mailOptions = {
from: req.body.email,
to: 'email@email',
subject: `Message from ${req.body.email}: ${req.body.subject}`,
text: req.body.message
}
transporter.sendMail(mailOptions, (error, info) => {
if(error){
console.log(error);
res.send('error');
}else{
console.log('Email sent:' + info.response);
res.send('success')
}
});
})
app.listen(PORT, ()=>{
console.log(`Server running on port ${PORT}`);
});
And then this is my app.js
const contactForm = document.querySelector('.contact-form');
let name = document.getElementById('name');
let email = document.getElementById('email');
let subject = document.getElementById('subject');
let message = document.getElementById('message');
contactForm.addEventListener('submit', (e) => {
e.preventDefault();
let formData = {
name: name.value,
email: email.value,
subject: subject.value,
message: message.value
}
let xhr = new XMLHttpRequest();
xhr.open('POST', '/');
xhr.setRequestHeader('content-type', 'application/json');
xhr.onload = function(){
console.log(xhr.responseText);
if(xhr.responseText == 'success') {
alert('Email Sent');
name.value = '';
email.value = '';
subject.value = '';
message.value = '';
}else{
alert('Something went wrong!');
}
}
xhr.send(JSON.stringify(formData));
});
Thank you in advance for your help!
I haven’t tried anything yet, I want to seek out some advice and then see whatsup when I test it out. I’ve also been looking for tutorials but I can’t find anything on how to handle more than one contact form with node. Thanks y’all.