feat(transport): Add the ability to save envelope as JSON in file transport

This will make it possible to read the message and send it later
properly.
This commit is contained in:
Alexis Mousset
2020-11-25 19:48:20 +01:00
committed by Alexis Mousset
parent 3683d122ba
commit 3f03b6296b
5 changed files with 140 additions and 38 deletions

View File

@@ -4,8 +4,7 @@ mod test {
use lettre::{transport::file::FileTransport, Message};
use std::{
env::temp_dir,
fs::{remove_file, File},
io::Read,
fs::{read_to_string, remove_file},
};
#[cfg(feature = "tokio02")]
@@ -27,15 +26,48 @@ mod test {
let result = sender.send(&email);
let id = result.unwrap();
let file = temp_dir().join(format!("{}.eml", id));
let mut f = File::open(file.clone()).unwrap();
let mut buffer = String::new();
let _ = f.read_to_string(&mut buffer);
let eml_file = temp_dir().join(format!("{}.eml", id));
let eml = read_to_string(&eml_file).unwrap();
assert_eq!(
buffer,
eml,
"From: NoBody <nobody@domain.tld>\r\nReply-To: Yuin <yuin@domain.tld>\r\nTo: Hei <hei@domain.tld>\r\nSubject: Happy new year\r\nDate: Tue, 15 Nov 1994 08:12:31 GMT\r\n\r\nBe happy!");
remove_file(file).unwrap();
remove_file(eml_file).unwrap();
}
#[test]
#[cfg(feature = "file-transport-envelope")]
fn file_transport_with_envelope() {
use lettre::Transport;
let sender = FileTransport::with_envelope(temp_dir());
let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year")
.date("Tue, 15 Nov 1994 08:12:31 GMT".parse().unwrap())
.body("Be happy!")
.unwrap();
let result = sender.send(&email);
let id = result.unwrap();
let eml_file = temp_dir().join(format!("{}.eml", id));
let eml = read_to_string(&eml_file).unwrap();
let json_file = temp_dir().join(format!("{}.json", id));
let json = read_to_string(&json_file).unwrap();
assert_eq!(
eml,
"From: NoBody <nobody@domain.tld>\r\nReply-To: Yuin <yuin@domain.tld>\r\nTo: Hei <hei@domain.tld>\r\nSubject: Happy new year\r\nDate: Tue, 15 Nov 1994 08:12:31 GMT\r\n\r\nBe happy!");
remove_file(eml_file).unwrap();
assert_eq!(
json,
"{\"forward_path\":[\"hei@domain.tld\"],\"reverse_path\":\"nobody@domain.tld\"}"
);
remove_file(json_file).unwrap();
}
#[cfg(feature = "async-std1")]
@@ -56,15 +88,13 @@ mod test {
let result = sender.send(email).await;
let id = result.unwrap();
let file = temp_dir().join(format!("{}.eml", id));
let mut f = File::open(file.clone()).unwrap();
let mut buffer = String::new();
let _ = f.read_to_string(&mut buffer);
let eml_file = temp_dir().join(format!("{}.eml", id));
let eml = read_to_string(&eml_file).unwrap();
assert_eq!(
buffer,
eml,
"From: NoBody <nobody@domain.tld>\r\nReply-To: Yuin <yuin@domain.tld>\r\nTo: Hei <hei@domain.tld>\r\nSubject: Happy new year\r\nDate: Tue, 15 Nov 1994 08:12:31 GMT\r\n\r\nBe happy!");
remove_file(file).unwrap();
remove_file(eml_file).unwrap();
}
#[cfg(feature = "tokio02")]
@@ -85,14 +115,12 @@ mod test {
let result = sender.send(email).await;
let id = result.unwrap();
let file = temp_dir().join(format!("{}.eml", id));
let mut f = File::open(file.clone()).unwrap();
let mut buffer = String::new();
let _ = f.read_to_string(&mut buffer);
let eml_file = temp_dir().join(format!("{}.eml", id));
let eml = read_to_string(&eml_file).unwrap();
assert_eq!(
buffer,
eml,
"From: NoBody <nobody@domain.tld>\r\nReply-To: Yuin <yuin@domain.tld>\r\nTo: Hei <hei@domain.tld>\r\nSubject: Happy new year\r\nDate: Tue, 15 Nov 1994 08:12:31 GMT\r\n\r\nBe happy!");
remove_file(file).unwrap();
remove_file(eml_file).unwrap();
}
}