1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! The stub transport only logs message envelope and drops the content. It can be useful for
//! testing purposes.
//!
//! ```rust
//! use lettre::stub::StubEmailTransport;
//! use lettre::{SimpleSendableEmail, EmailTransport, EmailAddress};
//!
//! let email = SimpleSendableEmail::new(
//!                 EmailAddress::new("user@localhost".to_string()),
//!                 vec![EmailAddress::new("root@localhost".to_string())],
//!                 "message_id".to_string(),
//!                 "Hello world".to_string(),
//!             );
//!
//! let mut sender = StubEmailTransport::new_positive();
//! let result = sender.send(&email);
//! assert!(result.is_ok());
//! ```
//!
//! Will log (when using a logger like `env_logger`):
//!
//! ```text
//! b7c211bc-9811-45ce-8cd9-68eab575d695: from=<user@localhost> to=<root@localhost>
//! ```

use EmailTransport;
use SendableEmail;
use std::io::Read;

/// This transport logs the message envelope and returns the given response
#[derive(Debug)]
pub struct StubEmailTransport {
    response: StubResult,
}

impl StubEmailTransport {
    /// Creates a new transport that always returns the given response
    pub fn new(response: StubResult) -> StubEmailTransport {
        StubEmailTransport { response: response }
    }

    /// Creates a new transport that always returns a success response
    pub fn new_positive() -> StubEmailTransport {
        StubEmailTransport {
            response: Ok(()),
        }
    }
}

/// SMTP result type
pub type StubResult = Result<(), ()>;

impl<'a, T: Read + 'a> EmailTransport<'a, T, StubResult> for StubEmailTransport {
    fn send<U: SendableEmail<'a, T>>(&mut self, email: &'a U) -> StubResult {

        info!(
            "{}: from=<{}> to=<{:?}>",
            email.message_id(),
            email.from(),
            email.to()
        );
        self.response
    }
}