rust_smtp_server/
backend.rs

1use crate::data::SMTPError;
2
3use async_trait::async_trait;
4
5use anyhow::{
6    anyhow,
7    Result,
8};
9
10use tokio::io::AsyncRead;
11
12type BodyType = String;
13
14//const BODY_7BIT: BodyType = "7BIT".to_owned();
15//const BODY_8BIT_MIME: BodyType = "8BITMIME".to_string();
16//const BODY_BINARY_MIME: BodyType = "BINARYMIME".to_string();
17
18pub trait Backend: Send + Sync + 'static {
19    type S: Session + Send;
20
21    fn new_session(&self) -> Result<Self::S>;
22}
23
24pub struct MailOptions {
25    pub body: BodyType,
26    pub size: usize,
27    pub require_tls: bool,
28    pub utf8: bool,
29    pub auth: String,
30}
31
32impl MailOptions {
33    pub fn new() -> Self {
34        MailOptions {
35            body: "7BIT".to_string(),
36            size: 0,
37            require_tls: false,
38            utf8: false,
39            auth: String::new(),
40        }
41    }
42}
43
44#[async_trait]
45pub trait Session {
46    fn reset(&mut self);
47
48    fn logout(&mut self) -> Result<()>;
49
50    fn auth_plain(&mut self, username: &str, password: &str) -> Result<()> {
51        Err(anyhow!(SMTPError::err_auth_unsupported().error()))
52    }
53
54    async fn mail(&mut self, from: &str, opts: &MailOptions) -> Result<()>;
55
56    async fn rcpt(&mut self, to: &str) -> Result<()>;
57
58    async fn data<R: AsyncRead + Send + Unpin>(&mut self, r: R) -> Result<()>;
59}