# Gửi bằng Python

> Gửi thư điện tử từ Python với thư viện yêu cầu.

## Điều kiện trước

- Một Sendtern [khóa API](https://sendtern.com/docs/authentication) được lưu như `SENDTERN_API_KEY`
- Python 3.8 or newer

1. **Yêu cầu cài đặt**: Bất kỳ trình khách HTTP nào cũng hoạt động. Hướng dẫn này sử dụng `requests`.

```
pip install requests
```

2. **Gửi thư điện tử**: 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. **Kiểm tra phản ứng**: Một yêu cầu thành công sẽ trả lại `200 OK` với id email. Dùng nó để tìm email sau này.

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

> **tip:** Thay thế người gửi ví dụ bằng một địa chỉ trên [domain xác thực](https://sendtern.com/docs/domains) của bạn.
