I have created a frontend page which has got a send email section which I want to connect with the server side page which is the email page. How will I integrate the server side code with my frontend code? I’m using node js for backend.
Here is the section code of the email
<section class="contactUs" id="Contact">
<div class="form-container">
<div class="text">Request Quote Now</div>
<form class="contact-form">
<div class="form-row">
<div class="input-data">
<input type="text" id="username" name="username" required>
<div class="underline"></div>
<label for="username">Name</label>
</div>
<div class="input-data">
<input type="email" id="email" name="email" required>
<div class="underline"></div>
<label for="email">Email</label>
</div>
</div>
<div class="form-row">
<div class="input-data">
<input type="text" id="subject" name="subject" required>
<div class="underline"></div>
<label for="subject">Subject</label>
</div>
<div class="input-data">
<input type="text" id="websiteName" name="websiteName" required>
<div class="underline"></div>
<label for="websiteName">Business Name</label>
</div>
</div>
<div class="form-row">
<div class="input-data textarea">
<textarea id="message" name="message" required></textarea>
<div class="underline"></div>
<label for="message">Message</label>
</div>
</div>
<div class="form-row submit-btn">
<div class="input-data">
<div class="inner"></div>
<input type="submit" class="submit" value="GET A QUOTE">
</div>
</div>
</form>
</div>
</section>
Here is the server side code
require('dotenv').config();
const express = require('express');
const nodemailer = require('nodemailer');
const app = express();
const PORT = process.env.PORT || 2000;
app.use(express.static('public'));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/myweb.html'));
});
// Route to handle form submission
app.post('/send', (req, res) => {
const { name, email, subject, websiteName, message } = req.body;
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '',
pass: ''
}
});
const mailOptions = {
from: email,
to: '[email protected]',
subject: `Message from ${email}: ${subject}`,
text:`Name: ${name}nWebsite: ${websiteName}nMessage: ${message}`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
return res.status(500).send('Something went wrong');
}
console.log('Email sent: ' + info.response);
res.status(200).send('Success');
});
});
// Start the server and listen on the specified port
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
New contributor
williamson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.