| Crates.io | unosend |
| lib.rs | unosend |
| version | 1.0.0 |
| created_at | 2025-12-27 19:06:57.851757+00 |
| updated_at | 2025-12-27 19:06:57.851757+00 |
| description | Official Rust SDK for Unosend - Email API for developers |
| homepage | https://unosend.co |
| repository | https://github.com/unosend/unosend-rust |
| max_upload_size | |
| id | 2007686 |
| size | 60,055 |
The official Rust SDK for Unosend - Email API for developers.
Add to your Cargo.toml:
[dependencies]
unosend = "1.0"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] }
use unosend::{Unosend, SendEmailRequest, EmailRecipient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Unosend::new("un_your_api_key");
let response = client.emails().send(SendEmailRequest {
from: "hello@yourdomain.com".into(),
to: "user@example.com".into(),
subject: "Welcome!".into(),
html: Some("<h1>Hello!</h1><p>Welcome to our platform.</p>".into()),
..Default::default()
}).await?;
println!("Email sent: {}", response.id);
Ok(())
}
Use priority to control email delivery speed:
let response = client.emails().send(SendEmailRequest {
from: "noreply@yourdomain.com".into(),
to: "user@example.com".into(),
subject: "Your verification code: 847293".into(),
html: Some("<p>Your OTP code is: <strong>847293</strong></p>".into()),
priority: Some("high".into()), // "high", "normal", or "low"
..Default::default()
}).await?;
use std::collections::HashMap;
let mut template_data = HashMap::new();
template_data.insert("first_name".into(), serde_json::json!("John"));
template_data.insert("company_name".into(), serde_json::json!("Acme Inc"));
let response = client.emails().send(SendEmailRequest {
from: "hello@yourdomain.com".into(),
to: "user@example.com".into(),
subject: "Welcome!".into(),
template_id: Some("550e8400-e29b-41d4-a716-446655440000".into()),
template_data: Some(template_data),
..Default::default()
}).await?;
use chrono::{Utc, Duration};
let tomorrow = Utc::now() + Duration::days(1);
let response = client.emails().send(SendEmailRequest {
from: "hello@yourdomain.com".into(),
to: "user@example.com".into(),
subject: "Good morning!".into(),
html: Some("<p>This email was scheduled.</p>".into()),
scheduled_for: Some(tomorrow.to_rfc3339()),
..Default::default()
}).await?;
let response = client.emails().send(SendEmailRequest {
from: "hello@yourdomain.com".into(),
to: vec!["user1@example.com".into(), "user2@example.com".into()].into(),
subject: "Team Update".into(),
html: Some("<h1>Important Update</h1>".into()),
cc: Some(vec!["manager@example.com".into()]),
bcc: Some(vec!["archive@example.com".into()]),
reply_to: Some("support@yourdomain.com".into()),
..Default::default()
}).await?;
// Add a domain
let domain = client.domains().create("yourdomain.com").await?;
println!("Domain added: {}", domain.id);
// List all domains
let domains = client.domains().list().await?;
for d in domains.data {
println!("{} - {}", d.name, d.status);
}
// Verify domain
let domain = client.domains().verify(&domain.id).await?;
// Delete domain
client.domains().delete(&domain.id).await?;
use unosend::CreateContactRequest;
// Create an audience
let audience = client.audiences().create("Newsletter Subscribers").await?;
// Add a contact
let contact = client.contacts().create(CreateContactRequest {
audience_id: audience.id.clone(),
email: "user@example.com".into(),
first_name: Some("John".into()),
last_name: Some("Doe".into()),
unsubscribed: None,
}).await?;
// List contacts
let contacts = client.contacts().list(&audience.id).await?;
use unosend::UnosendError;
match client.emails().send(request).await {
Ok(response) => println!("Sent: {}", response.id),
Err(UnosendError::Api { message, status_code }) => {
eprintln!("API Error: {} (status: {})", message, status_code);
}
Err(UnosendError::Network(e)) => {
eprintln!("Network error: {}", e);
}
Err(e) => eprintln!("Error: {}", e),
}
MIT