A Cloudflare Worker that handles contact form submissions and sends emails using Cloudflare Email Routing.
- Receives contact form submissions via POST requests
- Validates required fields (name, email, message)
- Sends formatted emails to multiple recipients
- CORS-enabled for cross-origin requests
- Built with Cloudflare Workers and Email Routing
- Node.js and npm installed
- Cloudflare account
- Wrangler CLI installed (
npm install -g wrangler)
- Clone the repository:
git clone <repository-url>
cd email- Install dependencies:
npm install- Configure your email addresses in
wrangler.toml:
[[send_email]]
name = "CONTACT_EMAIL"
allowed_destination_addresses = [
"[email protected]",
"[email protected]"
]- Update the sender address in src/index.js:27:
msg.setSender({ name: "Contact Form", addr: "[email protected]" });Run the worker locally:
npm run devDeploy to Cloudflare:
npm run deployView live logs:
npm run tailPOST /
{
"name": "John Doe",
"email": "[email protected]",
"company": "Example Corp (optional)",
"message": "Your message here"
}name: Sender's nameemail: Sender's email addressmessage: Message content
company: Sender's company name
Success (200):
{
"success": true,
"message": "Email sent successfully"
}Error (400/500):
{
"error": "Error message",
"details": "Additional details"
}Key configuration options:
name: Worker namecompatibility_date: Cloudflare Workers compatibility datecompatibility_flags: Enable Node.js compatibility with["nodejs_compat"]send_email: Email routing configuration
Email recipients are configured in two places:
- Allowed destinations in
wrangler.toml(security whitelist) - Actual recipients in src/index.js:30-31 using
msg.setRecipient()
Both must be configured for emails to be delivered.
mimetext: MIME message creation for email formattingwrangler: Cloudflare Workers CLI tool
- Only accepts POST requests
- Validates all required fields before processing
- Email destinations are whitelisted in configuration
- CORS headers can be configured for specific domains