Skip to content

Commit

Permalink
add subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebzaki-07 committed Oct 21, 2024
1 parent f56c151 commit b8feb24
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ JWT_REFRESH_TOKEN=ImagineAiRefreshToken
GOOGLE_CLIENT_ID=829264938526-gklvdpj86hpc8fn0b58u9lc5p56da1jv.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=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
GOOGLE_AUTH_URI=https://imagine-ai-17zf.vercel.app/auth/google/callback
EMAIL_USERNAME=your_email_username
EMAIL_PASSWORD=your_email_password
EMAIL_USER=your_email_username
EMAIL_PASS=your_email_password

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
40 changes: 40 additions & 0 deletions controllers/subscribeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const nodemailer = require('nodemailer');
const subscribeToEmail = async (req, res) => {
const { email } = req.body;

if (!/^[^\s@]+@gmail\.com$/i.test(email)) {
return res.status(400).send('Invalid Gmail address.');
}

let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
}
});


let mailOptions = {
from: process.env.EMAIL_USER,
to: email,
subject: 'Thank you for subscribing IMAGINE AI!',
text: 'Thank you for subscribing to our platform!',
html: '<h1>Thank you!</h1><p>You have successfully subscribed to our platform.</p>',
};



try {
await transporter.sendMail(mailOptions);
console.log('Email sent successfully to ' + email);
res.status(200).send('Subscription successful. Thank you email sent!');
} catch (error) {
console.error('Error sending email: ', error);
res.status(500).send('Error sending email.');
}
}

module.exports = {
subscribeToEmail,
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ app.get("/file", (req, res) => {
app.use("/openai", require("./routes/openaiRoutes"));
app.use("/auth", require("./routes/auth.js"));
app.use("/send-feedback", require("./routes/feedback.js"));
app.use("/subscribe" , require("./routes/subscribe.js"));
app.use(viewRoutes);

app.listen(process.env.PORT, () =>
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

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

10 changes: 10 additions & 0 deletions routes/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express');

const { subscribeToEmail } = require('../controllers/subscribeController');

const router = express.Router();

router.post('/', subscribeToEmail);

module.exports = router;

34 changes: 20 additions & 14 deletions views/includes/footer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -171,32 +171,38 @@
document.addEventListener('DOMContentLoaded', function () {
var subscribeButton = document.getElementById('subscribe_button');
var subscribeButton = document.getElementById('subscribe_button');
var form = document.getElementById('subscription-form');
subscribeButton.addEventListener('click', function (event) {
subscribeButton.addEventListener('click', function (event) {
event.preventDefault();
var emailInput = document.getElementById('email_input');
var email = emailInput.value.trim();
if (validateEmail(email)) {
showMessage('Thank you for subscribing!');
fetch('/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email })
}).then(response => {
if (response.ok) {
toastr.success('Thank you for subscribing! A confirmation email has been sent.', 'Success');
} else {
toastr.warning('Something went wrong. Please try again later.', 'Error');
}
});
} else {
console.log('Please enter a valid Gmail address.');
toastr["warning"]("Please provide valid Email Id.", "Validation")
toastr.warning("Please provide a valid Gmail address.", "Validation");
}
});
});
function validateEmail(email) {
function validateEmail(email) {
var emailRegex = /^[^\s@]+@gmail\.com$/i;
return emailRegex.test(email);
}
function showMessage(msg) {
toastr.success(msg, "Success");
}
})
}
});
</script>

Expand Down

0 comments on commit b8feb24

Please sign in to comment.