Rust SDK
Send email with Actix Web
Use Parcel Wing from Actix Web handlers with application data.
Install the SDK
The official Rust SDK is published as parcelwing.
Terminal
cargo add parcelwingcargo add tokio --features macros,rt-multi-thread
Actix Web handler
Create the Parcel Wing client during server startup, then call it from trusted async handlers.
src/main.rs
use actix_web::{web, App, HttpResponse, HttpServer, Responder};use parcelwing::{Client, EmailSendRequest};use serde::Deserialize;#[derive(Deserialize)]struct WelcomeRequest {email: String,first_name: Option<String>,}async fn send_welcome(client: web::Data<Client>,payload: web::Json<WelcomeRequest>,) -> actix_web::Result<impl Responder> {let emails = client.emails().send(EmailSendRequest::new(std::env::var("PARCELWING_FROM_EMAIL").unwrap(), payload.email.clone()).template_alias("welcome").template_param("first_name", payload.first_name.clone().unwrap_or_else(|| "friend".to_string())),).await.map_err(actix_web::error::ErrorBadGateway)?;Ok(HttpResponse::Ok().json(serde_json::json!({ "id": emails[0].id })))}#[actix_web::main]async fn main() -> std::io::Result<()> {let client = Client::new(std::env::var("PARCELWING_API_KEY").unwrap()).unwrap();HttpServer::new(move || {App::new().app_data(web::Data::new(client.clone())).route("/send-welcome", web::post().to(send_welcome))}).bind(("0.0.0.0", 8080))?.run().await}
Production notes
- Keep
PARCELWING_API_KEYin server-side environment variables. - Build one SDK client during server startup and reuse it from trusted handlers.
- Log
parcelwing::Error::Apidetails for support and debugging.