Providers
Cloudflare
Send email through Cloudflare Email Service.
import { createClient } from '@nicogenz/emails-sdk';
import { cloudflare } from '@nicogenz/emails-sdk/cloudflare';
const email = createClient({
provider: cloudflare({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!,
}),
});
Options
| Option | Type | Required | Description |
|---|---|---|---|
accountId | string | Yes | Your Cloudflare account ID. |
apiToken | string | Yes | A Cloudflare API token with permission to send email. |
fetch | typeof fetch | No | A custom fetch implementation. |
Workers binding
Inside a Cloudflare Worker, you can send through a send_email binding instead of the REST API. You don't need an account ID or API token then.
wrangler.jsonc
{
"send_email": [{ "name": "EMAIL" }],
}
import { createClient } from '@nicogenz/emails-sdk';
import { cloudflare } from '@nicogenz/emails-sdk/cloudflare';
export default {
async fetch(request, env) {
const email = createClient({
provider: cloudflare({ binding: env.EMAIL }),
});
await email.send({
from: 'Acme <[email protected]>',
to: '[email protected]',
subject: 'Welcome to Acme',
html: '<p>Thanks for signing up!</p>',
});
return new Response('Sent');
},
};
| Option | Type | Required | Description |
|---|---|---|---|
binding | CloudflareEmailBinding | Yes | The send_email binding from your Worker's env. |
Notes
- The REST API reports which recipients were delivered, queued, bounced or suppressed.
sendsucceeds as long as Cloudflare accepts the email, and these lists are inraw. - Errors thrown by the binding become an
EmailError: rate and daily limits map torate_limited, delivery and internal errors toprovider_error, and all other errors tovalidation. The original error is thecause. tagsandidempotencyKeyaren't supported. Passing either throws anEmailErrorwith the codeunsupported.- Cloudflare allows only one
replyToaddress. Passing more throws anEmailErrorwith the codeunsupported. headersmust start withX-or be on Cloudflare's header allowlist.- An email can have at most 50 recipients across
to,ccandbcc, and can be at most 5 MiB including attachments. sendBatchsends the emails one by one, because Cloudflare has no batch endpoint.