Request example (Python 3.6 or later)
# -*- coding: utf-8 -*-
import hmac
import uuid
import base64
import datetime
import requests
import os
from hashlib import sha1
from urllib.parse import urlencode
from urllib.parse import quote
def getSignature(param, method):
utf8 = getUtf8Encoder(param)
toSign = method + "&" + quote('/', 'utf-8') + "&" + utf8
message = toSign.encode(encoding='UTF-8', errors='strict')
print("message:", message)
key = (AccessKeySecret + "&").encode(encoding='UTF-8', errors='strict')
bytes = hmac.new(key, message, sha1)
Signature = str(base64.b64encode(bytes.digest()), encoding="utf-8") # Base64-encode the signature and convert the byte type to a string.
print('Signature after Base64 encoding:' + Signature)
return Signature
def getUtf8Encoder(param):
# UTF-8 encoding.
# Replace plus signs (+) with %20, asterisks (*) with %2A, and %7E with tildes (~).
return quote(param.replace('+', '%20').replace('*', '%2A'), encoding="utf-8")
if __name__ == '__main__':
# https://dm.aliyuncs.com/?Action=SingleSendMail
# & AccountName = test @ example.com
# & AddressType = 1
# & ReceiversName = test2
# & TemplateName = test1
# & Add more parameters as needed.
# & < Common request parameters >
protocol = "https"
method = "POST"
host = "dm.aliyuncs.com"
AccessKeySecret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] # The AccessKey secret issued by Alibaba Cloud for service access. Replace this with your AccessKey secret. We recommend that you obtain it from an environment variable.
time1 = datetime.datetime.now()
time2 = time1 + datetime.timedelta(hours=-8)
print('Current system time: ', time1.strftime("%Y-%m-%d %H:%M:%S"), ' -8 hours, converted to UTC: ', time2.strftime("%Y-%m-%dT%H:%M:%SZ"))
params_dict = {}
# Assign values to parameters.
params_dict["AccessKeyId"] = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] # The AccessKey ID issued by Alibaba Cloud for service access. Replace this with your AccessKey ID. # Common request parameter.
params_dict["AccountName"] = 'test***@example.net' # The sender address that is configured in the console.
params_dict["Action"] = 'SingleSendMail' # A required parameter. Valid values: SingleSendMail and BatchSendMail.
params_dict["AddressType"] = 1 # 0: a random account. 1: a sender address.
params_dict["Format"] = 'JSON' # The format of the return value. Valid values: JSON and XML. Default value: XML. # Common request parameter.
params_dict["HtmlBody"] = 'Chinese body' # The body of the email in HTML format.
params_dict["RegionId"] = 'cn-hangzhou' # The data center information. For information about other regions, see the "Endpoints" topic.
params_dict["ReplyToAddress"] = 'true' # Set the value to true to use the reply-to address that is configured in the console.
params_dict["SignatureMethod"] = 'HMAC-SHA1' # The signature algorithm. Only HMAC-SHA1 is supported. # Common request parameter.
params_dict["SignatureNonce"] = str(uuid.uuid4()) # A common request parameter. This is a unique random number that is used to prevent replay attacks. Use a different random number for each request. You can use a universally unique identifier (UUID) or a custom string.
params_dict["SignatureVersion"] = 1.0 # A common request parameter. The version of the signature algorithm. The current version is 1.0.
params_dict["Subject"] = 'Custom subject' # The email subject.
params_dict["TagName"] = 'Custom tag' # The email tag.
params_dict['Template'] = '{"TemplateData":{"username":"222"},"TemplateId":"411558"}'
params_dict["Timestamp"] = time2.strftime(
"%Y-%m-%dT%H:%M:%SZ") # A common request parameter. The timestamp of the request. The time must be in UTC and follow the ISO 8601 standard. The format is YYYY-MM-DDThh:mm:ssZ. For example, 2015-11-23T04:00:00Z corresponds to 12:00:00 on November 23, 2015 (UTC+8).
params_dict["ToAddress"] = 'test1***@example.net' # The recipient address.
params_dict["Version"] = '2015-11-23' # A common request parameter.
# Version:
# The API version number in the YYYY-MM-DD format. We recommend that you use 2015-11-23.
print('Initial parameters:', params_dict)
# The following code shows how to encode the value of HtmlBody:
# Initial parameter: params_dict["HtmlBody"] = 'Chinese body'
# The message in the getSignature function: %25E4%25B8%25AD%25E6%2596%2587body. This is the result of double URL encoding.
# In the final URL: %E4%B8%AD%E6%96%87body
# URL-encode the initial parameter dictionary. dict-->str
# The order of server parameters cannot be changed.
# Sort the dictionary to allow flexibility in the order of passed parameters.
params_dict_sorted = {}
for i in sorted(params_dict):
params_dict_sorted[i] = params_dict[i]
print(params_dict_sorted)
url_dict_to_str = urlencode(params_dict_sorted)
print('After the first URL encoding:' + url_dict_to_str)# Example: %E4%B8%AD%E6%96%87body
print('Start generating the signature...')
params_dict["Signature"] = getSignature(url_dict_to_str, method)
print('Finish generating the signature...')
param = urlencode(params_dict)
print('After adding the signature to the request parameters and URL-encoding again:' + param)# Example: %E4%B8%AD%E6%96%87body
# Generate the URL.
final_url = protocol + "://" + host
print('Final generated URL for reference:', protocol + "://" + host + "/?" + param)
# The URL for the POST request.
# response = requests.post(final_url, data=params_dict)
# The URL for the POST request - Pass parameters in the body.
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post(final_url, data=params_dict, headers=headers)
# Return information. RequestId is the unique identifier that Alibaba Cloud generates for the request. Example: {"RequestId":"D0291CF2-BFDA-46F1-9DFD-6B32B5675B38","EnvId":"120414808748"}
print('Returned parameters:' + response.text)
# Print the URL parameters.
# listurl = final_url.split("&")
# for i in listurl:
# print(i)To configure the AccessKey secret and AccessKey ID as environment variables, see Configure AccessKey pairs in environment variables.
To obtain the RegionId for other regions, see Endpoints.
Example of a final request URL for the China (Hangzhou) region
https://dm.aliyuncs.com/?AccessKeyId=xxxxxxxx&AccountName=test%40t1.example.com&Action=SingleSendMail&AddressType=1&Format=JSON&HtmlBody=%E4%B8%AD%E6%96%87body&RegionId=cn-hangzhou&ReplyToAddress=true&SignatureMethod=HMAC-SHA1&SignatureNonce=a883f1bf-e415-4a4d-91ba-085ad3bc7741&SignatureVersion=1.0&Subject=%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%BB%E9%A2%98&TagName=%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%87%E7%AD%BE&Template=%7B%22TemplateData%22%3A%7B%22username%22%3A%22222%22%7D%2C%22TemplateId%22%3A%22411558%22%7D&Timestamp=2025-12-31T06%3A01%3A51Z&ToAddress=test%40example.com&Version=2015-11-23&Signature=%2Fapm1xqY3Ob9k9IgFbgZcrbeYrg%3D