Send Email with Axum
Send transactional email from an Axum Rust app with the Reloop SDK. Includes Cargo dependencies and a complete HTTP handler send example.
Installation
Add the official Rust SDK and standard tokio dependencies to your Cargo.toml:
[dependencies]
axum = "0.7"
tokio = { version = "1", features = ["full"] }
reloop = "0.1"
serde_json = "1"
Sending an Email
use axum::{routing::post, Json, Router};
use reloop::ReloopClient;
use serde_json::json;
use std::env;
#[tokio::main]
async fn main() {
let app = Router::new().route("/send-email", post(send_email));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn send_email() -> Result<Json<serde_json::Value>, (axum::http::StatusCode, String)> {
let api_key = env::var("RELOOP_API_KEY").map_err(|_| {
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, "Missing API Key".to_string())
})?;
let client = ReloopClient::new(api_key, None);
match client.mail().send(json!({
"from": "onboarding@reloop.sh",
"to": "delivered@resend.dev",
"subject": "Hello from Axum",
"html": "<p>Congrats on sending your first email via Reloop from Rust Axum!</p>",
})).await {
Ok(res) => Ok(Json(res)),
Err(e) => Err((axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}Was this page helpful?
- Need help? Contact Support.
- Chat with Reloop developers on Discord.
- Check out our changelog.
- Questions? Contact Sales.
- LLM? Read llms.txt.