-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #545 from AyushSharma72/Email
Added Email functionality to the contact form issue #506
- Loading branch information
Showing
3 changed files
with
294 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
Oops, something went wrong.