Read Medium logo
No Results
Translate to
Read Medium Logo
Free OpenAI o1 chatTry OpenAI o1 API
Read Medium logo
No Results
Translate to
avatarTristan Trommer

Summary

The web content provides a comprehensive guide on setting up and using worker-mailchannels with Cloudflare Workers to send transactional emails for free, with a note that the service will be discontinued on June 30th, 2024.

Abstract

The article titled "How to Send Free Transactional Emails with worker-mailchannels via Cloudflare Workers" outlines a cost-effective method for sending transactional emails using worker-mailchannels, a tool that integrates with Cloudflare Workers and leverages MailChannels for email delivery. It explains the benefits of Cloudflare Workers for serverless execution at the network edge, the role of MailChannels in improving email delivery, and the simplicity of worker-mailchannels as an API-driven application for configuring email delivery. The guide includes detailed prerequisites, deployment instructions, domain lockdown setup, SPF and DKIM configuration for email authentication, and examples of how to use the API for sending emails, including a JavaScript code snippet for integration. However, it is important to note that Cloudflare Workers and MailChannels integration will no longer be supported after June 30th, 2024.

Opinions

  • The author believes that using worker-mailchannels with Cloudflare Workers offers a "hassle-free solution" for sending transactional emails.
  • The integration of Cloudflare Workers and MailChannels is presented as an innovative approach that eliminates the need for server management and improves performance by executing code at the network's edge.
  • The article suggests that MailChannels is a reliable service for bolstering email delivery and protecting domain reputation, which is crucial for ensuring emails reach the intended recipients.
  • The guide emphasizes the importance of proper email authentication through SPF and DKIM records to reduce the likelihood of emails being marked as spam.
  • The provided JavaScript example demonstrates the ease of integrating the worker-mailchannels API into applications for sending emails.

How to Send Free Transactional Emails with worker-mailchannels via Cloudflare Workers

Cloudflare Wokers and MailChannels will be coming to an end on June 30th, 2024.

Are you in search of a cost-effective, hassle-free solution for dispatching transactional emails? worker-mailchannels paired with Cloudflare Workers offers a streamlined process to accomplish just that. This guide will walk you through setting up and utilizing worker-mailchannels, ensuring a smooth experience in sending emails directly from your applications using an API.

Understanding Cloudflare Workers

Cloudflare Workers function as a serverless execution environment, empowering developers to execute code right at the network’s edge. This innovative feature, accessible through the Cloudflare ecosystem, eliminates the necessity of server management while offering global execution close to users, slashing latency and significantly improving performance.

Understanding MailChannels?

MailChannels serves as an outbound email relay service crafted to bolster email delivery and shield your domain’s reputation. It cleverly navigates outbound emails through a delivery-optimized network, resolving common email delivery hurdles like spam filters and IP reputation issues.

Understanding worker-mailchannels?

worker-mailchannels is a basic, API-driven application that eases the configuration of email delivery through Cloudflare Workers, leveraging MailChannels in the process.

Prerequisites

  1. A domain verified with Cloudflare.
  2. Wrangler CLI installed and authenticated on your system.

Deploy worker-mailchannels

  1. Clone the repository using git clone https://git.tristantrommer.com/tristantrommer/worker-mailchannels.git and navigate into the directory with cd worker-mailchannels.
  2. Install the required package dependencies with npm install.
  3. Deploy worker-mailchannels to Cloudflare with npm run deploy.
  4. Set up your Basic Auth username with npx wrangler secret put BASIC_AUTH_USERNAME and repeat the process for the password with npx wrangler secret put BASIC_AUTH_PASSWORD.

Setup Domain Lockdown

Create a TXT record with your domain to activate Domain Lockdown, crucial for using MailChannels through Cloudflare Workers.

  • Name: _mailchannels
  • Value: v=mc1 cfid=example.workers.dev

Setup SPF

Create an SPF record to mark MailChannels as an authorized email sender for your domain, hence reducing the chances of your emails being flagged as spam.

  • Name: @
  • Value: v=spf1 include:relay.mailchannels.net -all

Setup DKIM

Generate and deploy a DKIM private key using OpenSSL to validate that the emails are indeed sent from your domain, ensuring that the message content remains unaltered during transmission.

  1. Run openssl genrsa 2048 | tee priv_key.pem | openssl rsa -outform der | openssl base64 -A > priv_key.txt to generate a DKIM private key.
  2. Run echo -n "v=DKIM1;p=" > pub_key_record.txt && openssl rsa -in priv_key.pem -pubout -outform der | openssl base64 -A >> pub_key_record.txt to generate a DKIM public key.
  3. Run npx wrangler secret put DKIM_PRIVATE_KEY to deploy the base64 encoded DKIM private key.

Create a TXT record for DKIM to make the public key discoverable.

  • Name: mcdkim._domainkey
  • Value: Record from pub_key_record.txt

Usage

To utilize the API, send a POST request to https://worker-mailchannels.example.workers.dev/api, including the specified JSON payload in the body of the request. Remember to appropriately configure the Authorization header.

{
  "to": [
    {
      "email": "[email protected]",
      "name": "Example"
    }
  ],
  "replyTo": {
    "email": "[email protected]",
    "name": "Example"
  },
  "cc": [
    {
      "email": "[email protected]",
      "name": "Example"
    }
  ],
  "bcc": [
    {
      "email": "[email protected]",
      "name": "Example"
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "Example"
  },
  "subject": "Example",
  "text": "Example",
  "html": "<h1>Example</h1>"
}

Usage in JavaScript

const workerMailChannels = async () => {
  const workerMailChannelsURL = 'https://worker-mailchannels.example.workers.dev/api';
  const workerMailChannelsUsername = 'example';
  const workerMailChannelsPassword = 'example';
  const response = await fetch(workerMailChannelsURL, {
    method: 'POST',
    body: JSON.stringify({
      'to': [
        {'email': '[email protected]', 'name': 'Example'}
      ],
      'from': {'email': '[email protected]', 'name': 'Example'},
      'subject': 'Example',
      'html': '<h1>Example</h1>',
      'text': 'Example'
    }),
    headers: {
      'content-type': 'application/json',
      'Authorization': 'Basic ' + Buffer.from(workerMailChannelsUsername + ':' + workerMailChannelsPassword).toString('base64')
    }
  });
  if (response.ok) return true;
  return false;
}
Cloudflare Workers
Mailchannels
API
Recommended from ReadMedium
avatarHarendra
How I Am Using a Lifetime 100% Free Server

Get a server with 24 GB RAM + 4 CPU + 200 GB Storage + Always Free

3 min read
avatarDmitry Bobryshev
Architectures of modern Front-end applications

This post explores the advantages of popular architectures and helps you choose the best solution to meet your unique requirements.

7 min read
avatarJessica Stillman
Jeff Bezos Says the 1-Hour Rule Makes Him Smarter. New Neuroscience Says He’s Right

Jeff Bezos’s morning routine has long included the one-hour rule. New neuroscience says yours probably should too.

4 min read
avatarAustin Starks
I used OpenAI’s o1 model to develop a trading strategy. It is DESTROYING the market

It literally took one try. I was shocked.

8 min read
avatarCrafting-Code
I Stopped Using Kubernetes. Our DevOps Team Is Happier Than Ever

Why Letting Go of Kubernetes Worked for Us

10 min read
avatarTari Ibaba
This new JavaScript operator is an absolute game changer

Say goodbye to try-catch

4 min read