Skip to content

Email for professionals and teams

Update adding domain config

Hieu Nguyen (Jack)a92f65316bf0
diff --git a/docs/api.md b/docs/api.md
index bcf55c9..f3daf82 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -31,7 +31,7 @@ The same operations are available to scripts through admin API keys with the `do
| Mailflare route | Purpose |
| --- | --- |
| `GET /api/v1/domains` | List connected domains with their DNS status |
-| `POST /api/v1/domains` | Connect a domain and configure Cloudflare (`{ hostname, enableRouting?, enableSending?, replaceMxRecords?, receivingProvider? }`; `receivingProvider` defaults to `cloudflare`, and with another value Email Routing is not enabled) |
+| `POST /api/v1/domains` | Connect a domain and configure Cloudflare (`{ hostname, enableRouting?, enableSending?, replaceMxRecords?, receivingProvider?, sendingProvider? }`; `receivingProvider` defaults to `cloudflare`, and with another value Email Routing is not enabled. `sendingProvider` defaults to `cloudflare` (or `none` when `enableSending` is `false`), and the Cloudflare sending subdomain is only created for `cloudflare`) |
| `GET /api/v1/domains/[id]` | Get a connected domain |
| `DELETE /api/v1/domains/[id]` | Remove a domain and clean up its Cloudflare resources |
| `GET /api/v1/domains/[id]/dns` | View its routing, sending and authentication DNS status |
diff --git a/docs/providers.md b/docs/providers.md
index b15f382..bacec68 100644
--- a/docs/providers.md
+++ b/docs/providers.md
@@ -15,7 +15,7 @@ Open **Admin → Domains**, expand a domain, and use the **Setup receiving email
## Choosing at add time
-**New domain** asks which service receives the domain's mail. Cloudflare Email Routing is enabled on the zone only when you pick Cloudflare. For Resend or SES the domain is added without touching Email Routing; finish from the domain page once the credentials are in. The same choice is available over the API as `receivingProvider` (see [API](api.md#domain-management)).
+**New domain** asks which service receives the domain's mail (**Receive mail with**) and which sends it (**Send mail with**), each a dropdown of Cloudflare, Resend, Amazon SES or Not selected. Cloudflare Email Routing and the Cloudflare sending subdomain are only enabled on the zone for the Cloudflare choice. For Resend or SES the domain is added without touching either; finish from the domain page once the credentials are in. The same choices are available over the API as `receivingProvider` and `sendingProvider` (see [API](api.md#domain-management)).
## Resend
diff --git a/src/app/(admin)/domains/page.tsx b/src/app/(admin)/domains/page.tsx
index fbca232..dedcba3 100644
--- a/src/app/(admin)/domains/page.tsx
+++ b/src/app/(admin)/domains/page.tsx
@@ -14,7 +14,6 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
-import { Switch } from "@/components/ui/switch";
import { List } from "@/components/ui/list";
import { CheckCircle2, LoaderCircle, Plus } from "lucide-react";
import { authFetch } from "@/lib/auth/client";
@@ -34,7 +33,7 @@ export default function DomainsPage() {
const managesDns = me?.managesDns ?? true;
const [domainCheck, setDomainCheck] = useState<DomainPreflight | null>(null);
const [domainChecking, setDomainChecking] = useState(false);
- const [enableSending, setEnableSending] = useState(false);
+ const [sendingProvider, setSendingProvider] = useState<SendingProvider>("cloudflare");
const [receivingProvider, setReceivingProvider] = useState<ReceivingProvider>("cloudflare");
const [domainCheckError, setDomainCheckError] = useState<string | null>(null);
const [createOpen, setCreateOpen] = useState(false);
@@ -61,16 +60,13 @@ export default function DomainsPage() {
mutationFn: async () => {
const normalized = hostname.toLowerCase().trim();
let checkedDomain = domainCheck;
- let sendingRequested = enableSending;
if (checkedDomain?.hostname !== normalized) {
const result = await checkDomain(normalized);
if (!result.ok || !result.domain) {
throw new Error(result.error ?? "Domain check failed");
}
checkedDomain = result.domain;
- sendingRequested = true;
setDomainCheck(result.domain);
- setEnableSending(sendingRequested);
}
if (!checkedDomain) throw new Error("Domain check failed");
@@ -80,7 +76,7 @@ export default function DomainsPage() {
body: JSON.stringify({
hostname: checkedDomain.hostname,
enableRouting: true,
- enableSending: sendingRequested,
+ sendingProvider,
receivingProvider,
}),
});
@@ -91,7 +87,7 @@ export default function DomainsPage() {
onSuccess: () => {
setHostname("");
setDomainCheck(null);
- setEnableSending(false);
+ setSendingProvider("cloudflare");
setReceivingProvider("cloudflare");
setDomainCheckError(null);
setCreateOpen(false);
@@ -253,13 +249,11 @@ export default function DomainsPage() {
setDomainChecking(false);
if (!result.ok || !result.domain) {
setDomainCheck(null);
- setEnableSending(false);
setDomainCheckError(result.error ?? "Domain check failed");
return;
}
setDomainCheck(result.domain);
- setEnableSending(true);
};
return (
@@ -298,7 +292,6 @@ export default function DomainsPage() {
setHostname(e.target.value);
if (domainCheck?.hostname !== e.target.value.toLowerCase().trim()) {
setDomainCheck(null);
- setEnableSending(false);
}
}}
onBlur={() => void inspectDomain()}
@@ -330,27 +323,29 @@ export default function DomainsPage() {
</div>
<div className="flex items-center justify-between gap-4 rounded-xl bg-neutral-50 px-4 py-3">
<div>
- <Label htmlFor="enable-sending">Enable sending</Label>
+ <Label htmlFor="sending-provider">Send mail with</Label>
<p className="mt-1 text-xs leading-5 text-neutral-500">
{domainChecking
? "Checking Cloudflare access..."
- : domainCheck
- ? enableSending
- ? "Required to send email."
- : "Receive-only mode."
- : "Enter the domain and leave the field to verify it."}
+ : sendingProvider === "cloudflare"
+ ? "Cloudflare Email Sending is enabled on the zone."
+ : sendingProvider === "none"
+ ? "Receive-only mode. Add a sending service later."
+ : "Add your credentials and set it up from the domain page after adding."}
</p>
</div>
- {domainChecking ? (
- <LoaderCircle className="h-4 w-4 animate-spin text-neutral-500" />
- ) : (
- <Switch
- id="enable-sending"
- checked={enableSending}
- onCheckedChange={setEnableSending}
- disabled={!domainCheck}
- />
- )}
+ {domainChecking && <LoaderCircle className="h-4 w-4 shrink-0 animate-spin text-neutral-500" />}
+ <select
+ id="sending-provider"
+ value={sendingProvider}
+ onChange={(e) => setSendingProvider(e.target.value as SendingProvider)}
+ className="h-9 shrink-0 rounded-md border border-neutral-200 bg-white px-3 text-sm"
+ >
+ <option value="cloudflare">Cloudflare Email Sending</option>
+ <option value="resend">Resend</option>
+ <option value="ses">Amazon SES</option>
+ <option value="none">Not selected</option>
+ </select>
</div>
{domainCheck && (
<div className="flex items-center gap-3 rounded-xl bg-green-50 px-4 py-3 text-sm text-green-700">
diff --git a/src/app/api/domains/route.ts b/src/app/api/domains/route.ts
index 5b6fb4b..4d0e114 100644
--- a/src/app/api/domains/route.ts
+++ b/src/app/api/domains/route.ts
@@ -58,6 +58,7 @@ export async function POST(request: Request) {
enableSending: parsed.data.enableSending,
replaceMxRecords: parsed.data.replaceMxRecords,
receivingProvider: parsed.data.receivingProvider,
+ sendingProvider: parsed.data.sendingProvider,
});
return NextResponse.json(result);
} catch (err) {
diff --git a/src/app/api/v1/domains/route.ts b/src/app/api/v1/domains/route.ts
index c9d1a91..e0f0114 100644
--- a/src/app/api/v1/domains/route.ts
+++ b/src/app/api/v1/domains/route.ts
@@ -52,6 +52,7 @@ export async function POST(request: Request) {
enableSending: parsed.data.enableSending,
replaceMxRecords: parsed.data.replaceMxRecords,
receivingProvider: parsed.data.receivingProvider,
+ sendingProvider: parsed.data.sendingProvider,
});
let dns = result.dns;
try {
diff --git a/src/lib/domains/service.ts b/src/lib/domains/service.ts
index 9c87ea7..e8e305f 100644
--- a/src/lib/domains/service.ts
+++ b/src/lib/domains/service.ts
@@ -40,7 +40,7 @@ export async function addDomainForUser(
env: CloudflareEnv,
userId: string,
hostname: string,
- options?: { enableRouting?: boolean; enableSending?: boolean; replaceMxRecords?: boolean; receivingProvider?: "none" | "cloudflare" | "resend" | "ses" },
+ options?: { enableRouting?: boolean; enableSending?: boolean; replaceMxRecords?: boolean; receivingProvider?: "none" | "cloudflare" | "resend" | "ses"; sendingProvider?: "none" | "cloudflare" | "resend" | "ses" },
): Promise<{
domain: typeof domains.$inferSelect;
dns: DomainDnsView;
@@ -65,8 +65,11 @@ export async function addDomainForUser(
// Email Routing is Cloudflare's way of receiving; another provider brings its own MX,
// which its setup in the domain page creates once credentials exist.
const receivingProvider = options?.receivingProvider ?? "cloudflare";
+ // Cloudflare's sending subdomain is only provisioned when Cloudflare is the sender.
+ const requestedSending = options?.sendingProvider ?? (options?.enableSending === false ? "none" : "cloudflare");
const provisioned = await provisionDomainOnCloudflare(env, hostname, {
...options,
+ enableSending: requestedSending === "cloudflare",
enableRouting: receivingProvider === "cloudflare" ? (options?.enableRouting ?? true) : false,
replaceMxRecords: receivingProvider === "cloudflare" ? options?.replaceMxRecords : false,
});
@@ -90,7 +93,7 @@ export async function addDomainForUser(
routingStatus: provisioned.routingStatus ?? null,
sendingSubdomainTag: provisioned.sendingSubdomainTag,
sendingRequested: provisioned.sendingRequested,
- ...(existing && existing.sendingProvider !== "none" ? {} : { sendingProvider: provisioned.sendingRequested ? ("cloudflare" as const) : ("none" as const) }),
+ ...(options?.sendingProvider ? { sendingProvider: options.sendingProvider } : existing && existing.sendingProvider !== "none" ? {} : { sendingProvider: provisioned.sendingRequested ? ("cloudflare" as const) : ("none" as const) }),
sendingEnabled: provisioned.sendingEnabled,
routingEnabled: provisioned.routingEnabled,
};
diff --git a/src/lib/validators.ts b/src/lib/validators.ts
index 9274bc9..d45c85f 100644
--- a/src/lib/validators.ts
+++ b/src/lib/validators.ts
@@ -72,6 +72,8 @@ export const addDomainSchema = z.object({
enableRouting: z.boolean().optional(),
enableSending: z.boolean().optional(),
replaceMxRecords: z.boolean().optional(),
+ /** Which service sends mail for the domain; derived from `enableSending` when omitted. */
+ sendingProvider: z.enum(["none", "cloudflare", "resend", "ses"]).optional(),
/** Which service receives mail for the domain; Cloudflare Email Routing when omitted. */
receivingProvider: z.enum(["none", "cloudflare", "resend", "ses"]).optional(),
});