From 644b1e59b06704822c6a540c12ed44b6e827d221 Mon Sep 17 00:00:00 2001 From: tyranron Date: Sun, 8 Jul 2018 09:42:58 +0300 Subject: [PATCH] feat(transport-smtp): Upgrade to 0.2 version of native-tls crate --- lettre/Cargo.toml | 2 +- lettre/src/smtp/mod.rs | 4 +- website/content/sending-messages/smtp.md | 51 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lettre/Cargo.toml b/lettre/Cargo.toml index b1c3f65..8bf714a 100644 --- a/lettre/Cargo.toml +++ b/lettre/Cargo.toml @@ -22,7 +22,7 @@ is-it-maintained-open-issues = { repository = "lettre/lettre" } log = "^0.4" nom = { version = "^3.2", optional = true } bufstream = { version = "^0.1", optional = true } -native-tls = { version = "^0.1", optional = true } +native-tls = { version = "^0.2", optional = true } base64 = { version = "^0.9", optional = true } hostname = { version = "^0.1", optional = true } serde = { version = "^1.0", optional = true } diff --git a/lettre/src/smtp/mod.rs b/lettre/src/smtp/mod.rs index 63244cf..78bb695 100644 --- a/lettre/src/smtp/mod.rs +++ b/lettre/src/smtp/mod.rs @@ -125,8 +125,8 @@ impl SmtpClient { /// Creates an encrypted transport over submissions port, using the provided domain /// to validate TLS certificates. pub fn new_simple(domain: &str) -> Result { - let mut tls_builder = TlsConnector::builder()?; - tls_builder.supported_protocols(DEFAULT_TLS_PROTOCOLS)?; + let mut tls_builder = TlsConnector::builder(); + tls_builder.min_protocol_version(Some(DEFAULT_TLS_PROTOCOLS[0])); let tls_parameters = ClientTlsParameters::new(domain.to_string(), tls_builder.build().unwrap()); diff --git a/website/content/sending-messages/smtp.md b/website/content/sending-messages/smtp.md index 9adb7ef..ffa14bc 100644 --- a/website/content/sending-messages/smtp.md +++ b/website/content/sending-messages/smtp.md @@ -96,6 +96,57 @@ fn main() { } ``` +You can specify custom TLS settings: + +```rust,no_run +extern crate native_tls; +extern crate lettre; +extern crate lettre_email; + +use lettre::{ + ClientSecurity, ClientTlsParameters, EmailAddress, Envelope, + SendableEmail, SmtpClient, Transport, +}; +use lettre::smtp::authentication::{Credentials, Mechanism}; +use lettre::smtp::ConnectionReuseParameters; +use native_tls::{Protocol, TlsConnector}; + +fn main() { + let email = SendableEmail::new( + Envelope::new( + Some(EmailAddress::new("user@localhost".to_string()).unwrap()), + vec![EmailAddress::new("root@localhost".to_string()).unwrap()], + ).unwrap(), + "message_id".to_string(), + "Hello world".to_string().into_bytes(), + ); + + let mut tls_builder = TlsConnector::builder(); + tls_builder.min_protocol_version(Some(Protocol::Tlsv10)); + let tls_parameters = + ClientTlsParameters::new( + "smtp.example.com".to_string(), + tls_builder.build().unwrap() + ); + + let mut mailer = SmtpClient::new( + ("smtp.example.com", 465), ClientSecurity::Wrapper(tls_parameters) + ).unwrap() + .authentication_mechanism(Mechanism::Login) + .credentials(Credentials::new( + "example_username".to_string(), "example_password".to_string() + )) + .connection_reuse(ConnectionReuseParameters::ReuseUnlimited) + .transport(); + + let result = mailer.send(email); + + assert!(result.is_ok()); + + mailer.close(); +} +``` + #### Lower level You can also send commands, here is a simple email transaction without