Sendery
Sendery

言語別 SDK

Kotlin

Java SDK を使って Kotlin から Sendery のテンプレートを送信します。

インストール
implementation("co.sendery:sendery-java:0.1.0")
ソースコード

動作環境

Kotlin/JVM と Java 17 以上が必要です。Gradle の dependencies ブロックにJava SDK の依存関係を追加してください。

設定

name と action_url を使う welcome テンプレートを公開し、プロジェクトの API キーを作成します。サーバーの環境変数 SENDERY_API_KEY に保存してください。

Shell
export SENDERY_API_KEY="your_project_api_key"

メールを送信

呼び出しは同期型です。コルーチン内ではアプリのコルーチンライブラリを使い、withContext(Dispatchers.IO) 内で送信してください。

Kotlin
import co.sendery.Sendery

fun main() {
    val sendery = Sendery(System.getenv("SENDERY_API_KEY"))
    val receipt = sendery.send("[email protected]", "welcome", mapOf(
        "name" to "Alex",
        "action_url" to "https://example.com/start"
    ))
    println(receipt.id())
}

メールの状態を取得

返された ID で配信状態を確認します。

Kotlin
val message = sendery.get(receipt.id())
println(message.status())

送信を再試行

welcome-123 などのキーを 1 通に割り当て、再試行時は元の内容を使います。retry(3) は一時的なエラーに対して最大 3 回の追加試行を行い、send() だけでは 1 回のみ試行します。 API エラーは co.sendery.SenderyException を返します。status()、code()、errors()、retryAfter() を確認してください。

Kotlin
val email = sendery.prepare("[email protected]", "welcome", mapOf(
    "name" to "Alex", "action_url" to "https://example.com/start"
), null, "welcome-123")
val receipt = email.retry(3).send()