发送示例

用 Python 发送

从Python发送电子邮件,请求库。

先决条件

  • 一个存储为SENDTERN_API_KEY的Sendtern API 密钥
  • Python 3.8 or newer
  1. 1

    安装请求

    任何HTTP客户端都可以使用,本指南使用requests。

    1pip install requests
    
  2. 2

    发送电子邮件

    Create send.py.

    1# send.py
    2import os
    3import uuid
    4import requests
    5
    6res = requests.post(
    7    "https://sendtern.com/emails",
    8    headers={
    9        "Authorization": f"Bearer {os.environ['SENDTERN_API_KEY']}",
    10        "Idempotency-Key": str(uuid.uuid4()),
    11    },
    12    json={
    13        "from": "Acme <hello@mail.example.com>",
    14        "to": ["you@example.com"],
    15        "subject": "Hello from Python",
    16        "html": "<p>It works!</p>",
    17    },
    18    timeout=10,
    19)
    20
    21res.raise_for_status()
    22print(res.json())
    
  3. 3

    检查响应

    成功的请求返回 200 OK 与电子邮件 id. 使用它来查看电子邮件后.

    1{
    2  "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d"
    3}
    

将示例发件人替换为您的 verified domain 上的地址。