Skip to content
Open
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
3 changes: 2 additions & 1 deletion apps/api/src/controllers/Campaigns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {CampaignService} from '../services/CampaignService.js';
import {DomainService} from '../services/DomainService.js';
import {CatchAsync} from '../utils/asyncHandler.js';
import {parseListSort} from '../utils/listSort.js';
import {assertValidTemplateSyntax} from '../utils/templateValidation.js';
import {assertValidReplyTo, assertValidTemplateSyntax} from '../utils/templateValidation.js';

@Controller('campaigns')
export class Campaigns {
Expand Down Expand Up @@ -180,6 +180,7 @@ export class Campaigns {
if (from) {
await DomainService.verifyEmailDomain(from, auth.projectId);
}
assertValidReplyTo(replyTo);

const campaign = await CampaignService.update(auth.projectId, id!, {
name,
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/controllers/Templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {DomainService} from '../services/DomainService.js';
import {TemplateService} from '../services/TemplateService.js';
import {CatchAsync} from '../utils/asyncHandler.js';
import {parseListSort} from '../utils/listSort.js';
import {assertValidTemplateSyntax} from '../utils/templateValidation.js';
import {assertValidReplyTo, assertValidTemplateSyntax} from '../utils/templateValidation.js';

@Controller('templates')
export class Templates {
Expand Down Expand Up @@ -89,6 +89,7 @@ export class Templates {

// Verify domain ownership and verification
await DomainService.verifyEmailDomain(from, auth.projectId!);
assertValidReplyTo(replyTo);

const template = await TemplateService.create(auth.projectId!, {
name,
Expand Down Expand Up @@ -126,6 +127,7 @@ export class Templates {
if (from) {
await DomainService.verifyEmailDomain(from, auth.projectId!);
}
assertValidReplyTo(replyTo);

const template = await TemplateService.update(auth.projectId!, templateId, {
name,
Expand Down
27 changes: 26 additions & 1 deletion apps/api/src/utils/__tests__/templateValidation.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {describe, expect, it} from 'vitest';

import {HttpException} from '../../exceptions/index.js';
import {assertValidTemplateSyntax} from '../templateValidation.js';
import {assertValidReplyTo, assertValidTemplateSyntax} from '../templateValidation.js';

/**
* Rendering is intentionally forgiving — a broken template still sends, falling back to
Expand Down Expand Up @@ -62,3 +62,28 @@ describe('assertValidTemplateSyntax', () => {
expect(() => assertValidTemplateSyntax({subject: '{% if %}', body: 'fine'})).toThrow(/subject/);
});
});

/**
* Reply-To is free text in the dashboard because it may be on any domain, so this is
* what keeps a typo out of every email the template or campaign sends.
*/
describe('assertValidReplyTo', () => {
it('accepts an address on any domain', () => {
expect(() => assertValidReplyTo('support@other-domain.com')).not.toThrow();
});

it('ignores a Reply-To that is absent or empty', () => {
expect(() => assertValidReplyTo(undefined)).not.toThrow();
expect(() => assertValidReplyTo(null)).not.toThrow();
expect(() => assertValidReplyTo('')).not.toThrow();
});

it('rejects a value that is not an email address', () => {
expect(() => assertValidReplyTo('not-an-email')).toThrow(HttpException);
expect(() => assertValidReplyTo('support@')).toThrow('Reply-To must be a valid email address');
});

it('rejects a non-string value', () => {
expect(() => assertValidReplyTo(42)).toThrow(HttpException);
});
});
20 changes: 20 additions & 0 deletions apps/api/src/utils/templateValidation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {validateTemplate} from '@plunk/shared';
import {z} from 'zod';

import {ErrorCode, HttpException} from '../exceptions/index.js';

Expand Down Expand Up @@ -34,3 +35,22 @@ export function assertValidTemplateSyntax(fields: Record<string, unknown>): void
}
}
}

/**
* Reject a Reply-To that is not an email address.
*
* Unlike `from`, Reply-To may be on any domain, so the dashboard takes it as free
* text. Checking it here keeps a typo out of every email the template or campaign sends.
* Absent or empty values are allowed: they fall back to the from address.
*/
export function assertValidReplyTo(replyTo: unknown): void {
if (replyTo === undefined || replyTo === null || replyTo === '') {
return;
}

if (!z.string().email().safeParse(replyTo).success) {
throw new HttpException(400, 'Reply-To must be a valid email address', ErrorCode.VALIDATION_ERROR, {
field: 'replyTo',
});
}
}
17 changes: 9 additions & 8 deletions apps/web/src/components/EmailSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ export function EmailSettings({
replyToPlaceholder,
layout = 'grid',
}: EmailSettingsProps) {
// Use from email's local part as the reply-to placeholder if not provided
// Extract local part (before @) since the domain is shown in a dropdown
// Fall back to fromPlaceholder when from is empty (since reply-to defaults to from)
const fromLocalPart = from.includes('@') ? from.split('@')[0] : from;
const effectiveReplyToPlaceholder = replyToPlaceholder || fromLocalPart || fromPlaceholder;
// Reply-to defaults to the from address, so show it as the placeholder
const effectiveReplyToPlaceholder = replyToPlaceholder || from || 'Same as From email';
const GridWrapper = layout === 'grid' ? 'div' : 'div';
const gridClassName = layout === 'grid' ? 'grid gap-4 md:grid-cols-2' : 'space-y-4';

Expand Down Expand Up @@ -65,13 +62,17 @@ export function EmailSettings({

<GridWrapper className={layout === 'grid' ? 'grid gap-4 md:grid-cols-2' : ''}>
<div>
<EmailDomainInput
<Label htmlFor="replyTo">Reply-To email</Label>
<Input
id="replyTo"
label="Reply-To email"
type="email"
value={replyTo}
onChange={onReplyToChange}
onChange={e => onReplyToChange(e.target.value)}
placeholder={effectiveReplyToPlaceholder}
/>
<p className="text-xs text-neutral-500 mt-1">
Where replies are sent. Can be on any domain. Defaults to the From email if not set.
</p>
</div>
</GridWrapper>
</div>
Expand Down
Loading