Wrapping Aliyun DirectMail on Cloudflare Workers

2026.3.29

GitHub: https://github.com/imrcpan/aliyun-mail-cf-wrapper

A serverless wrapper for Aliyun DirectMail on Cloudflare Workers, allowing you to send emails easily via HTTP POST.

Why it

Traditional Aliyun DirectMail services typically rely on SMTP or complex SDKs to utilize the HTTP API, which can be quite inconvenient in many scenarios, such as lightweight Bash scripting. This wrapper simplifies the process, allowing you to send emails effortlessly with just a simple HTTP POST request, making it convenient for any use case.

Send Email

With this wrapper, you can easily send emails in almost any scenario:

python example:

import requests

requests.post(
    "https://your_cloudflare_deploy_here",
    json={
        "from": "[email protected]",
        "to": "[email protected]",
        "subject": "email subject",
        "body": "Hello World!",
    },
    headers={"X-Verify-Token": "your_token_here"},
)

bash example:

curl -X POST https://your_cloudflare_deploy_here \
-H "Content-Type: application/json" \
-H "X-Verify-Token: your_token_here" \
-d '{"from":"[email protected]","to":"[email protected]","subject":"email subject","body":"Hello World!"}'

In short, it's very simple.

Deploy on Cloudflare

1. Prepare Aliyun Access Key

  1. Go to RAM (Resource Access Management) => Users => Create or select an existing user.
  2. Click on Permissions, and add the AliyunDirectMailFullAccess policy.
  3. Click on the AccessKey tab, create a new AccessKey, and record your AccessKey ID and Secret.

2. Configure Cloudflare Secrets Store

First, prepare an auth token for authenticating the wrapped API. We recommend using a random number generator to create a token of at least 25 characters.

Next, head over to the Cloudflare Secrets Store page.

You'll need to configure the following three secrets here:

  1. aliyun_ak_msg_key: Your Access Key
  2. aliyun_ak_msg_secret: Your Access Secret
  3. msg_verify_token: Your Auth Token

3. Clone the Repository and Configure wrangler.jsonc

git clone https://github.com/imrcpan/aliyun-mail-cf-wrapper.git
cd aliyun-mail-cf-wrapper

Then copy file wrangler-example.jsonc to wrangler.jsonc:

cp wrangler-example.jsonc wrangler.jsonc

Open the file wrangler.jsonc, and you'll find the following configuration inside:

"secrets_store_secrets": [
    {
        "binding": "aliyun_ak_msg_user",
        "store_id": "YOUR_STORE_ID_HERE",
        "secret_name": "aliyun_ak_msg_user"
    },
    {
        "binding": "aliyun_ak_msg_key",
        "store_id": "YOUR_STORE_ID_HERE",
        "secret_name": "aliyun_ak_msg_key"
    },
    {
        "binding": "aliyun_ak_msg_secret",
        "store_id": "YOUR_STORE_ID_HERE",
        "secret_name": "aliyun_ak_msg_secret"
    },
    {
        "binding": "msg_verify_token",
        "store_id": "YOUR_STORE_ID_HERE",
        "secret_name": "msg_verify_token"
    }
]

Modify "store_id": "YOUR_STORE_ID_HERE",, by replacing the value with your own Cloudflare store_id. You can copy it from the Cloudflare Secrets Store page.

4. Deploy to Cloudflare Worker

Now for the final step: create a Worker and bind the three secrets we just created. Make sure the Variable Name matches the Secret Name exactly:

  1. Variable Name: aliyun_ak_msg_key, Secret Name: aliyun_ak_msg_key
  2. Variable Name: aliyun_ak_msg_secret, Secret Name: aliyun_ak_msg_secret
  3. Variable Name: msg_verify_token, Secret Name: msg_verify_token

Then, navigate to the src directory and deploy using the Cloudflare CLI tool:

npx wrangler deploy

If you haven't installed it yet, refer to the Cloudflare Official Doc and use the following command to install:

pnpm add -D wrangler@latest

Then, bind your domain in the Worker settings, and you're all set!

For International Users

In src/src/index.ts, the default service endpoint is set to China (Hangzhou). Since Alibaba Cloud uses different endpoints for different regions, please modify the configuration as needed to match your requirements.

Below are some reference endpoints; please refer to the official documentation for the most accurate information:

East China 1 (Hangzhou)
RegionId: cn-hangzhou
Public Endpoint: dm.aliyuncs.com

Singapore
RegionId:ap-southeast-1
Public Endpoint: dm.ap-southeast-1.aliyuncs.com

United States (formerly Sydney)
RegionId: ap-southeast-2
Public Endpoint: dm.ap-southeast-2.aliyuncs.com

United States (New)
RegionId: us-east-1
Public Endpoint: dm.us-east-1.aliyuncs.com

Germany (Frankfurt)
RegionId: eu-central-1
Public Endpoint: dm.eu-central-1.aliyuncs.com