@@ -18,6 +18,7 @@ import {
1818} from "../queue/queue-constants" ;
1919import { logger } from "../logger/log" ;
2020import { createWorkerHandler , TeamJob } from "../queue/bullmq-context" ;
21+ import { SuppressionService } from "./suppression-service" ;
2122
2223export async function sendCampaign ( id : string ) {
2324 let campaign = await db . campaign . findUnique ( {
@@ -253,6 +254,35 @@ async function processContactEmail(jobData: CampaignEmailJob) {
253254
254255 const unsubscribeUrl = createUnsubUrl ( contact . id , emailConfig . campaignId ) ;
255256
257+ // Check for suppressed emails before processing
258+ const toEmails = [ contact . email ] ;
259+ const ccEmails = emailConfig . cc || [ ] ;
260+ const bccEmails = emailConfig . bcc || [ ] ;
261+
262+ // Collect all unique emails to check for suppressions
263+ const allEmailsToCheck = [
264+ ...new Set ( [ ...toEmails , ...ccEmails , ...bccEmails ] ) ,
265+ ] ;
266+
267+ const suppressionResults = await SuppressionService . checkMultipleEmails (
268+ allEmailsToCheck ,
269+ emailConfig . teamId
270+ ) ;
271+
272+ // Filter each field separately
273+ const filteredToEmails = toEmails . filter (
274+ ( email ) => ! suppressionResults [ email ]
275+ ) ;
276+ const filteredCcEmails = ccEmails . filter (
277+ ( email ) => ! suppressionResults [ email ]
278+ ) ;
279+ const filteredBccEmails = bccEmails . filter (
280+ ( email ) => ! suppressionResults [ email ]
281+ ) ;
282+
283+ // Check if the contact's email (TO recipient) is suppressed
284+ const isContactSuppressed = filteredToEmails . length === 0 ;
285+
256286 const html = await renderer . render ( {
257287 shouldReplaceVariableValues : true ,
258288 variableValues : {
@@ -265,13 +295,80 @@ async function processContactEmail(jobData: CampaignEmailJob) {
265295 } ,
266296 } ) ;
267297
268- // Create single email
298+ if ( isContactSuppressed ) {
299+ // Create suppressed email record
300+ logger . info (
301+ {
302+ contactEmail : contact . email ,
303+ campaignId : emailConfig . campaignId ,
304+ teamId : emailConfig . teamId ,
305+ } ,
306+ "Contact email is suppressed. Creating suppressed email record."
307+ ) ;
308+
309+ const email = await db . email . create ( {
310+ data : {
311+ to : toEmails ,
312+ replyTo : emailConfig . replyTo ,
313+ cc : ccEmails . length > 0 ? ccEmails : undefined ,
314+ bcc : bccEmails . length > 0 ? bccEmails : undefined ,
315+ from : emailConfig . from ,
316+ subject : emailConfig . subject ,
317+ html,
318+ text : emailConfig . previewText ,
319+ teamId : emailConfig . teamId ,
320+ campaignId : emailConfig . campaignId ,
321+ contactId : contact . id ,
322+ domainId : emailConfig . domainId ,
323+ latestStatus : "SUPPRESSED" ,
324+ } ,
325+ } ) ;
326+
327+ await db . emailEvent . create ( {
328+ data : {
329+ emailId : email . id ,
330+ status : "SUPPRESSED" ,
331+ data : {
332+ error : "Contact email is suppressed. No email sent." ,
333+ } ,
334+ } ,
335+ } ) ;
336+
337+ return ;
338+ }
339+
340+ // Log if any CC/BCC emails were filtered out
341+ if ( ccEmails . length > filteredCcEmails . length ) {
342+ logger . info (
343+ {
344+ originalCc : ccEmails ,
345+ filteredCc : filteredCcEmails ,
346+ campaignId : emailConfig . campaignId ,
347+ teamId : emailConfig . teamId ,
348+ } ,
349+ "Some CC recipients were suppressed and filtered out from campaign email."
350+ ) ;
351+ }
352+
353+ if ( bccEmails . length > filteredBccEmails . length ) {
354+ logger . info (
355+ {
356+ originalBcc : bccEmails ,
357+ filteredBcc : filteredBccEmails ,
358+ campaignId : emailConfig . campaignId ,
359+ teamId : emailConfig . teamId ,
360+ } ,
361+ "Some BCC recipients were suppressed and filtered out from campaign email."
362+ ) ;
363+ }
364+
365+ // Create email with filtered recipients
269366 const email = await db . email . create ( {
270367 data : {
271- to : [ contact . email ] ,
368+ to : filteredToEmails ,
272369 replyTo : emailConfig . replyTo ,
273- cc : emailConfig . cc ,
274- bcc : emailConfig . bcc ,
370+ cc : filteredCcEmails . length > 0 ? filteredCcEmails : undefined ,
371+ bcc : filteredBccEmails . length > 0 ? filteredBccEmails : undefined ,
275372 from : emailConfig . from ,
276373 subject : emailConfig . subject ,
277374 html,
0 commit comments