Skip to content

Commit

Permalink
feat: emailSender.js emailcontroller.js
Browse files Browse the repository at this point in the history
  • Loading branch information
cgomezhub committed Dec 18, 2024
1 parent bf570d9 commit 2f84535
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
8 changes: 0 additions & 8 deletions src/controllers/emailController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
41 changes: 24 additions & 17 deletions src/utilities/emailSender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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);
};

Expand All @@ -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;

0 comments on commit 2f84535

Please sign in to comment.