-
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 #1849 from haseebzaki-07/new_branch_4
Add subscribe
- Loading branch information
Showing
7 changed files
with
114 additions
and
25 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,3 +1,5 @@ | ||
MONGO_URI=your_mongo_uri | ||
JWT_SECRET=your_jwt_secret | ||
PORT=5000 | ||
PORT=5000 | ||
ADMIN_EMAIL= | ||
ADMIN_EMAIL_PASSWORD= |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const nodemailer = require('nodemailer'); | ||
const dotenv = require('dotenv'); | ||
dotenv.config(); | ||
|
||
// Configure Nodemailer | ||
const transporter = nodemailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: process.env.ADMIN_EMAIL, | ||
pass: process.env.ADMIN_EMAIL_PASSWORD, | ||
}, | ||
}); | ||
|
||
// Subscription handler | ||
exports.subscribe = async (req, res) => { | ||
const { email } = req.body; | ||
|
||
// Define the confirmation email content | ||
const mailOptions = { | ||
from: "BuddyTrail", | ||
to: email, | ||
subject: 'Thank you for subscribing BuddyTrail!', | ||
text: `Thank you for subscribing to our newsletter! We'll keep you updated with the latest news and offers.`, | ||
}; | ||
|
||
try { | ||
// Send the confirmation email | ||
await transporter.sendMail(mailOptions); | ||
res.status(200).json({ message: 'Subscription successful! A confirmation email has been sent.' }); | ||
} catch (error) { | ||
console.error('Error sending confirmation email:', error); | ||
res.status(500).json({ message: 'Failed to send confirmation email. Please try again later.' }); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const express = require('express'); | ||
const { subscribe } = require('../controllers/subscribeController.js'); | ||
|
||
|
||
const router = express.Router(); | ||
|
||
router.post('/', subscribe); | ||
|
||
module.exports = router; |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
document.addEventListener("DOMContentLoaded", () => { | ||
const form = document.getElementById("newsletter-form"); | ||
const subscriptionMessage = document.getElementById("subscription-message"); | ||
|
||
form.addEventListener("submit", async (event) => { | ||
event.preventDefault(); | ||
|
||
const email = document.getElementById("email").value; | ||
|
||
try { | ||
const response = await fetch("http://localhost:5000/api/subscribe/", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ email }), | ||
}); | ||
|
||
if (response.ok) { | ||
subscriptionMessage.textContent = "Thank you for subscribing!"; | ||
subscriptionMessage.style.color = "green"; | ||
form.reset(); | ||
} else { | ||
const errorData = await response.json(); | ||
subscriptionMessage.textContent = errorData.message || "Subscription failed. Please try again."; | ||
subscriptionMessage.style.color = "red"; | ||
} | ||
} catch (error) { | ||
console.error("Error:", error); | ||
subscriptionMessage.textContent = "An error occurred. Please try again."; | ||
subscriptionMessage.style.color = "red"; | ||
} | ||
}); | ||
}); | ||
|