Skip to content

Commit

Permalink
Merge pull request #545 from AyushSharma72/Email
Browse files Browse the repository at this point in the history
Added Email functionality to the contact form issue #506
  • Loading branch information
ANSHIKA-26 authored Oct 8, 2024
2 parents 36ae3f9 + dfb9098 commit bf69f4e
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 241 deletions.
100 changes: 51 additions & 49 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,73 @@
const express = require('express');
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require("express");
const nodemailer = require("nodemailer");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 3000;

app.use(bodyParser.json());
app.use(cors());
app.use(cors());

app.post('/send-email', async (req, res) => {
const { firstName, lastName, email, phone, message } = req.body;
app.post("/send-email", async (req, res) => {
const { firstName, lastName, email, phone, message } = req.body;

let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'your-email-password'
}
});
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]", // add you email
pass: "your-email-password", // add your password
},
});

let mailOptions = {
from: email,
to: '[email protected]',
subject: `Contact Us Form Submission from ${firstName} ${lastName}`,
text: `You have received a new message from your website contact form.
let mailOptions = {
from: email,
to: "[email protected]", // add email where you want to send the message
subject: `Contact Us Form Submission from ${firstName} ${lastName}`,
text: `You have received a new message from your website contact form.
Name: ${firstName} ${lastName}
Email: ${email}
Phone: ${phone}
Message: ${message}`
};
Message: ${message}`,
};

try {
await transporter.sendMail(mailOptions);
res.status(200).send({ message: "Email sent successfully!" });
} catch (error) {
res.status(500).send({ message: "Error sending email", error });
}
try {
await transporter.sendMail(mailOptions);
res.status(200).send({ message: "Email sent successfully!" });
} catch (error) {
res.status(500).send({ message: "Error sending email", error });
}
});

// New endpoint for newsletter subscription
app.post('/subscribe', async (req, res) => {
const { email } = req.body;
app.post("/subscribe", async (req, res) => {
const { email } = req.body;

let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'your-email-password'
}
});
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "your-email-password",
},
});

let mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'New Newsletter Subscription',
text: `You have a new subscriber! Email: ${email}`
};
let mailOptions = {
from: "[email protected]",
to: "[email protected]",
subject: "New Newsletter Subscription",
text: `You have a new subscriber! Email: ${email}`,
};

try {
await transporter.sendMail(mailOptions);
res.status(200).send({ message: "Subscription successful!" });
} catch (error) {
res.status(500).send({ message: "Error sending subscription email", error });
}
try {
await transporter.sendMail(mailOptions);
res.status(200).send({ message: "Subscription successful!" });
} catch (error) {
res
.status(500)
.send({ message: "Error sending subscription email", error });
}
});

app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Server is running on port ${PORT}`);
});
Loading

0 comments on commit bf69f4e

Please sign in to comment.