Add file transport

This commit is contained in:
Alexis Mousset
2015-10-25 17:47:07 +01:00
parent 7ac43b73c3
commit 62df24c5b1
10 changed files with 98 additions and 6 deletions

View File

@@ -0,0 +1,20 @@
extern crate lettre;
use lettre::transport::file::FileEmailTransport;
use lettre::transport::EmailTransport;
use lettre::email::EmailBuilder;
fn main() {
let mut sender = FileEmailTransport::new("/tmp/");
let email = EmailBuilder::new()
.to("root@localhost")
.from("user@localhost")
.body("Hello World!")
.subject("Hello")
.build()
.unwrap();
let result = sender.send(email);
println!("{:?}", result);
assert!(result.is_ok());
}

View File

@@ -151,7 +151,7 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate rustc_serialize as serialize; extern crate rustc_serialize;
extern crate crypto; extern crate crypto;
extern crate time; extern crate time;
extern crate uuid; extern crate uuid;

View File

@@ -6,7 +6,7 @@ use std::fmt::{Display, Formatter};
use std::fmt; use std::fmt;
use transport::smtp::response::{Severity, Response}; use transport::smtp::response::{Severity, Response};
use serialize::base64::FromBase64Error; use rustc_serialize::base64::FromBase64Error;
use self::Error::*; use self::Error::*;
/// An enum of all error kinds. /// An enum of all error kinds.

52
src/transport/file/mod.rs Normal file
View File

@@ -0,0 +1,52 @@
//! TODO
use std::path::{Path, PathBuf};
use std::io::prelude::*;
use std::fs::File;
use transport::error::EmailResult;
use transport::smtp::response::Response;
use transport::EmailTransport;
use transport::smtp::response::{Code, Category, Severity};
use email::SendableEmail;
/// TODO
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) {
()
}
}

View File

@@ -2,7 +2,7 @@
pub mod smtp; pub mod smtp;
pub mod error; pub mod error;
pub mod stub; pub mod stub;
// pub mod file; pub mod file;
use transport::error::EmailResult; use transport::error::EmailResult;
use email::SendableEmail; use email::SendableEmail;

View File

@@ -3,8 +3,8 @@
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::fmt; use std::fmt;
use serialize::base64::{self, ToBase64, FromBase64}; use rustc_serialize::base64::{self, ToBase64, FromBase64};
use serialize::hex::ToHex; use rustc_serialize::hex::ToHex;
use crypto::hmac::Hmac; use crypto::hmac::Hmac;
use crypto::md5::Md5; use crypto::md5::Md5;
use crypto::mac::Mac; use crypto::mac::Mac;

View File

@@ -13,7 +13,7 @@ pub struct StubEmailTransport;
impl EmailTransport for StubEmailTransport { impl EmailTransport for StubEmailTransport {
fn send<T: SendableEmail>(&mut self, email: T) -> EmailResult { fn send<T: SendableEmail>(&mut self, email: T) -> EmailResult {
info!("message '{}': from '{}' to '{:?}'", info!("{}: from=<{}> to=<{:?}>",
email.message_id(), email.message_id(),
email.from_address(), email.from_address(),
email.to_addresses()); email.to_addresses());

View File

@@ -2,3 +2,4 @@ extern crate lettre;
mod transport_smtp; mod transport_smtp;
mod transport_stub; mod transport_stub;
mod transport_file;

19
tests/transport_file.rs Normal file
View File

@@ -0,0 +1,19 @@
extern crate lettre;
use lettre::transport::file::FileEmailTransport;
use lettre::transport::EmailTransport;
use lettre::email::EmailBuilder;
#[test]
fn file_transport() {
let mut sender = FileEmailTransport::new("/tmp/");
let email = EmailBuilder::new()
.to("root@localhost")
.from("user@localhost")
.body("Hello World!")
.subject("Hello")
.build()
.unwrap();
let result = sender.send(email);
assert!(result.is_ok());
}