言語別 SDK
Python
Python から Sendery の公開済みテンプレートを送信します。
インストール
pip install sendery==0.1.4動作環境
Python 3.10 以上が必要です。
設定
公開済みテンプレートを選び、プロジェクトの API キーを作成します。キーはサーバーの環境変数 SENDERY_API_KEY に保存してください。
Shell
export SENDERY_API_KEY="your_project_api_key"メールを送信
your-template を公開済みテンプレートのキーに、data をそのテンプレートの変数に置き換えてください。
受付済みメールの id と status が返ります。 同期型のため、非同期コードからは asyncio.to_thread() で呼び出してください。
Python
import os
from sendery import Sendery
sendery = Sendery(os.environ["SENDERY_API_KEY"])
receipt = sendery.send(
to="[email protected]",
template="your-template",
data={"name": "Alex", "action_url": "https://example.com/start"},
)
print(receipt["id"])メールの状態を取得
返された ID で配信状態を確認します。
Python
message = sendery.get(receipt["id"])
print(message["status"])送信を再試行
一時的なエラーには retry(3) で最大 3 回追加で試行できます。毎回同じ冪等性キーとメールのデータを使ってください。
Python
email = sendery.prepare(
to="[email protected]",
template="your-template",
data={"name": "Alex", "action_url": "https://example.com/start"},
idempotency_key="your-idempotency-key",
)
receipt = email.retry(3).send()エラー処理
Python
from sendery import SenderyError
try:
receipt = email.retry(3).send()
except SenderyError as error:
print(error.status, error.code, error.errors)
# error.retry_after is a delay in seconds, when provided.
raise特定のバージョンを送信
公開履歴のバージョンを指定すると、テンプレートの更新後もその版を送信できます。指定しない場合は最新バージョンを使います。
Python
receipt = sendery.prepare(
to="[email protected]",
template="your-template",
data={"name": "Alex", "action_url": "https://example.com/start"},
).version(3).send()添付ファイル
ファイルのバイト列を attachment() に渡します。base64への変換はヘルパーが行います。
最大10ファイル、合計5 MBまで送信できます。対応形式と上限は 添付ファイルのリファレンス を参照してください。
使用例
from pathlib import Path
from sendery import attachment
file = Path("document.pdf").read_bytes()
sendery.prepare(
to="[email protected]",
template="your-template",
data={"name": "Alex", "action_url": "https://example.com/start"},
idempotency_key="your-idempotency-key",
attachments=[attachment("document.pdf", file, "application/pdf")],
).retry().send()