From 662072e692c7b662380f30c876f28eb860f2c4af Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sat, 10 Mar 2018 21:16:01 +0100 Subject: [PATCH] fix(builder): Use parts for text and html methods to fix attachment inclusion --- README.md | 3 --- lettre/rustfmt.toml | 1 - lettre_email/rustfmt.toml | 1 - lettre_email/src/lib.rs | 55 ++++++++++----------------------------- 4 files changed, 14 insertions(+), 46 deletions(-) delete mode 120000 lettre/rustfmt.toml delete mode 120000 lettre_email/rustfmt.toml diff --git a/README.md b/README.md index 47b2131..6bc77d2 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,6 @@ Lettre provides the following features: ## Example -See https://github.com/lettre/lettre/blob/master/lettre_email/examples/smtp.rs -for a simple example of how to build and send an email. - ```rust,no_run extern crate lettre; extern crate lettre_email; diff --git a/lettre/rustfmt.toml b/lettre/rustfmt.toml deleted file mode 120000 index 39f97b0..0000000 --- a/lettre/rustfmt.toml +++ /dev/null @@ -1 +0,0 @@ -../rustfmt.toml \ No newline at end of file diff --git a/lettre_email/rustfmt.toml b/lettre_email/rustfmt.toml deleted file mode 120000 index 39f97b0..0000000 --- a/lettre_email/rustfmt.toml +++ /dev/null @@ -1 +0,0 @@ -../rustfmt.toml \ No newline at end of file diff --git a/lettre_email/src/lib.rs b/lettre_email/src/lib.rs index 8d3b7df..c170e1c 100644 --- a/lettre_email/src/lib.rs +++ b/lettre_email/src/lib.rs @@ -642,11 +642,14 @@ impl EmailBuilder { /// Sets the email body to plain text content pub fn set_text>(&mut self, body: S) { - self.message.set_body(body); - self.message.add_header(( - "Content-Type", - format!("{}", mime::TEXT_PLAIN_UTF_8).as_ref(), - )); + let text = PartBuilder::new() + .body(body) + .header(( + "Content-Type", + format!("{}", mime::TEXT_PLAIN_UTF_8).as_ref(), + )) + .build(); + self.add_child(text); } /// Sets the email body to HTML content @@ -657,9 +660,11 @@ impl EmailBuilder { /// Sets the email body to HTML content pub fn set_html>(&mut self, body: S) { - self.message.set_body(body); - self.message - .add_header(("Content-Type", format!("{}", mime::TEXT_HTML).as_ref())); + let html = PartBuilder::new() + .body(body) + .header(("Content-Type", format!("{}", mime::TEXT_HTML).as_ref())) + .build(); + self.add_child(html); } /// Sets the email content @@ -881,42 +886,10 @@ pub trait ExtractableEmail { #[cfg(test)] mod test { - - use super::{EmailBuilder, IntoEmail, SimpleEmail}; + use super::EmailBuilder; use lettre::{EmailAddress, SendableEmail}; use time::now; - #[test] - fn test_simple_email_builder() { - let email_builder = SimpleEmail::default(); - let date_now = now(); - - let email = email_builder - .to("user@localhost") - .from("user@localhost") - .cc(("cc@localhost", "Alias")) - .reply_to("reply@localhost") - .text("Hello World!") - .date(date_now.clone()) - .subject("Hello") - .header(("X-test", "value")) - .into_email() - .unwrap(); - - assert_eq!( - format!("{}", String::from_utf8_lossy(email.message().as_ref())), - format!( - "Subject: Hello\r\nContent-Type: text/plain; \ - charset=utf-8\r\nX-test: value\r\nTo: \r\nFrom: \ - \r\nCc: \"Alias\" \r\nReply-To: \ - \r\nDate: {}\r\nMIME-Version: 1.0\r\nMessage-ID: \ - <{}.lettre@localhost>\r\n\r\nHello World!\r\n", - date_now.rfc822z(), - email.message_id() - ) - ); - } - #[test] fn test_multiple_from() { let email_builder = EmailBuilder::new();