Quick setup examples
Axum
Send your first email from an Axum handler using reqwest against the MailBlastr API.
The mailblastr SDK is for Node.js — Rust users send email by POSTing JSON to https://api.mailblastr.com/emails. The example below builds a small Axum app whose handler calls the API with the async `reqwest` client on the Tokio runtime.
Prerequisites
- A MailBlastr API key.
- A verified domain to send from.
1. Install dependencies
Add Axum, an async HTTP client, a runtime, and JSON support to your Cargo project.
cargo add axum
cargo add reqwest -F json
cargo add tokio -F macros,rt-multi-thread
cargo add serde_json2. Set your API key
.env
MAILBLASTR_API_KEY=mb_xxxxxxxxx3. Send an email
Build an Axum router with a single route whose handler POSTs the email and returns the created id.
src/main.rs
use axum::{http::StatusCode, routing::get, Router};
use serde_json::json;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(endpoint));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn endpoint() -> Result<String, StatusCode> {
let api_key = std::env::var("MAILBLASTR_API_KEY")
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let res = reqwest::Client::new()
.post("https://api.mailblastr.com/emails")
.bearer_auth(api_key)
.json(&json!({
"from": "Acme <onboarding@yourdomain.com>",
"to": ["delivered@example.com"],
"subject": "Hello World",
"html": "<strong>It works!</strong>",
}))
.send()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let body: serde_json::Value = res
.json()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(body["id"].to_string())
}Opening your browser at http://localhost:3000 (or running curl localhost:3000) sends an email and returns its id.