Code Monkey home page Code Monkey logo

cloudflare-email's Introduction

Cloudflare Worker Email Server

Cloudflare Worker Email Server

Send free transactional emails from your Cloudflare Workers using MailChannels.

Getting Started!

  1. Clone this repository
  2. Install the dependencies with npm install
  3. Use the command npx wrangler secret put --env production TOKEN to deploy a securely stored token to Cloudflare. With this command, you will be prompted to enter a random secret value, which will be used to authenticate your requests with the HTTP Authorization header as described below. You can also set this encrypted value directly in your Cloudflare dashboard.
  4. Deploy the worker with npm run deploy

Or deploy directly to Cloudflare

Deploy to Cloudflare Workers

Setup SPF

SPF is a DNS record that helps prevent email spoofing. You will need to add an SPF record to your domain to allow MailChannels to send emails on your behalf.

  1. Add a TXT record to your domain with the following values:

     - Name: `@`
     - Value: `v=spf1 a mx include:relay.mailchannels.net ~all`
    

Note: If you're facing Domain Lockdown error, follow the below steps:

  1. Create a TXT record with the following values:

     - Name: `_mailchannels`
     - Value: `v=mc1 cfid=yourdomain.workers.dev` (the value of `cfid` will also be present in the error response)
    

Setup DKIM

This step is optional, but highly recommended. DKIM is a DNS record that helps prevent email spoofing. You may follow the steps listed in the MailChannels documentation to set up DKIM for your domain.

Usage

Once you have deployed this worker function to Cloudflare Workers, you can send emails by making a POST request to the worker on the /api/email endpoint with the following parameters:

  • Note you need to pass an Authorization header with the secure token you deployed. Like the following: Authorization: TOKEN

Basic Email

The Most basic request would look like this:

{
	"to": "[email protected]",
	"from": "[email protected]",
	"subject": "Hello World",
	"text": "Hello World"
}

HTML Emails

You can also send HTML emails by adding an html parameter to the request. This can be used in conjunction with the text parameter to send a multi-part email.

{
	"to": "[email protected]",
	"from": "[email protected]",
	"subject": "Hello World",
	"html": "<h1>Hello World</h1>"
}

Sender and Recipient Name

You can also specify a sender and recipient name by adding a name parameter to the request. This can be used for both the to and from parameters.

{
	"to": { "email": "[email protected]",  "name": "John Doe" },
	"from": { "email": "[email protected]", "name": "Jane Doe" },
	"subject": "Hello World",
	"text": "Hello World"
}

Sending to Multiple Recipients

You may also send to multiple recipients by passing an array of emails, or an array of objects with email and name properties.

{
	"to": [
		"[email protected]",
		"[email protected]"
 	],
	"from": "[email protected]",
	"subject": "Hello World",
	"text": "Hello World"
}

or

{
	"to": [
		{ "email": "[email protected]", "name": "John Doe" },
		{ "email": "[email protected]", "name": "Rose Doe" }
 	],
	"from": "[email protected]",
	"subject": "Hello World",
	"text": "Hello World"
}

Sending BCC and CC

You can also send BCC and CC emails by passing an array of emails, an object with email and name properties, or an array of either, similar to the to parameter.

{
	"to": "[email protected]",
	"from": "[email protected]",
	"subject": "Hello World",
	"text": "Hello World",
	"cc": [
		"[email protected]",
		"[email protected]"
	],
	"bcc": [
		"[email protected]"
	]
}

Reply To

You can also specify a reply to email address by adding a replyTo parameter to the request. Again, you can use an email string, an object with email and name properties, or an array of either.

{
	"to": "[email protected]",
	"from": "[email protected]",
	"replyTo": "[email protected]",
	"subject": "Hello World",
	"text": "Hello World"
}

cloudflare-email's People

Contributors

alter123 avatar benguild avatar sh4yy avatar taverasmisael avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cloudflare-email's Issues

cloudflare deploy

Hi,

I am trying to install this with the cloudflare deploy option,but after now 1 hour the status still showing " [Initializing] "
Is this still working or not, or should i do some pre settings before deploy ?

Return error details in HTTP body?

Was thinking about this. I don't have time to work on it tonight, but it might be nice to optionally return any errors to the caller in the HTTP body.

This could be disabled via an env variable.

Missing github workflow

Hey there, it is not possible to auto deploy this repository as it is missing a github workflow that does so. The manual instructions do work so I assume it was just an oversight?

plugin for worpress

Hi,

How can we use this to send notification mails from websites like wordpress and other?
or like a smtp service.

DKIM Support

Following the docs on how to setup DKIM it looks like we need to be able to pass a couple of things to the personalization object such as dkim_private_key.

As far as I can tell the worker currently doesn’t support that. Am I missing something?

Is this still working though?

I have deploy my this worker into production.

And added a SPF record and a _mailchannels record to my domainA.

And added a custom domainB to my worker.

Then try to POST https://domainB/api/email the body below:

{
	"to": "[email protected]",
	"from": "support@domainA",
	"subject": "Hello World",
	"text": "Hello World"
}

But it always showed Internal Server Error.

Is there anything I missing to use this email sender?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.