Repository: lettre/lettre (language: rust)

Issue:
Expected struct `lettre::SendableEmail`, found struct `lettre_email::Email`
Hi, I'm getting this error message when I try to send an email.

How do I send a lettre_email::Email ? Can I convert it to a SendableEmail?

I believe I need to build my email and SMTPTransport in this way in order to add the security credentials and mail information such as subject, body, etc. ? I tried just using the example code, and it's broken too :(  
The example code gives me these errors:
 - no function or associated item named `builder_unencrypted_localhost` found for type `lettre::SmtpTransport` in the current scope
 - expected struct `lettre::SendableEmail`, found struct `lettre_email::Email`


3 snippets of code that related to this error are included below. Thanks for the help!


```
let mut mailer = SmtpClient::new(("email-smtp.us-east-1.amazonaws.com", 25), ClientSecurity::Required(tls_parameters))
        .unwrap()
        .hello_name(ClientId::Domain("localhost".to_string()))
        .credentials(Credentials::new("<Insert username>".to_string(), "<Insert Password>".to_string()))
        .smtp_utf8(true)
        .authentication_mechanism(Mechanism::Plain)
        .connection_reuse(ConnectionReuseParameters::ReuseUnlimited)
        .transport();
```

```
  let mailbody =  get_mailbody(email.clone());

  let new_email = EmailBuilder::new()
                .to(email.email.as_str())
                .from("info@frink.global")
                .body(&mailbody)
                .subject(subject)
                .build()
                .unwrap();
```

```
if let Err(e) = mailer.send(new_email) {
                log_info(&APILOG,
                    format!("ERROR - could not Send Mail: {:?}", e));
                println!("{:?}", e);
            }
```



Discussion / hints:
Found the bug myself:
instead of mailer.send(new_email)  

Need to use ".into()":
mailer.send(new_email.into()) 

Note this is a bug in lettre itself. Ideally I'd like to see you have a ".send()" function that allows email and auto-converts, instead of requiring the user to convert it.

But if not, please fix the example at least.

Fix patch:
diff --git a/README.md b/README.md
index 8d39568af..405cfae31 100644
--- a/README.md
+++ b/README.md
@@ -67,7 +67,7 @@ fn main() {
     // Open a local connection on port 25
     let mut mailer = SmtpClient::new_unencrypted_localhost().unwrap().transport();
     // Send the email
-    let result = mailer.send(email.into());
+    let result = mailer.send(email);
 
     if result.is_ok() {
         println!("Email sent");
diff --git a/lettre/src/file/mod.rs b/lettre/src/file/mod.rs
index 327eb2bb9..8dcad546e 100644
--- a/lettre/src/file/mod.rs
+++ b/lettre/src/file/mod.rs
@@ -47,7 +47,9 @@ struct SerializableEmail {
 impl<'a> Transport<'a> for FileTransport {
     type Result = FileResult;
 
-    fn send(&mut self, email: SendableEmail) -> FileResult {
+    fn send<E: Into<SendableEmail>>(&mut self, email: E) -> FileResult {
+        let email = email.into();
+
         let message_id = email.message_id().to_string();
         let envelope = email.envelope().clone();
 
diff --git a/lettre/src/lib.rs b/lettre/src/lib.rs
index c565a079c..32d3318fb 100644
--- a/lettre/src/lib.rs
+++ b/lettre/src/lib.rs
@@ -192,5 +192,5 @@ pub trait Transport<'a> {
     type Result;
 
     /// Sends the email
-    fn send(&mut self, email: SendableEmail) -> Self::Result;
+    fn send<E: Into<SendableEmail>>(&mut self, email: E) -> Self::Result;
 }
diff --git a/lettre/src/sendmail/mod.rs b/lettre/src/sendmail/mod.rs
index 751abebd4..402c14611 100644
--- a/lettre/src/sendmail/mod.rs
+++ b/lettre/src/sendmail/mod.rs
@@ -41,7 +41,9 @@ impl SendmailTransport {
 impl<'a> Transport<'a> for SendmailTransport {
     type Result = SendmailResult;
 
-    fn send(&mut self, email: SendableEmail) -> SendmailResult {
+    fn send<E: Into<SendableEmail>>(&mut self, email: E) -> SendmailResult {
+        let email = email.into();
+
         let message_id = email.message_id().to_string();
 
         // Spawn the sendmail command
diff --git a/lettre/src/smtp/mod.rs b/lettre/src/smtp/mod.rs
index f8c782dc8..298b0d6a1 100644
--- a/lettre/src/smtp/mod.rs
+++ b/lettre/src/smtp/mod.rs
@@ -407,7 +407,9 @@ impl<'a> Transport<'a> for SmtpTransport {
         feature = "cargo-clippy",
         allow(clippy::match_same_arms, clippy::cyclomatic_complexity)
     )]
-    fn send(&mut self, email: SendableEmail) -> SmtpResult {
+    fn send<E: Into<SendableEmail>>(&mut self, email: E) -> SmtpResult {
+        let email = email.into();
+
         let message_id = email.message_id().to_string();
 
         if !self.client.is_connected() {
diff --git a/lettre/src/stub/mod.rs b/lettre/src/stub/mod.rs
index aae26ac5d..217973c41 100644
--- a/lettre/src/stub/mod.rs
+++ b/lettre/src/stub/mod.rs
@@ -30,7 +30,9 @@ pub type StubResult = Result<(), ()>;
 impl<'a> Transport<'a> for StubTransport {
     type Result = StubResult;
 
-    fn send(&mut self, email: SendableEmail) -> StubResult {
+    fn send<E: Into<SendableEmail>>(&mut self, email: E) -> StubResult {
+        let email = email.into();
+
         info!(
             "{}: from=<{}> to=<{:?}>",
             email.message_id(),
diff --git a/lettre_email/examples/local.rs b/lettre_email/examples/local.rs
index d6140280b..c7ebde042 100644
--- a/lettre_email/examples/local.rs
+++ b/lettre_email/examples/local.rs
@@ -22,7 +22,7 @@ fn main() {
     // Open a local connection on port 25
     let mut mailer = SmtpClient::new_unencrypted_localhost().unwrap().transport();
     // Send the email
-    let result = mailer.send(email.into());
+    let result = mailer.send(email);
 
     if result.is_ok() {
         println!("Email sent");


Test patch: