Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: Welcome email for New Subscribers added #1121

Merged
merged 8 commits into from
Aug 9, 2024
31 changes: 31 additions & 0 deletions backend/app/routes/subscribers/postSubscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ const to = require("await-to-js").default;
const Subscriber = require('../../models/Subscriber');
const { ErrorHandler } = require('../../../helpers/error')
const constants = require('../../../constants');
const nodemailer = require('nodemailer')
const config = require('../../../config')
const { welcomeSubscriberMailTemplate } = require('../../../utility/emailTemplates')

module.exports = async (req, res, next) => {
const transporter = nodemailer.createTransport({
type: 'SMTP',
host: config.EMAIL_HOST,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add env.sample file and add these env variables with required values/placeholders(in case of credential)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no need for env.sample file because, .env file already there with required values/placeholders.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then add the new env variables that got introduced in this pr there itself. It will be easier for new developers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no new varaible used here and they already exists in .env file

secure: true,
debug: true,
port: 465,
auth: {
user: config.EMAIL_USER,
pass: config.EMAIL_PASS,
},
});
const [err, response] = await to(Subscriber.create({ ...req.body }));
if (err) {
if (err.code === 11000) {
Expand All @@ -21,6 +35,23 @@ module.exports = async (req, res, next) => {
});
return next(error);
}
const mailOptions = {
from: `HITK TECH Community <${config.EMAIL_USER}>`,
to: req.body.email,
subject: 'Welcome to the HITK TECH Community newsletter 😍',
html: welcomeSubscriberMailTemplate(),
};
await transporter.sendMail(mailOptions).catch((err) => {
if (err) {
const error = new ErrorHandler(constants.ERRORS.UNEXPECTED, {
statusCode: 500,
message: 'The server encountered an unexpected condition which prevented it from fulfilling the request.',
errStack: err,
user: req.body.email,
});
throw error;
}
});
res.status(200).send({
message: 'Subscribed Successfully',
response: response,
Expand Down
16 changes: 15 additions & 1 deletion backend/utility/emailTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,18 @@ module.exports.resetPasswordMailTemplate = (data) => {
Please ignore this mail If you not request the service
`;
return emailContent;
};
};

module.exports.welcomeSubscriberMailTemplate=()=>{
const emailContent=`
<h2>Hello There, 👋</h2><br/>
Welcome to HITK Tech Community! 💐
<br/><br/>
Thanks for subscribing to our newsletter and joining our community.We're so happy to have you.🤩
<br/><br/>
Bye for now!
<br/>
The HITK Tech Community Team
`
return emailContent
}
Loading