# Mailtrap
An unofficial Rust library for interacting with the [Mailtrap](https://mailtrap.io) API. This library allows you to create and send emails programmatically using Mailtrap's sending API.
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
mailtrap = "0.1.0"
tokio = { version = "1", features = ["full"] }
```
## Usage
Here's a simple example of how to send an email:
```rust,no_run
use mailtrap::Email;
#[tokio::main]
async fn main() {
let email = Email::new()
.from("sender@example.com")
.to("recipient@example.com")
.subject("Hello from Rust")
.text("This is a test email sent from Rust using the Mailtrap API.")
.category("test");
// send returns a Result<bool, Error>
// You need to provide the API endpoint and your API token.
// The API endpoint is usually "https://send.api.mailtrap.io/" for production.
let res = email.send(
"https://send.api.mailtrap.io/",
Some("YOUR_API_TOKEN"),
None
).await;
match res {
Ok(success) => {
if success {
println!("Email sent successfully!");
} else {
println!("Failed to send email.");
}
}
Err(e) => eprintln!("Error: {}", e),
}
}
```
## Advanced Usage
You can also send emails with HTML content, attachments, and custom headers.
```rust,no_run
use mailtrap::{Email, Attachment, Header, ContentType, Disposition};
#[tokio::main]
async fn main() {
// Create an attachment
let attachment = Attachment::new()
.content("Attachment content".as_bytes().to_vec())
.filename("test.txt")
.content_type(ContentType::Plain)
.disposition(Disposition::Attachment);
let email = Email::new()
.from("\"Sender Name\" <sender@example.com>")
.to("recipient@example.com")
.cc("cc@example.com")
.bcc("bcc@example.com")
.reply_to("reply@example.com")
.subject("Advanced Email Test")
.text("Plain text content")
.html("<h1>HTML Content</h1><p>This is an email with HTML.</p>")
.attachments(vec![attachment])
.header("X-Custom-Header", "Custom Value")
.category("integration-test");
let res = email.send(
"https://send.api.mailtrap.io/",
Some("YOUR_API_TOKEN"),
None
).await;
// handle response...
}
```
## Features
- **Simple Builder API**: Create emails with a fluent interface.
- **Multiple Content Types**: Support for both plain text and HTML content.
- **Attachments**: Easy support for file attachments with customizable content type and disposition.
- **Custom Headers**: Add any custom headers to your emails.
- **Categories**: Tag your emails with categories for analytics.
- **Async/Await**: Built on `reqwest` and `tokio` for non-blocking I/O.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.