# 用 Python 發送

> 使用 requests 函式庫從 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) 的地址替换示例发件人。
