| Crates.io | send_with_us |
| lib.rs | send_with_us |
| version | 0.1.2 |
| created_at | 2025-04-05 06:41:29.892455+00 |
| updated_at | 2025-06-14 16:08:53.016799+00 |
| description | Async Rust client for the SendWithUs transactional email API |
| homepage | |
| repository | https://github.com/endoze/send_with_us |
| max_upload_size | |
| id | 1621652 |
| size | 166,622 |
An async Rust client for the SendWithUs API.
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"] }
use send_with_us::{Api, Config, ApiClient};
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(())
}
use send_with_us::{Api, Attachment, ApiClient};
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(())
}
use send_with_us::{Api, Config, ApiClient};
#[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(())
}
use send_with_us::{Api, ApiClient, types::{TemplateOptions}};
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api = Api::with_api_key("YOUR_API_KEY");
let template = TemplateOptions {
name: "Welcome Email".to_string(),
subject: "Welcome to Our Service".to_string(),
html: "<html><body>Welcome, {{name}}!</body></html>".to_string(),
text: "Welcome, {{name}}!".to_string(),
preheader: Some("Welcome to our service".to_string()),
amp_html: None,
};
let result = api.create_template(template).await?;
Ok(())
}
use send_with_us::{Api, ApiClient, Error, types::{EmailOptions, Recipient}};
#[tokio::main]
async fn main() {
let api = Api::with_api_key("YOUR_API_KEY");
let recipient = Recipient::new("test@example.com");
let options = EmailOptions::new("", recipient); // Empty template ID
match api.send_email(options).await {
Ok(response) => println!("Email sent: {:?}", response),
Err(Error::MissingTemplateId) => eprintln!("Error: Template ID is required"),
Err(Error::InvalidCredentials) => eprintln!("Error: Invalid API key"),
Err(Error::ConnectionFailed) => eprintln!("Error: Could not connect to SendWithUs API"),
Err(err) => eprintln!("Error: {}", err),
}
}
The client provides methods for all SendWithUs API endpoints:
send_email - Send a templated emaillist_templates - List all templatesrender - Render a template with datacreate_template - Create a new templatelist_drip_campaigns - List all drip campaignsstart_on_drip_campaign - Start a recipient on a drip campaignremove_from_drip_campaign - Remove a recipient from a drip campaignSee the API documentation for complete details.
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.
This project is licensed under the MIT License - see the LICENSE file for details.