]> Untitled Git - lemmy.git/commitdiff
Adding starttls support. Fixes #1997 (#2051)
authorDessalines <dessalines@users.noreply.github.com>
Wed, 26 Jan 2022 16:42:43 +0000 (11:42 -0500)
committerGitHub <noreply@github.com>
Wed, 26 Jan 2022 16:42:43 +0000 (16:42 +0000)
* Adding starttls support. Fixes #1997

* Change name to tls_type, make a string

config/defaults.hjson
crates/utils/src/email.rs
crates/utils/src/settings/structs.rs

index 663aa4b57e9eeee00799261b661c9633e4a24522..bdf19142e2687b7572e3a986a5ed90511b4ccca2 100644 (file)
@@ -76,8 +76,8 @@
     smtp_password: "string"
     # Address to send emails from, eg noreply@your-instance.com
     smtp_from_address: "noreply@example.com"
     smtp_password: "string"
     # Address to send emails from, eg noreply@your-instance.com
     smtp_from_address: "noreply@example.com"
-    # Whether or not smtp connections should use tls
-    use_tls: true
+    # Whether or not smtp connections should use tls. Can be none, tls, or starttls
+    tls_type: "none"
   }
   # Parameters for automatic configuration of new instance (only used at first start)
   setup: {
   }
   # Parameters for automatic configuration of new instance (only used at first start)
   setup: {
index d9ac1710d86762f8e2760b2b376e702611b0fdac..69c0fb17b5c523e36448b565418b55f003fc14d1 100644 (file)
@@ -1,11 +1,7 @@
 use crate::{settings::structs::Settings, LemmyError};
 use lettre::{
   message::{header, Mailbox, MultiPart, SinglePart},
 use crate::{settings::structs::Settings, LemmyError};
 use lettre::{
   message::{header, Mailbox, MultiPart, SinglePart},
-  transport::smtp::{
-    authentication::Credentials,
-    client::{Tls, TlsParameters},
-    extension::ClientId,
-  },
+  transport::smtp::{authentication::Credentials, extension::ClientId},
   Address,
   Message,
   SmtpTransport,
   Address,
   Message,
   SmtpTransport,
@@ -77,13 +73,15 @@ pub fn send_email(
 
   // don't worry about 'dangeous'. it's just that leaving it at the default configuration
   // is bad.
 
   // don't worry about 'dangeous'. it's just that leaving it at the default configuration
   // is bad.
-  let mut builder = SmtpTransport::builder_dangerous(smtp_server).port(smtp_port);
 
   // Set the TLS
 
   // Set the TLS
-  if email_config.use_tls {
-    let tls_config = TlsParameters::new(smtp_server.to_string()).expect("the TLS backend is happy");
-    builder = builder.tls(Tls::Wrapper(tls_config));
-  }
+  let builder_dangerous = SmtpTransport::builder_dangerous(smtp_server).port(smtp_port);
+
+  let mut builder = match email_config.tls_type.as_str() {
+    "starttls" => SmtpTransport::starttls_relay(smtp_server)?,
+    "tls" => SmtpTransport::relay(smtp_server)?,
+    _ => builder_dangerous,
+  };
 
   // Set the creds if they exist
   if let (Some(username), Some(password)) = (email_config.smtp_login, email_config.smtp_password) {
 
   // Set the creds if they exist
   if let (Some(username), Some(password)) = (email_config.smtp_login, email_config.smtp_password) {
index 1f7121347db467c66add1a9aaecf8ed85eecb213..6e66b92d637481fc68fd376d77e4604bf0b8ed89 100644 (file)
@@ -89,7 +89,7 @@ pub struct DatabaseConfig {
   pub pool_size: u32,
 }
 
   pub pool_size: u32,
 }
 
-#[derive(Debug, Deserialize, Serialize, Clone, Document)]
+#[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)]
 pub struct EmailConfig {
   /// Hostname and port of the smtp server
   #[doku(example = "localhost:25")]
 pub struct EmailConfig {
   /// Hostname and port of the smtp server
   #[doku(example = "localhost:25")]
@@ -101,8 +101,10 @@ pub struct EmailConfig {
   #[doku(example = "noreply@example.com")]
   /// Address to send emails from, eg "noreply@your-instance.com"
   pub smtp_from_address: String,
   #[doku(example = "noreply@example.com")]
   /// Address to send emails from, eg "noreply@your-instance.com"
   pub smtp_from_address: String,
-  /// Whether or not smtp connections should use tls
-  pub use_tls: bool,
+  /// Whether or not smtp connections should use tls. Can be none, tls, or starttls
+  #[default("none")]
+  #[doku(example = "none")]
+  pub tls_type: String,
 }
 
 #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
 }
 
 #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]