#api #attachment #logging #client #config #endpoint #tokio #tracing

send_with_us

Async Rust client for the SendWithUs transactional email API

1 unstable release

Uses new Rust 2024

new 0.1.0 Apr 5, 2025

#5 in #attachment

MIT license

96KB
1.5K SLoC

SendWithUs Rust Client

An async Rust client for the SendWithUs API.

Features

  • Full async support with Tokio
  • Type-safe API
  • Comprehensive error handling
  • Support for all SendWithUs API endpoints
  • Simple and ergonomic interface
  • File attachment support
  • Optional logging support via tracing

Installation

Add this to your Cargo.toml:

[dependencies]
send_with_us = "0.1.0"
# With optional logging feature enabled
send_with_us = { version = "0.1.0", features = ["logging"] }

Usage

Basic Example

use send_with_us::{Api, Config};
use send_with_us::types::{EmailOptions, Recipient};
use std::collections::HashMap;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let api = Api::with_api_key("YOUR_API_KEY");
  
  let recipient = Recipient::new("user@example.com").with_name("Example User");
  
  let mut email_data = HashMap::new();
  email_data.insert("first_name".to_string(), json!("John"));
  email_data.insert("amount".to_string(), json!(100));
  
  let options = EmailOptions::new("template_id", recipient)
    .with_data(email_data);
  
  match api.send_email(options).await {
    Ok(response) => println!("Email sent successfully: {:?}", response),
    Err(e) => eprintln!("Failed to send email: {}", e),
  }
  
  Ok(())
}

With Email Attachments

use send_with_us::{Api, Attachment};
use send_with_us::types::{EmailOptions, Recipient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let api = Api::with_api_key("YOUR_API_KEY");
  let recipient = Recipient::new("user@example.com");
  
  let attachment = Attachment::from_path("path/to/file.pdf").await?;
  
  let options = EmailOptions::new("template_id", recipient)
    .with_files(vec![attachment]);
  
  api.send_email(options).await?;
  
  Ok(())
}

Custom Configuration

use send_with_us::{Api, Config};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let config = Config::new("YOUR_API_KEY")
    .with_url("https://custom-api.sendwithus.com")
    .with_api_version("2")
    .with_debug(true);
  
  let api = Api::new(config);
  
  let templates = api.list_templates().await?;
  println!("Templates: {:?}", templates);
  
  Ok(())
}

API Documentation

The client provides methods for all SendWithUs API endpoints:

  • send_email - Send a templated email
  • list_templates - List all templates
  • render - Render a template with data
  • create_template - Create a new template
  • list_drip_campaigns - List all drip campaigns
  • start_on_drip_campaign - Start a recipient on a drip campaign
  • remove_from_drip_campaign - Remove a recipient from a drip campaign
  • And many more...

See the API documentation for complete details.

Logging

This library provides optional logging integration via the tracing crate. To enable it, add the logging feature to your dependency:

[dependencies]
send_with_us = { version = "0.1.0", features = ["logging"] }

When the logging feature is enabled, the library will log API operations, request details, and responses at appropriate trace levels. This can be helpful for debugging and monitoring API usage.

For development and testing, the tracing crate is included as a dev-dependency, allowing example code to use it without requiring it for production usage.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~6–18MB
~232K SLoC