Sendery
Sendery

言語別 SDK

Kotlin

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

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

動作環境

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

設定

公開済みテンプレートを選び、プロジェクトの API キーを作成します。キーはサーバーの環境変数 SENDERY_API_KEY に保存してください。

Shell
export SENDERY_API_KEY="your_project_api_key"

メールを送信

your-template を公開済みテンプレートのキーに、data をそのテンプレートの変数に置き換えてください。

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

Kotlin
import co.sendery.Sendery

fun main() {
    val sendery = Sendery(System.getenv("SENDERY_API_KEY"))
    val receipt = sendery.send("[email protected]", "your-template", 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())

送信を再試行

一時的なエラーには retry(3) で最大 3 回追加で試行できます。毎回同じ冪等性キーとメールのデータを使ってください。 API エラーでは co.sendery.SenderyException が発生します。

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

特定のバージョンを送信

公開履歴のバージョンを指定すると、テンプレートの更新後もその版を送信できます。指定しない場合は最新バージョンを使います。

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

添付ファイル

ファイル名、バイト列、MIME型を指定した Attachment のリストを渡します。base64への変換はSDKが行います。

最大10ファイル、合計5 MBまで送信できます。対応形式と上限は 添付ファイルのリファレンス を参照してください。

使用例
import co.sendery.Attachment
import java.nio.file.Files
import java.nio.file.Path

val file = Files.readAllBytes(Path.of("document.pdf"))

sendery.prepare(
    "[email protected]",
    "your-template",
    mapOf("name" to "Alex", "action_url" to "https://example.com/start"),
    null,
    "your-idempotency-key",
    listOf(Attachment("document.pdf", file, "application/pdf"))
).retry().send()