-
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 #881 from 17arindam/newsletter
Newsletter storing in database and a email is being sent to user
- Loading branch information
Showing
10 changed files
with
187 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
EMAIL_USER=your_gmail | ||
EMAIL_PASS=your_16_digit_pass |
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ async function ConnectDb() { | |
try { | ||
// const DatabaseConnect = process.env.DatabaseConnect; | ||
await mongoose.connect( | ||
"Your MongoDb Connection String" //establish database connection // mongodb+srv://username:[email protected]/DatabseName | ||
"mongodb://localhost:27017/" //establish database connection // mongodb+srv://username:[email protected]/DatabseName | ||
); | ||
|
||
console.log("connected to database"); | ||
|
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,49 @@ | ||
require("dotenv").config(); | ||
const nodemailer = require("nodemailer"); | ||
|
||
|
||
// Create a Nodemailer transporter using SMTP | ||
const transporter = nodemailer.createTransport({ | ||
service: "gmail", // or your preferred email service | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
}); | ||
|
||
// Function to send newsletter subscription confirmation via email | ||
exports.sendSubscriptionConfirmation = async (email) => { | ||
// Construct the email content | ||
const emailText = ` | ||
Dear Customer, | ||
Thank you for subscribing to the WordWise newsletter! | ||
We're thrilled to welcome you to our community of passionate writers and language enthusiasts. | ||
If you have any questions or feedback, feel free to reach out. | ||
Best regards, | ||
WordWise Team | ||
`; | ||
|
||
try { | ||
await transporter.sendMail({ | ||
from: process.env.EMAIL_USER, | ||
to: email, | ||
subject: "Thank You for Subscribing!", | ||
text: emailText, | ||
}); | ||
|
||
} catch (error) { | ||
|
||
if (error.code === "ECONNREFUSED") { | ||
throw new Error( | ||
"Failed to connect to email server. Please try again later.", | ||
); | ||
} else { | ||
throw new Error( | ||
`Failed to send subscription confirmation email: ${error.message}`, | ||
); | ||
} | ||
} | ||
}; |
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,44 @@ | ||
const NewsletterEmail = require("../modal/NewsLetterModel"); // Import the Mongoose model | ||
const { sendSubscriptionConfirmation } = require("../config/nodemailer"); // Import the mailer function | ||
|
||
// Controller for handling newsletter subscriptions | ||
exports.subscribeToNewsletter = async (req, res) => { | ||
const { email } = req.body; | ||
|
||
if (!email) { | ||
return res.status(400).json({ error: "Email is required" }); | ||
} | ||
|
||
try { | ||
// Check if the email already exists in the database | ||
const existingEmail = await NewsletterEmail.findOne({ email }); | ||
if (existingEmail) { | ||
return res | ||
.status(400) | ||
.json({ error: "This email is already subscribed." }); | ||
} | ||
|
||
// Save the email to the database | ||
const newEmail = new NewsletterEmail({ email }); | ||
await newEmail.save(); | ||
|
||
try { | ||
await sendSubscriptionConfirmation(email); | ||
} catch (error) { | ||
console.error("Error sending confirmation email:", error); | ||
return res.status(500).json({ | ||
error: | ||
"Subscription successful, but there was an error sending the confirmation email.", | ||
}); | ||
} | ||
|
||
return res.status(201).json({ | ||
message: "Subscription successful! A confirmation email has been sent.", | ||
}); | ||
} catch (error) { | ||
console.error("Error subscribing to newsletter:", error); | ||
return res | ||
.status(500) | ||
.json({ error: "Error subscribing to the newsletter." }); | ||
} | ||
}; |
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,20 @@ | ||
/* eslint-disable no-useless-escape */ | ||
const mongoose = require("mongoose"); | ||
|
||
// Define the schema for newsletter emails | ||
const NewsletterEmailSchema = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, // Ensure no duplicate emails | ||
trim: true, | ||
match: [/.+\@.+\..+/, "Please enter a valid email address"], // Simple email validation | ||
}, | ||
subscribedAt: { | ||
type: Date, | ||
default: Date.now, // Automatically set the date of subscription | ||
}, | ||
}); | ||
|
||
// Export the model | ||
module.exports = mongoose.model("NewsletterEmail", NewsletterEmailSchema); |
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
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