smtpd

Crates.iosmtpd
lib.rssmtpd
version0.1.0
created_at2025-10-05 17:08:49.579861+00
updated_at2025-10-05 17:08:49.579861+00
descriptionAsynchronous, extensible SMTP server library built on Tokio
homepagehttps://github.com/uksarkar/smtpd_rs
repositoryhttps://github.com/uksarkar/smtpd_rs
max_upload_size
id1869241
size152,631
Utpal Sarkar (uksarkar)

documentation

README

smtpd

⚠️ BETA — Use with caution, API may change

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 (feature native-tls-backend) or rustls (feature rustls-backend) backend.
  • Customizable handlers for AUTH, RCPT, and DATA commands via the Handler struct's [SmtpHandler::handle_auth], [SmtpHandler::handle_email], and [SmtpHandler::handle_rcpt] method
  • 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.
Commit count: 0

cargo fmt