# Email Service Crate
This crate provides a simple email sending functionality for Rust applications. It uses the `lettre` library to handle SMTP communication.
## Features
* Sends emails via SMTP.
* Uses environment variables for SMTP configuration.
* Provides an `EmailClient` struct for easy email sending.
## Usage
1. Add this crate as a dependency to your `Cargo.toml` file:
```toml
[dependencies]
email-service = { version = "0.1.0" } # Replace with the actual version
```
2. Configure environment variables:
* `SMTP_HOST`: The SMTP host address (e.g., `smtp.example.com`).
* `SMTP_PORT`: The SMTP port number (e.g., `587`).
* `SMTP_USERNAME`: The SMTP username.
* `SMTP_PASSWORD`: The SMTP password.
3. Use the `EmailClient` in your code:
```rust
use email_service::{EmailClient, EmailClientConfig};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv::dotenv().ok();
let smtp_host = env::var("SMTP_HOST").expect("SMTP_HOST is missing");
let smtp_port = env::var("SMTP_PORT")
.expect("SMTP_PORT is missing")
.parse::<u16>()?;
let smtp_username = env::var("SMTP_USERNAME").expect("SMTP_USERNAME is missing");
let smtp_password = env::var("SMTP_PASSWORD").expect("SMTP_PASSWORD is missing");
let config = EmailClientConfig {
smtp_host,
smtp_port,
smtp_username,
smtp_password,
};
let email_client = EmailClient::new(config);
let recipient = "recipient@example.com".to_string();
let sender = "sender@example.com".to_string();
let subject = "Test Email".to_string();
let body = "Hello, this is a test email!".to_string();
email_client.send_email(recipient, sender, subject, body).await?;
println!("Email sent successfully!");
Ok(())
}
```
## License
[MIT](LICENSE)