Crates.io | email-service |
lib.rs | email-service |
version | 0.2.0 |
created_at | 2025-06-16 17:47:00.922712+00 |
updated_at | 2025-06-17 06:22:24.312607+00 |
description | An easy way to configure simple mail sender |
homepage | |
repository | https://gitlab.com/dshamany/rust-email-service.git |
max_upload_size | |
id | 1714569 |
size | 49,982 |
This crate provides a simple email sending functionality for Rust applications. It uses the lettre
library to handle SMTP communication.
EmailClient
struct for easy email sending.Add this crate as a dependency to your Cargo.toml
file:
[dependencies]
email-service = { version = "0.1.0" } # Replace with the actual version
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.Use the EmailClient
in your code:
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(())
}