Send emails
Send your first email using the REST API or Workers binding.
Send emails from your applications using Cloudflare Email Service. You can use the REST API from any platform or the Workers binding for applications built on Cloudflare Workers.
- Sign up for a Cloudflare account ↗.
- Install
Node.js↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0 or later.
Before using Email Sending, you need to configure your domain.
-
In the Cloudflare dashboard, go to Email Sending.
Go to Email Sending -
Select Onboard Domain.
-
Choose a domain from your Cloudflare account.
-
Select Continue to proceed with DNS configuration.
-
Select Add records and onboard. This will add the following DNS records on the
cf-bouncesubdomain of your domain:- MX records for bounce handling.
- TXT record for SPF to authorize sending emails.
- TXT record for DKIM to provide authentication for emails sent from your domain.
- TXT record for DMARC on
_dmarc.yourdomain.com.
Once your domain is onboarded, you can start sending emails.
Send an email with a single curl command. Replace <ACCOUNT_ID> with your Cloudflare account ID and <API_TOKEN> with an API token.
curl "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/email/sending/send" \ --header "Authorization: Bearer <API_TOKEN>" \ --header "Content-Type: application/json" \ --data '{ "to": "recipient@example.com", "from": "welcome@yourdomain.com", "subject": "Welcome to our service!", "html": "<h1>Welcome!</h1><p>Thanks for signing up.</p>", "text": "Welcome! Thanks for signing up." }'A successful response includes the delivery status for each recipient:
{ "success": true, "errors": [], "messages": [], "result": { "delivered": ["recipient@example.com"], "permanent_bounces": [], "queued": [] }}For more details, see the REST API reference.
If you are building on Cloudflare Workers, you can use the Workers binding for native email sending. Start by creating a new Worker project.
-
Create a new Worker project:
npm create cloudflare@latest -- email-service-tutorialyarn create cloudflare email-service-tutorialpnpm create cloudflare@latest email-service-tutorialWhen prompted, select "Hello World" Worker as the template.
-
Add the email binding to your Wrangler configuration file:
JSONC {"$schema": "./node_modules/wrangler/config-schema.json","send_email": [{"name": "EMAIL","remote": true}]}TOML [[send_email]]name = "EMAIL"remote = true -
Create your Worker code in
src/index.ts:TypeScript // Configuration - Update these valuesconst YOUR_DOMAIN = "yourdomain.com"; // Replace with your verified domainconst RECIPIENT_EMAIL = "recipient@example.com"; // Replace with your email to receive test emailsexport default {async fetch(request: Request, env: Env): Promise<Response> {// Send a welcome emailconst response = await env.EMAIL.send({to: RECIPIENT_EMAIL,from: `welcome@${YOUR_DOMAIN}`,subject: "Welcome to our service!",html: "<h1>Welcome!</h1><p>Thanks for signing up.</p>",text: "Welcome! Thanks for signing up.",});return new Response(`Email sent: ${response.messageId}`);},} satisfies ExportedHandler<Env>; -
You can use
npx wrangler devto develop your Worker project and send emails. This runs your code locally while connecting to Cloudflare Email Service (using remote bindings).Terminal window npx wrangler dev# ⎔ Starting remote preview...# Total Upload: 24.96 KiB / gzip: 6.17 KiB# [wrangler:info] Ready on http://localhost:8787 -
Deploy your Worker:
Terminal window npm run deploy
After deploying, test that your Worker can send emails:
-
Visit your Worker URL in a browser (shown in the deploy output, for example:
https://email-service-tutorial.<your-subdomain>.workers.dev). -
You should see a response like
Email sent: <message-id>. -
Check the inbox for the email address you specified in
RECIPIENT_EMAIL. If you do not see the email, check your spam folder.
Now that you can send emails, explore advanced features:
- Route incoming emails - Process emails sent to your domain
- API reference - Complete API documentation
- Examples - Real-world implementation patterns