Crate smtpd

Crate smtpd 

Source
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

§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.
SmtpConfig
SMTP Server Configuration
SmtpServer
The core SMTP server struct.

Enums§

AuthData
Represents the parsed authentication data provided by the client.
AuthMach
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§

SmtpHandler
Defines the async hook interface for handling key SMTP events: authentication (AUTH), recipient validation (RCPT TO), and message delivery (DATA).
SmtpHandlerFactory
Defines how new SmtpHandler instances 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

Attribute Macros§

async_trait