# Python でトランザクションメールを送信する

> Sendtern SDK を使って Python からトランザクションメールを送信します。Email Playground でテストし、メール ID で実際の配信を追跡できます。

## 前提条件

- Sendtern [API キー](https://sendtern.com/ja/docs/authentication) が `SENDTERN_API_KEY` として保存されます
- Python 3.9 以降

1. **SDK をインストールする**: プロジェクト内でこのコマンドを実行します。

```
python -m pip install https://github.com/Sendtern/sendtern-sdks/releases/download/v0.3.0/sendtern-0.3.0-py3-none-any.whl
```

2. **メールを送信**: `send.py` を作成し、`python send.py` で実行します。

```
import os
from sendtern import Sendtern

client = Sendtern(os.environ["SENDTERN_API_KEY"])
result = client.emails.send({
  "from": "Acme <hello@mail.example.com>",
  "to": "you@example.com",
  "subject": "Welcome to Acme",
  "text": "Thanks for signing up."
})
print(result["id"])
```

3. **応答をチェック**: 成功した要求は、メール id を含む `200 OK` を返します。これを使って後でメールを検索してください。

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

## 送信せずにテストする

例を `preview.py` として保存し、`python preview.py` を実行します。送信用ドメインを設定する前に、[Email Playground](https://sendtern.com/ja/dashboard/playground) でメールとヘッダーを確認できます。

```
import os
from sendtern import Sendtern

client = Sendtern(os.environ["SENDTERN_API_KEY"])
result = client.emails.capture({
  "from": "sandbox@sendtern.test",
  "to": "recipient@example.com",
  "subject": "Welcome email preview",
  "html": "<p>Thanks for signing up.</p>"
})
print(result["id"])
```

実際に送信したメールは、返された ID で[配信イベントを確認](https://sendtern.com/ja/docs/statuses)できます。月間送信上限は[トランザクションメールの料金プラン](https://sendtern.com/ja/pricing)をご覧ください。

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