How to send emails with Rust

Send emails from Rust with `reqwest` (async) and `serde_json`.

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.

use reqwest::Client;
use serde_json::json;

async fn send_welcome(to: &str) -> reqwest::Result<()> {
    let client = Client::new();
    client
        .post("https://api.publiq.digital/v1/emails")
        .bearer_auth(std::env::var("PUBLIQ_API_KEY").unwrap())
        .json(&json!({
            "from": "you@yourdomain.com",
            "to": to,
            "templateKey": "welcome-email",
            "variables": { "first_name": "Ana" }
        }))
        .send()
        .await?;
    Ok(())
}

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 Rust — Publiq Docs