How to send emails with Kotlin

Send emails from Kotlin (Android/server) with the `OkHttp` client.

There is no official SDK — call the Publiq REST API with your language HTTP client, from the server, with the API key in the Authorization header.

import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody

fun sendWelcome(to: String) {
    val client = OkHttpClient()
    val json = """{"from":"you@yourdomain.com","to":"$to","templateKey":"welcome-email"}"""
    val request = Request.Builder()
        .url("https://api.publiq.digital/v1/emails")
        .header("Authorization", "Bearer ${System.getenv("PUBLIQ_API_KEY")}")
        .post(json.toRequestBody("application/json".toMediaType()))
        .build()
    client.newCall(request).execute().use { }
}

Best practices

  • Always call Publiq from the server — never expose the API key on the client.
  • Create the client instance once and reuse it across requests.
  • Prefer templateKey over inline HTML to keep content versioned.
  • Handle PubliqError (status/code); the SDK already retries transient errors with backoff.
  • Send from a verified domain — see Domains.

See also

How to send emails with Kotlin — Publiq Docs