Skip to main content

mailtrap/
lib.rs

1//! # Mailtrap
2//!
3//! `mailtrap` is an unofficial Rust library for interacting with the [Mailtrap](https://mailtrap.io) API.
4//! It allows you to create and send emails programmatically, manage accounts, and handle access permissions.
5//!
6//! ## Usage
7//!
8//! Add this to your `Cargo.toml`:
9//!
10//! ```toml
11//! [dependencies]
12//! mailtrap = "0.3.1"
13//! tokio = { version = "1", features = ["full"] }
14//! ```
15//!
16//! ## Examples
17//!
18//! ### Basic Email Sending
19//!
20//! ```rust,no_run
21//! use mailtrap::Email;
22//!
23//! #[tokio::main]
24//! async fn main() {
25//!     let email = Email::new()
26//!         .from("sender@example.com")
27//!         .to("recipient@example.com")
28//!         .subject("Hello from Rust")
29//!         .text("This is a test email sent from Rust using the Mailtrap API.")
30//!         .category("test");
31//!
32//!     // send returns a Result<bool, Error>
33//!     let res = email.send(
34//!         None, // Use default API URL
35//!         Some("YOUR_API_TOKEN"),
36//!         None
37//!     ).await;
38//!
39//!     match res {
40//!         Ok(success) => {
41//!             if success {
42//!                 println!("Email sent successfully!");
43//!             } else {
44//!                 println!("Failed to send email.");
45//!             }
46//!         }
47//!         Err(e) => eprintln!("Error: {}", e),
48//!     }
49//! }
50//! ```
51//!
52//! ### Advanced Email (Attachments, HTML, Headers)
53//!
54//! ```rust,no_run
55//! use mailtrap::{Email, Attachment, ContentType, Disposition};
56//!
57//! #[tokio::main]
58//! async fn main() {
59//!     let attachment = Attachment::new()
60//!         .content("Attachment content".as_bytes().to_vec())
61//!         .filename("test.txt")
62//!         .content_type(ContentType::Plain)
63//!         .disposition(Disposition::Attachment);
64//!
65//!     let email = Email::new()
66//!         .from("sender@example.com")
67//!         .to("recipient@example.com")
68//!         .subject("Advanced Email Test")
69//!         .text("Plain text content")
70//!         .html("<h1>HTML Content</h1>")
71//!         .attachments(vec![attachment])
72//!         .header("X-Custom-Header", "Custom Value");
73//!
74//!     let res = email.send(None, Some("YOUR_API_TOKEN"), None).await;
75//!     
76//!     match res {
77//!         Ok(_) => println!("Advanced email sent!"),
78//!         Err(e) => eprintln!("Error: {}", e),
79//!     }
80//! }
81//! ```
82//!
83//! ### Batch Sending and Templates
84//!
85//! ```rust,no_run
86//! use mailtrap::{BatchEmail, BatchEmailRequest};
87//!
88//! #[tokio::main]
89//! async fn main() {
90//!     let request1 = BatchEmailRequest::new()
91//!         .to("recipient1@example.com")
92//!         .template_uuid("YOUR_TEMPLATE_UUID")
93//!         .template_variable("user_name", "Alice");
94//!
95//!     let request2 = BatchEmailRequest::new()
96//!         .to("recipient2@example.com")
97//!         .template_uuid("YOUR_TEMPLATE_UUID")
98//!         .template_variable("user_name", "Bob");
99//!
100//!     let batch_email = BatchEmail::new()
101//!         .from("sender@example.com")
102//!         .template_uuid("YOUR_TEMPLATE_UUID")
103//!         .request(request1)
104//!         .request(request2);
105//!
106//!     let res = batch_email.send(None, Some("YOUR_API_TOKEN"), None).await;
107//!     
108//!     match res {
109//!         Ok(_) => println!("Batch email sent!"),
110//!         Err(e) => eprintln!("Error: {}", e),
111//!     }
112//! }
113//! ```
114//!
115//! ### Account Access
116//!
117//! ```rust,no_run
118//! use mailtrap::Access;
119//!
120//! #[tokio::main]
121//! async fn main() {
122//!     let access = Access::new();
123//!     let account_id = "YOUR_ACCOUNT_ID";
124//!
125//!     // List access permissions
126//!     let res = access.list(
127//!         None,
128//!         Some("YOUR_API_TOKEN"),
129//!         None,
130//!         account_id,
131//!         None, None, None
132//!     ).await;
133//!     
134//!     match res {
135//!         Ok(records) => println!("Found {} access records", records.len()),
136//!         Err(e) => eprintln!("Error: {}", e),
137//!     }
138//! }
139//! ```
140//!
141//! ### Accounts
142//!
143//! ```rust,no_run
144//! use mailtrap::Accounts;
145//!
146//! #[tokio::main]
147//! async fn main() {
148//!     let accounts_client = Accounts::new();
149//!
150//!     // List available accounts
151//!     let res = accounts_client.list(
152//!         None,
153//!         Some("YOUR_API_TOKEN"),
154//!         None
155//!     ).await;
156//!
157//!     match res {
158//!         Ok(accounts) => {
159//!             for account in accounts {
160//!                 println!("Account: {}", account.name);
161//!             }
162//!         }
163//!         Err(e) => eprintln!("Error: {}", e),
164//!     }
165//! }
166//! ```
167
168pub mod email;
169pub use email::*;
170
171pub mod access;
172pub use access::*;
173
174pub mod accounts;
175pub use accounts::*;