Skip to content

bobadilla-tech/landing-contact-us-email

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contact Form Worker

A Cloudflare Worker that handles contact form submissions and sends emails using Cloudflare Email Routing.

Features

  • 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

Setup

Prerequisites

  • Node.js and npm installed
  • Cloudflare account
  • Wrangler CLI installed (npm install -g wrangler)

Installation

  1. Clone the repository:
git clone <repository-url>
cd email
  1. Install dependencies:
npm install
  1. Configure your email addresses in wrangler.toml:
[[send_email]]
name = "CONTACT_EMAIL"
allowed_destination_addresses = [
  "[email protected]",
  "[email protected]"
]
  1. Update the sender address in src/index.js:27:
msg.setSender({ name: "Contact Form", addr: "[email protected]" });

Development

Run the worker locally:

npm run dev

Deployment

Deploy to Cloudflare:

npm run deploy

View live logs:

npm run tail

API Usage

Endpoint

POST /

Request Body

{
  "name": "John Doe",
  "email": "[email protected]",
  "company": "Example Corp (optional)",
  "message": "Your message here"
}

Required Fields

  • name: Sender's name
  • email: Sender's email address
  • message: Message content

Optional Fields

  • company: Sender's company name

Response

Success (200):

{
  "success": true,
  "message": "Email sent successfully"
}

Error (400/500):

{
  "error": "Error message",
  "details": "Additional details"
}

Configuration

wrangler.toml

Key configuration options:

  • name: Worker name
  • compatibility_date: Cloudflare Workers compatibility date
  • compatibility_flags: Enable Node.js compatibility with ["nodejs_compat"]
  • send_email: Email routing configuration

Email Recipients

Email recipients are configured in two places:

  1. Allowed destinations in wrangler.toml (security whitelist)
  2. Actual recipients in src/index.js:30-31 using msg.setRecipient()

Both must be configured for emails to be delivered.

Dependencies

  • mimetext: MIME message creation for email formatting
  • wrangler: Cloudflare Workers CLI tool

Security

  • Only accepts POST requests
  • Validates all required fields before processing
  • Email destinations are whitelisted in configuration
  • CORS headers can be configured for specific domains

About

📧 Sending Emails through cloudflare

Topics

Resources

Stars

Watchers

Forks