# 用 Python 发送

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

## 先决条件

- 一个存储为`SENDTERN_API_KEY`的Sendtern [API 密钥](https://sendtern.com/docs/authentication)
- Python 3.8 or newer

1. **安装请求**: 任何HTTP客户端都可以使用,本指南使用`requests`。

```
pip install requests
```

2. **发送电子邮件**: Create `send.py`.

```
# send.py
import os
import uuid
import requests

res = requests.post(
    "https://sendtern.com/emails",
    headers={
        "Authorization": f"Bearer {os.environ['SENDTERN_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "from": "Acme <hello@mail.example.com>",
        "to": ["you@example.com"],
        "subject": "Hello from Python",
        "html": "<p>It works!</p>",
    },
    timeout=10,
)

res.raise_for_status()
print(res.json())
```

3. **检查响应**: 成功的请求返回 `200 OK` 与电子邮件 id. 使用它来查看电子邮件后.

```
{
  "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d"
}
```

> **tip:** 将示例发件人替换为您的 [verified domain](https://sendtern.com/docs/domains) 上的地址。
