Skip to content

Commit

Permalink
fix: email bcc bug
Browse files Browse the repository at this point in the history
  • Loading branch information
KishenKumarrrrr committed Sep 13, 2023
1 parent b8bab32 commit 153323c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/src/email/services/email-transactional.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ async function handleStatusCallbacks(
}
break
case SesEventType.Open:
// Cannot check that open applies to the main recipient
await EmailMessageTransactional.update(
{
status: TransactionalEmailMessageStatus.Opened,
Expand All @@ -202,6 +203,7 @@ async function handleStatusCallbacks(
)
break
case SesEventType.Send:
// Cannot check that send applies to the main recipient
await EmailMessageTransactional.update(
{
status: TransactionalEmailMessageStatus.Sent,
Expand Down
36 changes: 36 additions & 0 deletions backend/src/email/utils/callback/parsers/ses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ const parseNotificationAndEvent = async (
message: any,
metadata: Metadata
): Promise<void> => {
if (!isNotificationAndEventForMainRecipient(message, type)) {
logger.info({
message: 'SES notification or event is not for the main recipient',
action: 'filterNotification',
body: message,
})
return
}
switch (type) {
case SesEventType.Delivery:
await updateDeliveredStatus(metadata)
Expand Down Expand Up @@ -250,4 +258,32 @@ const parseRecord = async (record: SesRecord): Promise<void> => {
}
}

// Checks whether the notification/event is meant for the main recipient of the email.
function isNotificationAndEventForMainRecipient(
message: any,
type: SesEventType
): boolean {
// We cannot filter "OPEN" and "SEND" events due to the response given by AWS SES
if (type === SesEventType.Open || type === SesEventType.Send) {
return true
}
const mainRecipient: string = message?.mail?.commonHeaders?.to[0]
if (!mainRecipient) {
throw new Error('Failed to find main recipient in message')
}
const mainRecipientDelivered = message?.delivery?.recipients?.some(
(e: string) => e === mainRecipient
)
const mainRecipientBounced = message.bounce?.bouncedRecipients?.some(
(e: any) => e.emailAddress === mainRecipient
)
const mainRecipientComplained = message.complaint?.complainedRecipients?.some(
(e: any) => e.emailAddress === mainRecipient
)

return (
mainRecipientBounced || mainRecipientDelivered || mainRecipientComplained
)
}

export { HttpEvent, SesRecord, isEvent, parseRecord, validateSignature }

0 comments on commit 153323c

Please sign in to comment.