@@ -2,10 +2,15 @@ import { EmailContent } from "~/types";
22import { db } from "../db" ;
33import { UnsendApiError } from "~/server/public-api/api-error" ;
44import { EmailQueueService } from "./email-queue-service" ;
5- import { validateDomainFromEmail , validateApiKeyDomainAccess } from "./domain-service" ;
5+ import {
6+ validateDomainFromEmail ,
7+ validateApiKeyDomainAccess ,
8+ } from "./domain-service" ;
69import { EmailRenderer } from "@usesend/email-editor/src/renderer" ;
710import { logger } from "../logger/log" ;
811import { SuppressionService } from "./suppression-service" ;
12+ import { sanitizeCustomHeaders } from "~/server/utils/email-headers" ;
13+ import { Prisma } from "@prisma/client" ;
914
1015async function checkIfValidEmail ( emailId : string ) {
1116 const email = await db . email . findUnique ( {
@@ -66,26 +71,27 @@ export async function sendEmail(
6671 scheduledAt,
6772 apiKeyId,
6873 inReplyToId,
74+ headers,
6975 } = emailContent ;
7076 let subject = subjectFromApiCall ;
7177 let html = htmlFromApiCall ;
7278
7379 let domain : Awaited < ReturnType < typeof validateDomainFromEmail > > ;
74-
80+
7581 // If this is an API call with an API key, validate domain access
7682 if ( apiKeyId ) {
7783 const apiKey = await db . apiKey . findUnique ( {
7884 where : { id : apiKeyId } ,
7985 include : { domain : true } ,
8086 } ) ;
81-
87+
8288 if ( ! apiKey ) {
8389 throw new UnsendApiError ( {
8490 code : "BAD_REQUEST" ,
8591 message : "Invalid API key" ,
8692 } ) ;
8793 }
88-
94+
8995 domain = await validateApiKeyDomainAccess ( from , teamId , apiKey ) ;
9096 } else {
9197 // For non-API calls (dashboard, etc.), use regular domain validation
@@ -261,6 +267,7 @@ export async function sendEmail(
261267 latestStatus : scheduledAtDate ? "SCHEDULED" : "QUEUED" ,
262268 apiId : apiKeyId ,
263269 inReplyToId,
270+ headers : headers ? JSON . stringify ( headers ) : undefined ,
264271 } ,
265272 } ) ;
266273
@@ -556,6 +563,9 @@ export async function sendBulkEmails(
556563 latestStatus : "SUPPRESSED" ,
557564 apiId : apiKeyId ,
558565 inReplyToId,
566+ headers : originalContent . headers
567+ ? JSON . stringify ( originalContent . headers )
568+ : undefined ,
559569 } ,
560570 } ) ;
561571
@@ -628,6 +638,7 @@ export async function sendBulkEmails(
628638 bcc,
629639 scheduledAt,
630640 apiKeyId,
641+ headers,
631642 } = content ;
632643
633644 // Find the original index for this email
@@ -691,7 +702,6 @@ export async function sendBulkEmails(
691702 : undefined ;
692703
693704 try {
694- // Create email record
695705 const email = await db . email . create ( {
696706 data : {
697707 to : Array . isArray ( to ) ? to : [ to ] ,
@@ -712,6 +722,7 @@ export async function sendBulkEmails(
712722 scheduledAt : scheduledAtDate ,
713723 latestStatus : scheduledAtDate ? "SCHEDULED" : "QUEUED" ,
714724 apiId : apiKeyId ,
725+ headers : headers ? JSON . stringify ( headers ) : undefined ,
715726 } ,
716727 } ) ;
717728
0 commit comments