A Cloudflare Worker that forwards incoming emails to a webhook URL. This service allows you to receive email notifications and forward them to any webhook endpoint of your choice.
- Forwards incoming emails to a configurable webhook URL
- Includes fallback email for error notifications
- Built on Cloudflare Workers for high reliability and low latency
- Easy to deploy and configure
- Node.js (v16 or later)
- Wrangler CLI (Cloudflare Workers CLI)
- A Cloudflare account
- A webhook endpoint to receive the email data
-
Clone this repository:
git clone git@github.com:bnqtoan/email2webhook.git cd email2webhook -
Install dependencies:
npm install
-
Create a
.envfile in the root directory with your configuration:WEBHOOK_URL=your_webhook_url_here FALLBACK_EMAIL=your_fallback_email@example.com -
Login to Cloudflare:
wrangler login
-
Set up your environment variables in Cloudflare:
wrangler secret put WEBHOOK_URL wrangler secret put FALLBACK_EMAIL
-
Deploy the worker:
wrangler deploy
-
After deployment, you'll receive a workers.dev subdomain. You can also set up a custom domain in the Cloudflare dashboard.
-
Once deployed, your worker will be available at
https://your-worker.your-subdomain.workers.dev -
Configure your email service to forward emails to your worker's address. The worker will:
- Receive the email
- Process it
- Forward the content to your webhook URL
- Send error notifications to your fallback email if something goes wrong
The webhook will receive a POST request with the following JSON structure:
{
"from": "sender@example.com",
"to": "your-worker@your-subdomain.workers.dev",
"subject": "Email Subject",
"text": "Email body in plain text",
"html": "Email body in HTML format (if available)",
"attachments": [
{
"filename": "attachment.pdf",
"content": "base64_encoded_content",
"contentType": "application/pdf"
}
]
}-
Run the worker locally:
wrangler dev
-
Test your changes before deploying to production.
- Keep your
.envfile secure and never commit it to version control - Use HTTPS for your webhook URL
- Regularly rotate your secrets and credentials
- Monitor your worker's logs for any suspicious activity
If you encounter issues:
- Check the Cloudflare Workers logs in the dashboard
- Verify your webhook URL is accessible
- Ensure your fallback email is correctly configured
- Check that your email forwarding is properly set up
MIT
Contributions are welcome! Please feel free to submit a Pull Request.