Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/docs/self-hosting/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ Add the following environment variables.

</Step>
<Step title="Email content retention (optional)">
By default, stored email content stays in the database forever. To bound database growth, set `EMAIL_CLEANUP_DAYS` to a positive number of days. A daily job (midnight UTC) then clears the `text`, `html`, `attachments` and `headers` values for emails older than the cutoff. Delivery status and event history are kept.
By default, stored email content stays in the database forever. To bound database growth, set `EMAIL_CLEANUP_DAYS` to a positive number of days. A daily job (midnight UTC) then clears the `text`, `html`, `attachments` and `headers` values of emails sent more than that many days ago. Delivery status and event history are kept, and the dashboard explains that the content was removed. Scheduled and queued emails keep their content until they're sent.

The job clears up to 100,000 emails a night in small batches. If you turn it on for an existing database, older emails are cleared over several nights. Autovacuum then makes the freed space reusable, which slows how fast the database grows; it usually doesn't shrink the files on disk.

```env
EMAIL_CLEANUP_DAYS=30
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Encourage autovacuum to visit "Email" sooner: after ~2% of its rows change
-- instead of the default 20%. The email content cleanup rewrites up to 100k
-- rows a night; this makes that space reusable sooner instead of the table
-- growing. It only changes a table setting and touches no data.
--
-- The ALTER needs a SHARE UPDATE EXCLUSIVE lock: normal reads and writes carry
-- on, but it waits for a running VACUUM, ANALYZE or CREATE INDEX CONCURRENTLY
-- on "Email". lock_timeout makes it give up after 10s instead of waiting
-- indefinitely during a deploy; the migration then fails and can be retried.
BEGIN;
SET LOCAL lock_timeout = '10s';
ALTER TABLE "Email" SET (autovacuum_vacuum_scale_factor = 0.02, autovacuum_analyze_scale_factor = 0.02);
COMMIT;
26 changes: 20 additions & 6 deletions apps/web/src/app/(dashboard)/emails/email-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export default function EmailDetails({ emailId }: { emailId: string }) {
<EmailPreview
html={emailQuery.data?.html}
text={emailQuery.data?.text}
createdAt={emailQuery.data?.createdAt}
contentRetentionDays={emailQuery.data?.contentRetentionDays}
/>
</motion.div>
</div>
Expand Down Expand Up @@ -138,12 +140,25 @@ export default function EmailDetails({ emailId }: { emailId: string }) {
);
}

function isPastRetention(
createdAt: Date | string | null | undefined,
days: number | null | undefined,
) {
if (!createdAt || !days) return false;
const ageMs = Date.now() - new Date(createdAt).getTime();
return ageMs >= days * 24 * 60 * 60 * 1000;
}

const EmailPreview = ({
html,
text,
createdAt,
contentRetentionDays,
}: {
html: string | null | undefined;
text: string | null | undefined;
createdAt?: Date | string | null;
contentRetentionDays?: number | null;
}) => {
const [show, setShow] = useState(false);

Expand All @@ -166,8 +181,10 @@ const EmailPreview = ({
if (!srcDoc) {
return (
<div className="dark:bg-slate-200 h-[350px] overflow-visible rounded border-t flex items-center justify-center">
<span className="text-sm text-muted-foreground dark:text-slate-500">
No content to preview
<span className="text-sm text-muted-foreground dark:text-slate-500 px-6 text-center">
{isPastRetention(createdAt, contentRetentionDays)
? `Email content is removed ${contentRetentionDays} days after sending. Its status and events are kept.`
: "No content to preview"}
</span>
</div>
);
Expand Down Expand Up @@ -306,10 +323,7 @@ const getErrorMessage = (data: SesBounce) => {
if (data.bounceType === "Permanent") {
return BOUNCE_ERROR_MESSAGES[data.bounceType][
data.bounceSubType as
| "General"
| "NoEmail"
| "Suppressed"
| "OnAccountSuppressionList"
"General" | "NoEmail" | "Suppressed" | "OnAccountSuppressionList"
];
} else if (data.bounceType === "Transient") {
return BOUNCE_ERROR_MESSAGES[data.bounceType][
Expand Down
6 changes: 5 additions & 1 deletion apps/web/src/server/api/routers/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
teamProcedure,
} from "~/server/api/trpc";
import { db } from "~/server/db";
import { env } from "~/env";
import { cancelEmail, updateEmail } from "~/server/service/email-service";

const statuses = Object.values(EmailStatus) as [EmailStatus];
Expand Down Expand Up @@ -246,7 +247,10 @@ export const emailRouter = createTRPCRouter({
},
});

return email;
if (!email) return email;

// Email content (html, text) is cleared after this many days, when set.
return { ...email, contentRetentionDays: env.EMAIL_CLEANUP_DAYS ?? null };
}),

cancelEmail: emailProcedure.mutation(async ({ input }) => {
Expand Down
Loading
Loading