feat: add In-Reply-To option (#165)

This commit is contained in:
KM Koushik
2025-05-25 20:44:13 +10:00
committed by GitHub
parent ff32ff9302
commit 15e5327024
14 changed files with 754 additions and 592 deletions
@@ -4,7 +4,7 @@ import { EmailAttachment } from "~/types";
import { convert as htmlToText } from "html-to-text";
import { getConfigurationSetName } from "~/utils/ses-utils";
import { db } from "../db";
import { sendEmailThroughSes, sendEmailWithAttachments } from "../aws/ses";
import { sendRawEmail } from "../aws/ses";
import { getRedis } from "../redis";
import { DEFAULT_QUEUE_OPTIONS } from "../queue/queue-constants";
import { Prisma } from "@prisma/client";
@@ -331,34 +331,37 @@ async function executeEmail(
? htmlToText(email.html)
: undefined;
let inReplyToMessageId: string | undefined = undefined;
if (email.inReplyToId) {
const replyEmail = await db.email.findUnique({
where: {
id: email.inReplyToId,
},
});
if (replyEmail && replyEmail.sesEmailId) {
inReplyToMessageId = replyEmail.sesEmailId;
}
}
try {
const messageId = attachments.length
? await sendEmailWithAttachments({
to: email.to,
from: email.from,
subject: email.subject,
replyTo: email.replyTo ?? undefined,
bcc: email.bcc,
cc: email.cc,
text,
html: email.html ?? undefined,
region: domain?.region ?? env.AWS_DEFAULT_REGION,
configurationSetName,
attachments,
})
: await sendEmailThroughSes({
to: email.to,
from: email.from,
subject: email.subject,
replyTo: email.replyTo ?? undefined,
text,
html: email.html ?? undefined,
region: domain?.region ?? env.AWS_DEFAULT_REGION,
configurationSetName,
attachments,
unsubUrl,
isBulk,
});
const messageId = await sendRawEmail({
to: email.to,
from: email.from,
subject: email.subject,
replyTo: email.replyTo ?? undefined,
bcc: email.bcc,
cc: email.cc,
text,
html: email.html ?? undefined,
region: domain?.region ?? env.AWS_DEFAULT_REGION,
configurationSetName,
attachments: attachments.length > 0 ? attachments : undefined,
unsubUrl,
isBulk,
inReplyToMessageId,
});
// Delete attachments after sending the email
await db.email.update({
@@ -63,6 +63,7 @@ export async function sendEmail(
bcc,
scheduledAt,
apiKeyId,
inReplyToId,
} = emailContent;
let subject = subjectFromApiCall;
let html = htmlFromApiCall;
@@ -99,6 +100,22 @@ export async function sendEmail(
}
}
if (inReplyToId) {
const email = await db.email.findUnique({
where: {
id: inReplyToId,
teamId,
},
});
if (!email) {
throw new UnsendApiError({
code: "BAD_REQUEST",
message: '"inReplyTo" is invalid',
});
}
}
if (!text && !html) {
throw new UnsendApiError({
code: "BAD_REQUEST",
@@ -131,6 +148,7 @@ export async function sendEmail(
scheduledAt: scheduledAtDate,
latestStatus: scheduledAtDate ? "SCHEDULED" : "QUEUED",
apiId: apiKeyId,
inReplyToId,
},
});