diff --git a/backend/app/routes/subscribers/postSubscriber.js b/backend/app/routes/subscribers/postSubscriber.js index 4860dae1..8ad7c279 100644 --- a/backend/app/routes/subscribers/postSubscriber.js +++ b/backend/app/routes/subscribers/postSubscriber.js @@ -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, + 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) { @@ -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, diff --git a/backend/utility/emailTemplates.js b/backend/utility/emailTemplates.js index 975ad999..118e8382 100644 --- a/backend/utility/emailTemplates.js +++ b/backend/utility/emailTemplates.js @@ -105,4 +105,18 @@ module.exports.resetPasswordMailTemplate = (data) => { Please ignore this mail If you not request the service `; return emailContent; -}; \ No newline at end of file +}; + +module.exports.welcomeSubscriberMailTemplate=()=>{ + const emailContent=` +

Hello There, 👋


+ Welcome to HITK Tech Community! 💐 +

+ Thanks for subscribing to our newsletter and joining our community.We're so happy to have you.🤩 +

+ Bye for now! +
+ The HITK Tech Community Team + ` + return emailContent +} \ No newline at end of file