← Email Service / email-service / get-started
Send emails
Send emails from your applications using Cloudflare Email Service. You can use the Workers binding for applications built on Cloudflare Workers, the REST API from any platform, or SMTP from any SMTP-capable application or mail client.
Set up your domain
Before using Email Sending, configure your domain.
-
In the Cloudflare dashboard, go to Compute > Email Service > Email Sending.
Go to Email Sending ↗ -
Select Onboard Domain.
-
Choose a domain from your Cloudflare account. Optionally review the DNS records that Cloudflare will add to the
cf-bouncesubdomain of your domain:- MX records to route bounce emails to Cloudflare.
- 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.
-
Select Done.
Once your domain is onboarded, you can start sending emails.
Send your first email
You can send your first email using the Workers binding, the REST API, or SMTP.
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-tutorialWhen prompted, select "Hello World" Worker as the template.
-
Add the email binding to your Wrangler configuration file:
{ "send_email": [ { "name": "EMAIL", "remote": true, }, ], }[[send_email]] name = "EMAIL" remote = true -
Create your Worker code in
src/index.ts:// Configuration - Update these values const YOUR_DOMAIN = "yourdomain.com"; // Replace with your verified domain const RECIPIENT_EMAIL = "[email protected]"; // Replace with your email to receive test emails export default { async fetch(request: Request, env: Env): Promise<Response> { // Send a welcome email const 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>; -
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).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:
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.
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": "[email protected]",
"from": "[email protected]",
"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": ["[email protected]"],
"permanent_bounces": [],
"queued": []
}
}For more details, see the REST API reference.
Send an email with a single curl command. Replace <API_TOKEN> with a Cloudflare API token that has the Email Sending: Edit permission, and replace the --mail-from and --mail-rcpt addresses with your own.
cat > mail.txt <<EOF
From: [email protected]
To: [email protected]
Subject: Welcome to our service!
Thanks for signing up.
EOF
curl --ssl-reqd \
--url "smtps://smtp.mx.cloudflare.net:465" \
--user "api_token:<API_TOKEN>" \
--mail-from "[email protected]" \
--mail-rcpt "[email protected]" \
--upload-file mail.txtThe sender domain ([email protected]) must be onboarded for Email Sending on the account that owns the API token.
For connection details, authentication, response codes, and language-specific examples, see the SMTP reference.
Next steps
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