Skip to content

Commit

Permalink
Merge pull request #15 from beabee-communityrm/fix/sendgrid-send-limit
Browse files Browse the repository at this point in the history
fix: allow SendGrid to send more than 1,000 emails
  • Loading branch information
wpf500 authored Jul 16, 2024
2 parents de245ec + df11486 commit c1f4663
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions apps/backend/src/core/providers/email/SendGridProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,36 @@ export default class SendGridProvider extends BaseProvider {
recipients: EmailRecipient[],
opts?: EmailOptions
): Promise<void> {
const resp = await sgMail.sendMultiple({
from: {
email: email.fromEmail,
name: email.fromName
},
subject: email.subject,
html: email.body,
personalizations: recipients.map((recipient) => ({
to: recipient.to,
...(recipient.mergeFields && { substitutions: recipient.mergeFields })
})),
...(opts?.sendAt && {
sendAt: +opts.sendAt
}),
...(opts?.attachments && {
attachments: opts.attachments.map((attachment) => ({
filename: attachment.name,
type: attachment.type,
content: attachment.content
}))
}),
mailSettings: {
sandboxMode: {
enable: this.testMode
for (let i = 0; i < recipients.length; i += 1000) {
const resp = await sgMail.sendMultiple({
from: {
email: email.fromEmail,
name: email.fromName
},
subject: email.subject,
html: email.body,
personalizations: recipients.slice(i, i + 1000).map((recipient) => ({
to: recipient.to,
...(recipient.mergeFields && { substitutions: recipient.mergeFields })
})),
...(opts?.sendAt && {
sendAt: +opts.sendAt
}),
...(opts?.attachments && {
attachments: opts.attachments.map((attachment) => ({
filename: attachment.name,
type: attachment.type,
content: attachment.content
}))
}),
mailSettings: {
sandboxMode: {
enable: this.testMode
}
}
}
});
});

log.info("Sent email", { resp });
log.info(`Sent email to recipients ${i} to ${i + 1000}`, { resp });
}
}
}

0 comments on commit c1f4663

Please sign in to comment.