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
//! This transport creates a file for each email, containing the enveloppe information and the email
//! itself.

use std::path::{Path, PathBuf};
use std::io::prelude::*;
use std::fs::File;

use transport::EmailTransport;
use transport::error::EmailResult;
use transport::smtp::response::Response;
use transport::smtp::response::{Category, Code, Severity};
use email::SendableEmail;

/// Writes the content and the enveloppe information to a file
pub struct FileEmailTransport {
    path: PathBuf,
}

impl FileEmailTransport {
    /// Creates a new transport to the given directory
    pub fn new<P: AsRef<Path>>(path: P) -> FileEmailTransport {
        let mut path_buf = PathBuf::new();
        path_buf.push(path);
        FileEmailTransport { path: path_buf }
    }
}

impl EmailTransport for FileEmailTransport {
    fn send<T: SendableEmail>(&mut self, email: T) -> EmailResult {
        let mut file = self.path.clone();
        file.push(format!("{}.txt", email.message_id()));

        let mut f = try!(File::create(file.as_path()));

        let log_line = format!("{}: from=<{}> to=<{}>\n",
                               email.message_id(),
                               email.from_address(),
                               email.to_addresses().join("> to=<"));

        try!(f.write_all(log_line.as_bytes()));
        try!(f.write_all(format!("{}", email.message()).as_bytes()));

        info!("{} status=<written>", log_line);

        Ok(Response::new(Code::new(Severity::PositiveCompletion, Category::MailSystem, 0),
                         vec![format!("Ok: email written to {}",
                                      file.to_str().unwrap_or("non-UTF-8 path"))]))
    }

    fn close(&mut self) {
        ()
    }
}