Mailman is a project that sends contact form data in an email to a specified recipient using Cloudflare Workers and Cloudflare Email Routing. It also includes spam protection using Cloudflare Turnstile to ensure only legitimate submissions are processed.
- Cloudflare account with Email Routing enabled.
- Verified email addresses in Cloudflare Email Routing.
- A sitekey and secret key. You can obtain the sitekey and secret key from the Cloudflare dashboard after adding your zone to Turnstile.
- Send Contact Form Entries: Mailman sends contact form data directly to an email address using Cloudflare's Email Worker API.
- Spam Protection: Integration with Cloudflare Turnstile ensures that bots cannot send spam emails via the contact form.
- Workers Logging: Enhanced logging provides insight into form submissions and errors.
index.ts
: Main worker script to handle incoming requests and send emails.wrangler.toml
: Configuration file for the Cloudflare Worker.
-
Clone the repository and intall dependencies:
git clone https://github.com/your-username/mailman.git cd mailman npm install
-
Set environment variables in
wrangler.toml
:[vars] SENDER = "[email protected]" RECIPIENT = "[email protected]"
Note: Set
TURNSTILE_SECRET_KEY
as a Wrangler secret either usingnpx wrangler secret put <KEY>
or your Cloudflare dashboard. -
Start the development server:
npm run dev
-
Deploy the worker:
npm run deploy
Mailman accepts POST requests with form data as the body. By default, the form fields include firstName, lastName, email, and message. Turnstile adds a hidden cf-turnstile-response field for server-side validation.
<form action="https://your-worker-url.workers.dev" method="POST">
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" required>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<!-- Turnstile widget -->
<div class="cf-turnstile" data-sitekey="your-site-key"></div>
<button type="submit">Send Message</button>
</form>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
You can also send a POST request using curl
:
curl -X POST https://your-worker-url.workers.dev \
-d "firstName=John&lastName=Doe&[email protected]&reason=Just%20saying%20hi!"
The server validates Turnstile tokens using Cloudflare’s siteverify
API to block invalid submissions. Logs are added to your Cloudflare Worker dashboard to help track successful and failed attempts.
This project is licensed under the MIT License.
Feel free to contribute to this project by opening issues or submitting pull requests on GitHub.