diff --git a/Cargo.toml b/Cargo.toml index 0a8a98f..221efba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,5 +71,9 @@ name = "smtp" required-features = ["smtp-transport"] [[example]] -name = "smtp_gmail" +name = "smtp_tls" +required-features = ["smtp-transport", "native-tls"] + +[[example]] +name = "smtp_starttls" required-features = ["smtp-transport", "native-tls"] diff --git a/examples/smtp_starttls.rs b/examples/smtp_starttls.rs new file mode 100644 index 0000000..b4bd71a --- /dev/null +++ b/examples/smtp_starttls.rs @@ -0,0 +1,26 @@ +use lettre::transport::smtp::authentication::Credentials; +use lettre::{Message, SmtpTransport, Transport}; + +fn main() { + let email = Message::builder() + .from("NoBody ".parse().unwrap()) + .reply_to("Yuin ".parse().unwrap()) + .to("Hei ".parse().unwrap()) + .subject("Happy new year") + .body("Be happy!") + .unwrap(); + + let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); + + // Open a remote connection to gmail using STARTTLS + let mailer = SmtpTransport::starttls_relay("smtp.gmail.com") + .unwrap() + .credentials(creds) + .build(); + + // Send the email + match mailer.send(&email) { + Ok(_) => println!("Email sent successfully!"), + Err(e) => panic!("Could not send email: {:?}", e), + } +} diff --git a/examples/smtp_gmail.rs b/examples/smtp_tls.rs similarity index 100% rename from examples/smtp_gmail.rs rename to examples/smtp_tls.rs diff --git a/src/transport/smtp/transport.rs b/src/transport/smtp/transport.rs index 7d5fe00..66006fe 100644 --- a/src/transport/smtp/transport.rs +++ b/src/transport/smtp/transport.rs @@ -5,7 +5,7 @@ use r2d2::{Builder, Pool}; use super::{ClientId, Credentials, Error, Mechanism, Response, SmtpConnection, SmtpInfo}; #[cfg(any(feature = "native-tls", feature = "rustls-tls"))] -use super::{Tls, TlsParameters, SUBMISSIONS_PORT}; +use super::{Tls, TlsParameters, SUBMISSIONS_PORT, SUBMISSION_PORT}; use crate::{Envelope, Transport}; #[allow(missing_debug_implementations)] @@ -50,6 +50,19 @@ impl SmtpTransport { .tls(Tls::Wrapper(tls_parameters))) } + /// Simple and secure transport, should be used when the server doesn't support wrapped TLS connections. + /// Creates an encrypted transport over submissions port, by first connecting using an unencrypted + /// connection and then upgrading it with STARTTLS, using the provided domain to validate TLS certificates. + /// If the connection can't be upgraded it will fail connecting altogether. + #[cfg(any(feature = "native-tls", feature = "rustls-tls"))] + pub fn starttls_relay(relay: &str) -> Result { + let tls_parameters = TlsParameters::new(relay.into())?; + + Ok(Self::builder_dangerous(relay) + .port(SUBMISSION_PORT) + .tls(Tls::Required(tls_parameters))) + } + /// Creates a new local SMTP client to port 25 /// /// Shortcut for local unencrypted relay (typical local email daemon that will handle relaying)