-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f81c0ed
commit 88f258b
Showing
5 changed files
with
110 additions
and
59 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
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,12 +1,12 @@ | ||
const Newsletter = require("../Models/Newsletter"); | ||
const nodemailer = require("nodemailer"); | ||
const { sendMailToSubscriber } = require("../sendSubscribeMail"); | ||
|
||
const subscribeNewsletter = async (req, res) => { | ||
try { | ||
const { email } = req.body; | ||
const { name, email } = req.body; | ||
|
||
// Check if email is provided | ||
if (!email) { | ||
if (!email || !name) { | ||
return res.status(400).json({ message: "Email is required" }); | ||
} | ||
|
||
|
@@ -17,39 +17,10 @@ const subscribeNewsletter = async (req, res) => { | |
} | ||
|
||
// Save the subscriber email to the database | ||
const newSubscriber = new Newsletter({ email }); | ||
const newSubscriber = new Newsletter({ name, email }); | ||
await newSubscriber.save(); | ||
|
||
// Configure the transporter | ||
const transporter = nodemailer.createTransport({ | ||
service: "gmail", | ||
auth: { | ||
user: process.env.EMAIL_USER, // Your email | ||
pass: process.env.EMAIL_PASS, // Your email password (use environment variables in production) | ||
}, | ||
}); | ||
|
||
const mailOptions = { | ||
from: "[email protected]", | ||
to: email, | ||
subject: "Thank you for Subscribing to Our Newsletter", | ||
html: ` | ||
<div style="font-family: Arial, sans-serif; text-align: center;"> | ||
<h2>Thank You for Subscribing!</h2> | ||
<p>Dear Subscriber,</p> | ||
<p>We are thrilled to have you with us. Stay tuned for our latest updates and offers!</p> | ||
<a href="https://bitbox-in.netlify.app/" | ||
style="display: inline-block; padding: 10px 20px; margin-top: 20px; color: white; background-color: #007BFF; text-decoration: none; border-radius: 5px;"> | ||
Explore More | ||
</a> | ||
<p style="margin-top: 30px;">Best Regards,<br>BitBox Team</p> | ||
</div> | ||
`, | ||
}; | ||
|
||
// Send the confirmation email | ||
await transporter.sendMail(mailOptions); | ||
|
||
sendMailToSubscriber(newSubscriber) | ||
res.status(200).json({ message: "Subscription successful, confirmation email sent" }); | ||
} catch (error) { | ||
console.error("Error in subscribing to newsletter:", 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 |
---|---|---|
@@ -1,21 +1,26 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const NewsletterSchema = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
match: [/\S+@\S+\.\S+/, 'Please enter a valid email address'] | ||
}, | ||
subscribedAt: { | ||
type: Date, | ||
default: Date.now | ||
}, | ||
status: { | ||
type: String, | ||
enum: ['Subscribed', 'Unsubscribed'], | ||
default: 'Subscribed' | ||
} | ||
name: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
match: [/\S+@\S+\.\S+/, 'Please enter a valid email address'] | ||
}, | ||
subscribedAt: { | ||
type: Date, | ||
default: Date.now | ||
}, | ||
status: { | ||
type: String, | ||
enum: ['Subscribed', 'Unsubscribed'], | ||
default: 'Subscribed' | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model("Newsletter", NewsletterSchema); |
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,65 @@ | ||
const nodemailer = require('nodemailer'); | ||
require('dotenv').config(); | ||
|
||
const sendMailToSubscriber = (userdata) => { | ||
const transporter = nodemailer.createTransport({ | ||
service: "gmail", | ||
host: "smtp.gmail.com", | ||
port: 587, | ||
secure: false, | ||
auth: { | ||
user: process.env.EMAIL_ID, | ||
pass: process.env.PASS_KEY, | ||
}, | ||
}); | ||
|
||
async function main() { | ||
try { | ||
await transporter.sendMail({ | ||
from: { | ||
name: "BitBox", | ||
address: process.env.EMAIL_ID, | ||
}, | ||
to: userdata.email, | ||
subject: "Welcome to BitBox! 🚀 Join the Developer Community", | ||
text: "Thank you for joining BitBox, your hub for project sharing and collaboration!", | ||
html: ` | ||
<div style="background-color: #f4f4f9; color: #333; padding: 20px; font-family: Arial, sans-serif;"> | ||
<div style="max-width: 600px; margin: 0 auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);"> | ||
<h2 style="text-align: center; color: #4a90e2;">Welcome to BitBox, ${userdata.name}!</h2> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Hi ${userdata.name}, | ||
</p> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Thank you for subscribing to BitBox! We're excited to have you join a growing community of developers who showcase, share, and collaborate on projects. | ||
</p> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
As a BitBox member, you can: | ||
</p> | ||
<ul style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
<li>📂 <strong>Upload Projects:</strong> Easily upload and showcase your projects on our platform.</li> | ||
<li>🔍 <strong>Discover Projects:</strong> Explore a wide range of innovative projects shared by fellow developers.</li> | ||
<li>🤝 <strong>Collaborate:</strong> Connect with other developers to share feedback and collaborate on projects.</li> | ||
<li>📚 <strong>Learn and Grow:</strong> Learn from shared projects and contribute to the community’s knowledge base.</li> | ||
</ul> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Join us as we build a supportive environment where developers can grow, learn, and create together. Welcome aboard! | ||
</p> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Warm regards, <br/> | ||
The BitBox Team | ||
</p> | ||
</div> | ||
</div> | ||
`, | ||
}); | ||
console.log("Email sent successfully to " + userdata.email); | ||
} catch (error) { | ||
console.error("Error sending email:", error); | ||
} | ||
} | ||
|
||
main(); | ||
}; | ||
|
||
module.exports = { sendMailToSubscriber }; |