Create a Draft and Send an Email
Enterprise email API requires two steps to send an email: first create a draft and save the returned email ID, then send that draft. For batch notifications or marketing emails, use the Direct Mail product.
Call Sequence
- Call Create a Draft Email.
- Read the draft ID from the
message.idfield in the response. - Call Send a Draft Email.
- Save the request ID and business result for auditing and troubleshooting.
Python Example
import requests
API_BASE = "https://alimail-cn.aliyuncs.com"
def send_mail(access_token: str, mailbox: str, message: dict) -> None:
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
draft = requests.post(
f"{API_BASE}/v2/users/{mailbox}/messages",
headers=headers,
json={"message": message},
timeout=15,
)
draft.raise_for_status()
draft_id = draft.json()["message"]["id"]
sent = requests.post(
f"{API_BASE}/v2/users/{mailbox}/messages/{draft_id}/send",
headers=headers,
json={"saveToSentItems": True},
timeout=15,
)
sent.raise_for_status()
Production code should also validate recipients, limit the scope of a single send, and add idempotent control for duplicate requests. Sending is a real external action; during integration testing, prioritize using a test email account.
Reference: Original Example.