From d9d165d80d4251c4579d1943ec46aebb767bc2c4 Mon Sep 17 00:00:00 2001 From: Carlos Gomez Date: Wed, 18 Dec 2024 06:27:37 -0400 Subject: [PATCH 1/2] fix: redo emailSender.js and emailController.js --- src/controllers/emailController.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/controllers/emailController.js b/src/controllers/emailController.js index 13de952e3..b8149edba 100644 --- a/src/controllers/emailController.js +++ b/src/controllers/emailController.js @@ -12,17 +12,13 @@ const sendEmail = async (req, res) => { const { to, subject, html } = req.body; await emailSender(to, subject, html) - .then(result => { - console.log('Email sent successfully:', result); + .then(() => { res.status(200).send(`Email sent successfully to ${to}`); }) - .catch(error => { - console.error('Error sending email:', error); + .catch(() => { res.status(500).send('Error sending email'); }); - } catch (error) { - console.error('Error sending email:', error); return res.status(500).send('Error sending email'); } }; @@ -55,7 +51,7 @@ const sendEmailToAll = async (req, res) => { const emailList = await EmailSubcriptionList.find({ email: { $ne: null } }); emailList.forEach((emailObject) => { const { email } = emailObject; - const emailContent = ` + const subscriptionEmailContent = ` @@ -68,7 +64,7 @@ const sendEmailToAll = async (req, res) => {

If you would like to unsubscribe, please click here

`; - emailSender(email, subject, emailContent); + emailSender(email, subject, subscriptionEmailContent); }); return res.status(200).send('Email sent successfully'); } catch (error) { @@ -107,13 +103,9 @@ const addNonHgnEmailSubscription = async (req, res) => { } const payload = { email }; - const token = jwt.sign( - payload, - jwtSecret, - { - expiresIn: 360, - }, - ); + const token = jwt.sign(payload, jwtSecret, { + expiresIn: 360, + }); const emailContent = ` From 2f84535bcc7f745a11c7c895b0033d6e86adeefb Mon Sep 17 00:00:00 2001 From: Carlos Gomez Date: Wed, 18 Dec 2024 13:22:46 -0400 Subject: [PATCH 2/2] feat: emailSender.js emailcontroller.js --- src/controllers/emailController.js | 8 ------ src/utilities/emailSender.js | 41 +++++++++++++++++------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/controllers/emailController.js b/src/controllers/emailController.js index 7eb79aaf4..140bdd440 100644 --- a/src/controllers/emailController.js +++ b/src/controllers/emailController.js @@ -70,19 +70,11 @@ const sendEmail = async (req, res) => { if (!subject) missingFields.push('Subject'); if (!html) missingFields.push('HTML content'); if (!to) missingFields.push('Recipient email'); - console.log('missingFields', missingFields); return res .status(400) .send(`${missingFields.join(' and ')} ${missingFields.length > 1 ? 'are' : 'is'} required`); } - // Extract images and create attachments - const { html: processedHtml, attachments } = extractImagesAndCreateAttachments(html); - - // Log recipient for debugging - console.log('Recipient:', to); - - await emailSender(to, subject, html) .then(() => { res.status(200).send(`Email sent successfully to ${to}`); diff --git a/src/utilities/emailSender.js b/src/utilities/emailSender.js index b0fb40112..610ff1459 100644 --- a/src/utilities/emailSender.js +++ b/src/utilities/emailSender.js @@ -34,6 +34,7 @@ const transporter = nodemailer.createTransport({ const sendEmail = async (mailOptions) => { try { const { token } = await OAuth2Client.getAccessToken(); + mailOptions.auth = { user: config.email, refreshToken: config.refreshToken, @@ -45,6 +46,7 @@ const sendEmail = async (mailOptions) => { } return result; } catch (error) { + console.error('Error sending email:', error); logger.logException(error, `Error sending email: ${mailOptions.to}`); throw error; } @@ -57,7 +59,6 @@ const processQueue = async () => { if (isProcessing || queue.length === 0) return; isProcessing = true; - console.log('Processing email queue...'); const processBatch = async () => { if (queue.length === 0) { @@ -67,12 +68,10 @@ const processQueue = async () => { const batch = queue.shift(); try { - console.log('Sending email...'); await sendEmail(batch); } catch (error) { logger.logException(error, 'Failed to send email batch'); } - setTimeout(processBatch, config.rateLimitDelay); }; @@ -94,21 +93,29 @@ const emailSender = ( replyTo = null, ) => { if (!process.env.sendEmail) return; - const recipientsArray = Array.isArray(recipients) ? recipients : [recipients]; - for (let i = 0; i < recipients.length; i += config.batchSize) { - const batchRecipients = recipientsArray.slice(i, i + config.batchSize); - queue.push({ - from: config.email, - bcc: batchRecipients.join(','), - subject, - html: message, - attachments, - cc, - replyTo, + return new Promise((resolve, reject) => { + const recipientsArray = Array.isArray(recipients) ? recipients : [recipients]; + for (let i = 0; i < recipients.length; i += config.batchSize) { + const batchRecipients = recipientsArray.slice(i, i + config.batchSize); + queue.push({ + from: config.email, + bcc: batchRecipients.join(','), + subject, + html: message, + attachments, + cc, + replyTo, + }); + } + setImmediate(async () => { + try { + await processQueue(); + resolve('Emails processed successfully'); + } catch (error) { + reject(error); + } }); - } - console.log('Emails queued:', queue.length); - setImmediate(processQueue); + }); }; module.exports = emailSender;