|
1 | | -# jetemail-rust |
2 | | -JetEmail Rust SDK |
| 1 | +# JetEmail Rust SDK |
| 2 | + |
| 3 | +The official Rust SDK for the [JetEmail](https://jetemail.com) email API. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add to your `Cargo.toml`: |
| 8 | + |
| 9 | +```toml |
| 10 | +[dependencies] |
| 11 | +jetemail = "0.1" |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Send an email |
| 17 | + |
| 18 | +```rust |
| 19 | +use jetemail::{CreateEmailOptions, JetEmail}; |
| 20 | + |
| 21 | +#[tokio::main] |
| 22 | +async fn main() -> jetemail::Result<()> { |
| 23 | + let client = JetEmail::new("je_your_api_key"); |
| 24 | + |
| 25 | + let email = CreateEmailOptions::new( |
| 26 | + "you@example.com", |
| 27 | + "recipient@example.com", |
| 28 | + "Hello from JetEmail!", |
| 29 | + ) |
| 30 | + .with_html("<h1>Hello World!</h1>"); |
| 31 | + |
| 32 | + let response = client.emails.send(email).await?; |
| 33 | + println!("Email sent! ID: {}", response.id); |
| 34 | + |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Send to multiple recipients |
| 40 | + |
| 41 | +```rust |
| 42 | +let email = CreateEmailOptions::new( |
| 43 | + "you@example.com", |
| 44 | + vec!["alice@example.com".into(), "bob@example.com".into()], |
| 45 | + "Hello everyone!", |
| 46 | +) |
| 47 | +.with_html("<p>Hi there!</p>") |
| 48 | +.with_cc("cc@example.com") |
| 49 | +.with_bcc("bcc@example.com") |
| 50 | +.with_reply_to("reply@example.com"); |
| 51 | +``` |
| 52 | + |
| 53 | +### Send with attachments |
| 54 | + |
| 55 | +```rust |
| 56 | +use jetemail::Attachment; |
| 57 | + |
| 58 | +let email = CreateEmailOptions::new( |
| 59 | + "you@example.com", |
| 60 | + "recipient@example.com", |
| 61 | + "Email with attachment", |
| 62 | +) |
| 63 | +.with_html("<p>See attached.</p>") |
| 64 | +.with_attachment(Attachment::from_content(b"file contents", "file.txt")) |
| 65 | +.with_attachment(Attachment::from_path("./report.pdf")?); |
| 66 | +``` |
| 67 | + |
| 68 | +### Send batch emails |
| 69 | + |
| 70 | +Send up to 100 emails in a single API call: |
| 71 | + |
| 72 | +```rust |
| 73 | +let emails = vec![ |
| 74 | + CreateEmailOptions::new("you@example.com", "alice@example.com", "Hello Alice!") |
| 75 | + .with_html("<p>Hi Alice!</p>"), |
| 76 | + CreateEmailOptions::new("you@example.com", "bob@example.com", "Hello Bob!") |
| 77 | + .with_html("<p>Hi Bob!</p>"), |
| 78 | +]; |
| 79 | + |
| 80 | +let response = client.batch.send(emails).await?; |
| 81 | +``` |
| 82 | + |
| 83 | +### Custom headers |
| 84 | + |
| 85 | +```rust |
| 86 | +let email = CreateEmailOptions::new( |
| 87 | + "you@example.com", |
| 88 | + "recipient@example.com", |
| 89 | + "With custom headers", |
| 90 | +) |
| 91 | +.with_html("<p>Hello!</p>") |
| 92 | +.with_header("X-Custom-Header", "custom-value"); |
| 93 | +``` |
| 94 | + |
| 95 | +## Configuration |
| 96 | + |
| 97 | +### Environment variable |
| 98 | + |
| 99 | +```rust |
| 100 | +// Uses JETEMAIL_API_KEY environment variable |
| 101 | +let client = JetEmail::default(); |
| 102 | +``` |
| 103 | + |
| 104 | +### Advanced configuration |
| 105 | + |
| 106 | +Use `ConfigBuilder` for full control over the client. This lets you override the API base URL (useful for testing against a local or staging server), set a custom user agent, or provide your own `reqwest` client with custom timeouts or proxy settings. |
| 107 | + |
| 108 | +```rust |
| 109 | +use jetemail::ConfigBuilder; |
| 110 | + |
| 111 | +let config = ConfigBuilder::new("je_your_api_key") |
| 112 | + .base_url("https://staging-api.jetemail.com") // default: https://api.jetemail.com |
| 113 | + .user_agent("my-app/1.0") |
| 114 | + .client( |
| 115 | + reqwest::Client::builder() |
| 116 | + .timeout(std::time::Duration::from_secs(30)) |
| 117 | + .build() |
| 118 | + .unwrap(), |
| 119 | + ) |
| 120 | + .build(); |
| 121 | + |
| 122 | +let client = JetEmail::with_config(config); |
| 123 | +``` |
| 124 | + |
| 125 | +## Blocking Usage |
| 126 | + |
| 127 | +Enable the `blocking` feature for synchronous usage: |
| 128 | + |
| 129 | +```toml |
| 130 | +[dependencies] |
| 131 | +jetemail = { version = "0.1", features = ["blocking"] } |
| 132 | +``` |
| 133 | + |
| 134 | +```rust |
| 135 | +let client = JetEmail::new("je_your_api_key"); |
| 136 | +let response = client.emails.send(email)?; |
| 137 | +``` |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +MIT |
0 commit comments