Expand description
§smtpd
smtpd is an asynchronous, extensible SMTP server library built on top of tokio.
Inspired by smtpd.
It provides flexible support for authentication, TLS, and custom handling
of SMTP commands.
§Features
- Asynchronous SMTP server using Tokio.
- Supports authentication mechanisms:
AuthMach::Plain,AuthMach::Login,AuthMach::CramMD5. - TLS support (STARTTLS and implicit TLS) with optional
native-tls(featurenative-tls-backend) orrustls(featurerustls-backend) backend. - Customizable handlers for AUTH, RCPT, and DATA commands via the Handler struct’s
SmtpHandler::handle_auth,SmtpHandler::handle_email, andSmtpHandler::handle_rcptmethod - Configurable limits for message size and recipients.
§Example
use smtpd::{async_trait, start_server, SmtpConfig, AuthMach};
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let config = SmtpConfig {
bind_addr: "127.0.0.1:2525".to_string(),
require_auth: true,
auth_machs: vec![AuthMach::Plain, AuthMach::Login],
..Default::default()
};
let factory = MyHandlerFactory {};
start_server(config, factory).await?;
Ok(())
}
struct MyHandler {}
#[async_trait]
impl smtpd::SmtpHandler for MyHandler {
async fn handle_auth(
&mut self,
_session: &smtpd::Session,
data: smtpd::AuthData,
) -> Result<smtpd::Response, smtpd::Error> {
let (username, password, _) = data.data();
if username == "abc" && password == "efg" {
return Ok(smtpd::Response::Default);
}
Err(smtpd::Error::Abort)
}
async fn handle_rcpt(
&mut self,
_session: &smtpd::Session,
to: &str,
) -> Result<smtpd::Response, smtpd::Error> {
// allow recipients only from gmail
if to.ends_with("gmail.com") {
return Ok(smtpd::Response::Default);
}
Err(smtpd::Error::Abort)
}
}
struct MyHandlerFactory;
impl smtpd::SmtpHandlerFactory for MyHandlerFactory {
type Handler = MyHandler;
fn new_handler(&self, _session: &smtpd::Session) -> Self::Handler {
MyHandler {}
}
}§Modules
session: Represents a client session.handler: Defines traits for handling SMTP commands.tls: TLS configuration and provider utilities.stream: Connection stream abstraction with read/write helpers.utils: Parsing and helper functions for SMTP commands.
Structs§
- Session
- Represents an active SMTP session.
- Smtp
Config - SMTP Server Configuration
- Smtp
Server - The core SMTP server struct.
Enums§
- Auth
Data - Represents the parsed authentication data provided by the client.
- Auth
Mach - Represents the supported SMTP authentication mechanisms.
- Error
- Error for handler operations.
- Response
- Represents an SMTP response that can be written to the client stream.
- TlsConfig
- Represents the TLS configuration for the SMTP server.
- TlsMode
- Specifies the TLS mode for the SMTP server.
Traits§
- Smtp
Handler - Defines the async hook interface for handling key SMTP events:
authentication (
AUTH), recipient validation (RCPT TO), and message delivery (DATA). - Smtp
Handler Factory - Defines how new
SmtpHandlerinstances are created per session.
Functions§
- start_
server - Starts an SMTP server with the provided configuration and handler factory.
Type Aliases§
- Result
- Convenient alias for handler results