Add replyto option

This commit is contained in:
KMKoushik
2024-05-16 07:18:44 +10:00
parent 2b77ef3413
commit 3df022f6b2
8 changed files with 16 additions and 3 deletions

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Email" ADD COLUMN "replyTo" TEXT;

View File

@@ -157,6 +157,7 @@ model Email {
to String to String
from String from String
subject String subject String
replyTo String?
text String? text String?
html String? html String?
latestStatus EmailStatus @default(QUEUED) latestStatus EmailStatus @default(QUEUED)

View File

@@ -108,6 +108,7 @@ export async function sendEmailThroughSes({
subject, subject,
text, text,
html, html,
replyTo,
region = "us-east-1", region = "us-east-1",
configurationSetName, configurationSetName,
}: EmailContent & { }: EmailContent & {
@@ -117,6 +118,7 @@ export async function sendEmailThroughSes({
const sesClient = getSesClient(region); const sesClient = getSesClient(region);
const command = new SendEmailCommand({ const command = new SendEmailCommand({
FromEmailAddress: from, FromEmailAddress: from,
ReplyToAddresses: replyTo ? [replyTo] : undefined,
Destination: { Destination: {
ToAddresses: [to], ToAddresses: [to],
}, },
@@ -155,10 +157,12 @@ export async function sendEmailThroughSes({
} }
} }
// Need to improve this. Use some kinda library to do this
export async function sendEmailWithAttachments({ export async function sendEmailWithAttachments({
to, to,
from, from,
subject, subject,
replyTo,
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
text, text,
html, html,
@@ -174,13 +178,13 @@ export async function sendEmailWithAttachments({
const boundary = "NextPart"; const boundary = "NextPart";
let rawEmail = `From: ${from}\n`; let rawEmail = `From: ${from}\n`;
rawEmail += `To: ${to}\n`; rawEmail += `To: ${to}\n`;
rawEmail += `Reply-To: ${replyTo}\n`;
rawEmail += `Subject: ${subject}\n`; rawEmail += `Subject: ${subject}\n`;
rawEmail += `MIME-Version: 1.0\n`; rawEmail += `MIME-Version: 1.0\n`;
rawEmail += `Content-Type: multipart/mixed; boundary="${boundary}"\n\n`; rawEmail += `Content-Type: multipart/mixed; boundary="${boundary}"\n\n`;
rawEmail += `--${boundary}\n`; rawEmail += `--${boundary}\n`;
rawEmail += `Content-Type: text/html; charset="UTF-8"\n\n`; rawEmail += `Content-Type: text/html; charset="UTF-8"\n\n`;
rawEmail += `${html}\n\n`; rawEmail += `${html}\n\n`;
for (const attachment of attachments) { for (const attachment of attachments) {
const content = attachment.content; // Convert buffer to base64 const content = attachment.content; // Convert buffer to base64
const mimeType = const mimeType =

View File

@@ -15,6 +15,7 @@ const route = createRoute({
to: z.string().email(), to: z.string().email(),
from: z.string().email(), from: z.string().email(),
subject: z.string(), subject: z.string(),
replyTo: z.string().optional(),
text: z.string().optional(), text: z.string().optional(),
html: z.string().optional(), html: z.string().optional(),
attachments: z attachments: z

View File

@@ -1,6 +1,7 @@
import { OpenAPIHono } from "@hono/zod-openapi"; import { OpenAPIHono } from "@hono/zod-openapi";
import { swaggerUI } from "@hono/swagger-ui"; import { swaggerUI } from "@hono/swagger-ui";
import { handleError } from "./api-error"; import { handleError } from "./api-error";
import { env } from "~/env";
export function getApp() { export function getApp() {
const app = new OpenAPIHono().basePath("/api"); const app = new OpenAPIHono().basePath("/api");
@@ -14,7 +15,7 @@ export function getApp() {
version: "1.0.0", version: "1.0.0",
title: "Unsend API", title: "Unsend API",
}, },
servers: [{ url: `${new URL(c.req.url).origin}/api` }], servers: [{ url: `${env.APP_URL}/api` }],
})); }));
app.openAPIRegistry.registerComponent("securitySchemes", "Bearer", { app.openAPIRegistry.registerComponent("securitySchemes", "Bearer", {

View File

@@ -6,7 +6,8 @@ import { queueEmail } from "./job-service";
export async function sendEmail( export async function sendEmail(
emailContent: EmailContent & { teamId: number } emailContent: EmailContent & { teamId: number }
) { ) {
const { to, from, subject, text, html, teamId, attachments } = emailContent; const { to, from, subject, text, html, teamId, attachments, replyTo } =
emailContent;
const fromDomain = from.split("@")[1]; const fromDomain = from.split("@")[1];
@@ -34,6 +35,7 @@ export async function sendEmail(
to, to,
from, from,
subject, subject,
replyTo,
text, text,
html, html,
teamId, teamId,

View File

@@ -81,6 +81,7 @@ async function executeEmail(
to: email.to, to: email.to,
from: email.from, from: email.from,
subject: email.subject, subject: email.subject,
replyTo: email.replyTo ?? undefined,
text: email.text ?? undefined, text: email.text ?? undefined,
html: email.html ?? undefined, html: email.html ?? undefined,
region: domain?.region ?? env.AWS_DEFAULT_REGION, region: domain?.region ?? env.AWS_DEFAULT_REGION,

View File

@@ -4,6 +4,7 @@ export type EmailContent = {
subject: string; subject: string;
text?: string; text?: string;
html?: string; html?: string;
replyTo?: string;
attachments?: Array<EmailAttachment>; attachments?: Array<EmailAttachment>;
}; };