SENTD Rust SDK
Official Rust client for the SENTD Email API.
Installation
Add this to your Cargo.toml:
[dependencies]
sentd = "1.0"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
Quick Start
use sentd::{Sentd, SendEmailRequest};
#[tokio::main]
async fn main() -> Result<(), sentd::Error> {
let client = Sentd::new("your_api_key");
let result = client.emails().send(SendEmailRequest {
from: "hello@yourdomain.com".to_string(),
to: vec!["user@example.com".to_string()],
subject: "Welcome!".to_string(),
html: Some("<h1>Hello World</h1>".to_string()),
..Default::default()
}).await?;
println!("Email sent! ID: {}", result.data.id);
Ok(())
}
Features
- Emails - Send, list, get, cancel, reschedule, and resend emails
- Batch - Send bulk emails efficiently
- Templates - Create and manage email templates
- Domains - Add and verify sending domains
- Webhooks - Configure webhook endpoints
- Analytics - Get email statistics
API Reference
Emails
let result = client.emails().send(SendEmailRequest {
from: "hello@yourdomain.com".to_string(),
to: vec!["user@example.com".to_string()],
subject: "Hello".to_string(),
html: Some("<h1>World</h1>".to_string()),
..Default::default()
}).await?;
let emails = client.emails().list(Some(ListEmailsParams {
limit: Some(10),
status: Some("delivered".to_string()),
..Default::default()
})).await?;
let email = client.emails().get("email_id").await?;
client.emails().cancel("email_id").await?;
client.emails().resend("email_id").await?;
Batch
use sentd::{SendBatchRequest, BatchEmail};
use std::collections::HashMap;
let mut data = HashMap::new();
data.insert("name".to_string(), serde_json::json!("Alice"));
let result = client.batch().send(SendBatchRequest {
emails: vec![
BatchEmail {
to: "user1@example.com".to_string(),
data: Some(data),
..Default::default()
},
],
from: Some("hello@yourdomain.com".to_string()),
subject: Some("Newsletter".to_string()),
html: Some("<h1>Hello {{name}}</h1>".to_string()),
..Default::default()
}).await?;
Templates
use sentd::CreateTemplateRequest;
let template = client.templates().create(CreateTemplateRequest {
name: "Welcome Email".to_string(),
subject_template: "Welcome, {{name}}!".to_string(),
html_template: "<h1>Hello {{name}}</h1>".to_string(),
..Default::default()
}).await?;
let templates = client.templates().list().await?;
let mut data = HashMap::new();
data.insert("name".to_string(), serde_json::json!("John"));
let preview = client.templates().preview("template_id", data).await?;
Domains
let domain = client.domains().add("yourdomain.com").await?;
let domains = client.domains().list().await?;
let result = client.domains().verify("domain_id").await?;
Webhooks
use sentd::CreateWebhookRequest;
let webhook = client.webhooks().create(CreateWebhookRequest {
url: "https://yourdomain.com/webhooks".to_string(),
events: vec!["email.delivered".to_string(), "email.bounced".to_string()],
active: Some(true),
}).await?;
let result = client.webhooks().test("webhook_id").await?;
Analytics
use sentd::analytics::AnalyticsParams;
let analytics = client.analytics().get(Some(AnalyticsParams {
days: Some(30),
group_by: Some("day".to_string()),
})).await?;
let csv = client.analytics().export_csv(Some(30)).await?;
Configuration
let client = Sentd::with_base_url("your_api_key", "https://custom.api.sentd.io");
Error Handling
use sentd::Error;
match client.emails().send(request).await {
Ok(result) => println!("Sent: {}", result.data.id),
Err(Error::Authentication(msg)) => eprintln!("Invalid API key: {}", msg),
Err(Error::RateLimit(msg)) => eprintln!("Rate limited: {}", msg),
Err(Error::Validation(msg)) => eprintln!("Validation error: {}", msg),
Err(Error::NotFound(msg)) => eprintln!("Not found: {}", msg),
Err(e) => eprintln!("Error: {}", e),
}
License
MIT License - see LICENSE for details.