-
Notifications
You must be signed in to change notification settings - Fork 342
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 #1724 from AyushSharma72/email
added send email functionality
- Loading branch information
Showing
6 changed files
with
236 additions
and
142 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,15 +1,47 @@ | ||
|
||
const ContactUs = require('../models/ContactUs'); | ||
|
||
const ContactUs = require("../models/ContactUs"); | ||
const nodemailer = require("nodemailer"); | ||
exports.submitContactForm = async (req, res) => { | ||
const { name, email, message } = req.body; | ||
|
||
try { | ||
const newContact = new ContactUs({ name, email, message }); | ||
await newContact.save(); | ||
return res.status(201).json({ message: 'Message sent successfully!' }); | ||
return res.status(201).json({ message: "Message sent successfully!" }); | ||
} catch (error) { | ||
console.error('Error saving contact form:', error); | ||
return res.status(500).json({ message: 'Failed to send message. Please try again later.' }); | ||
console.error("Error saving contact form:", error); | ||
return res | ||
.status(500) | ||
.json({ message: "Failed to send message. Please try again later." }); | ||
} | ||
}; | ||
|
||
exports.sendEmail = async (req, resp) => { | ||
const transporter = nodemailer.createTransport({ | ||
service: "gmail", | ||
auth: { | ||
user: "your email id", | ||
pass: "your password", | ||
}, | ||
}); | ||
|
||
const mailOptions = { | ||
from: req.body.Email, | ||
to: "your email id", | ||
subject: "BuddyTrail user message", | ||
text: ` | ||
Name: ${req.body.Name} | ||
Phone:${req.body.Phone} | ||
Email: ${req.body.Email} | ||
Message: ${req.body.Message}`, | ||
}; | ||
|
||
transporter.sendMail(mailOptions, (error, info) => { | ||
if (error) { | ||
console.log("Error sending email: " + error); | ||
resp.status(500).send("Error sending email"); | ||
} else { | ||
console.log("Email sent: " + info.response); | ||
resp.status(200).send("Form data sent successfully"); | ||
} | ||
}); | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,9 +1,11 @@ | ||
const express = require('express'); | ||
const { submitContactForm } = require('../controllers/ContactController.js'); | ||
|
||
const express = require("express"); | ||
const { | ||
submitContactForm, | ||
sendEmail, | ||
} = require("../controllers/ContactController.js"); | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/', submitContactForm); | ||
|
||
router.post("/", submitContactForm); | ||
router.post("/email", sendEmail); | ||
module.exports = router; |
Oops, something went wrong.