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.
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.
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.
AliyunDirectMailFullAccess policy.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:
wrangler.jsoncgit 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.
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:
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!
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