# Python で送信

> requests ライブラリを使って Python からメールを送信します。

## 前提条件

- Sendtern [API キー](https://sendtern.com/docs/authentication) が `SENDTERN_API_KEY` として保存されます
- 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. **応答をチェック**: 成功した要求は、メール id を含む `200 OK` を返します。これを使って後でメールを検索してください。

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

> **tip:** 例の送信者を自分のメールアドレスで置き換えます。 [確認されたドメイン](https://sendtern.com/docs/domains).
