Skip to content

Commit

Permalink
Merge pull request #1724 from AyushSharma72/email
Browse files Browse the repository at this point in the history
added send email functionality
  • Loading branch information
samarth-6 authored Oct 29, 2024
2 parents 3435771 + 0486b80 commit 1253602
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 142 deletions.
44 changes: 38 additions & 6 deletions backend/controllers/ContactController.js
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");
}
});
};
36 changes: 19 additions & 17 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Import required modules
const express = require('express');
const connectDB = require('./config/db'); // Database connection
const authRoutes = require('./routes/authRoutes'); // Authentication routes
const ContactRoutes = require('./routes/ContactRoutes');
const cookieParser = require('cookie-parser'); // Middleware for parsing cookies
const config = require('./config/config'); // Config file for environment variables
const cors = require('cors'); // Middleware for Cross-Origin Resource Sharing
const express = require("express");
const connectDB = require("./config/db"); // Database connection
const authRoutes = require("./routes/authRoutes"); // Authentication routes
const ContactRoutes = require("./routes/ContactRoutes");
const cookieParser = require("cookie-parser"); // Middleware for parsing cookies
const config = require("./config/config"); // Config file for environment variables
const cors = require("cors"); // Middleware for Cross-Origin Resource Sharing

// Initialize express app
const app = express();
Expand All @@ -14,21 +14,23 @@ const app = express();
connectDB();

// CORS configuration
app.use(cors({
origin: 'http://127.0.0.1:5504', // Correct: specify the base URL only
credentials: true // Allow credentials (cookies) to be sent with requests
}));
app.use(
cors({
origin: "http://127.0.0.1:5505", // Correct: specify the base URL only
credentials: true, // Allow credentials (cookies) to be sent with requests
})
);

// Middleware
app.use(express.json()); // Parse JSON bodies
app.use(cookieParser()); // Enable cookie parsing
app.use(express.json()); // Parse JSON bodies
app.use(cookieParser()); // Enable cookie parsing

// API routes
app.use('/api/auth', authRoutes);
app.use('/api/contact', ContactRoutes);
app.use("/api/auth", authRoutes);
app.use("/api/contact", ContactRoutes);

// Server listening on configured port
const PORT = config.port || 5000; // Use config for port, default to 5000 if undefined
const PORT = config.port || 5000; // Use config for port, default to 5000 if undefined
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Server running on port ${PORT}`);
});
12 changes: 11 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"express": "^4.21.1",
"express-rate-limit": "^7.4.1",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.7.3"
"mongoose": "^8.7.3",
"nodemailer": "^6.9.16"
}
}
12 changes: 7 additions & 5 deletions backend/routes/ContactRoutes.js
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;
Loading

0 comments on commit 1253602

Please sign in to comment.