diff --git a/crates/im-core/src/settings.rs b/crates/im-core/src/settings.rs index 6f538e0..ca4a794 100644 --- a/crates/im-core/src/settings.rs +++ b/crates/im-core/src/settings.rs @@ -59,7 +59,7 @@ pub async fn smtp(store: &Store) -> Result { let port = get(store, "smtp_port") .await? .and_then(|raw| raw.parse().ok()) - .unwrap_or(465); + .unwrap_or(587); let username = get(store, "smtp_username").await?.unwrap_or_default(); let from = get(store, "smtp_from").await?.unwrap_or_default(); let password = get(store, "smtp_password") @@ -120,6 +120,7 @@ mod tests { async fn smtp_roundtrip_with_sealed_password() { let store = Store::open(Path::new(":memory:")).await.unwrap(); assert!(!smtp(&store).await.unwrap().configured()); + assert_eq!(smtp(&store).await.unwrap().port, 587); let value = Smtp { host: "smtp.example.com".into(), diff --git a/crates/im-web/src/admin.rs b/crates/im-web/src/admin.rs index fafabfc..e282ed0 100644 --- a/crates/im-web/src/admin.rs +++ b/crates/im-web/src/admin.rs @@ -70,6 +70,7 @@ async fn admin_page(cx: &Cx) -> Result { let section = query_value(&query, "section").unwrap_or_else(|| "users".to_string()); let error = query_value(&query, "error"); let ok = query_value(&query, "ok"); + let why = query_value(&query, "why"); let invited = query_value(&query, "invited"); let nav = |current: &str| { @@ -104,8 +105,11 @@ async fn admin_page(cx: &Cx) -> Result { } )), (_, Some(code)) => Some(format!( - r#"
{}
"#, - error_text(code) + r#"
{}{}
"#, + error_text(code), + why.as_deref() + .map(|detail| format!("
{}
", escape(detail))) + .unwrap_or_default(), )), _ => None, }; @@ -432,7 +436,14 @@ async fn smtp_test(cx: &Cx) -> Result { Some(&e.to_string()), ) .await; - back(cx, "mail", "&error=smtp_test") + back( + cx, + "mail", + &format!( + "&error=smtp_test&why={}", + crate::oidc::urlencode(&e.to_string()) + ), + ) } } } diff --git a/crates/im-web/src/mailer.rs b/crates/im-web/src/mailer.rs index 918e367..78bae40 100644 --- a/crates/im-web/src/mailer.rs +++ b/crates/im-web/src/mailer.rs @@ -21,9 +21,18 @@ fn transport(smtp: &Smtp) -> Result::relay(&smtp.host) - .map_err(|e| MailError::Backend(e.to_string()))? - .port(smtp.port); + // 465 is implicit TLS — the handshake wraps the connection from the first + // byte. Everything else (587 above all) is submission: the session starts + // in the clear and STARTTLS upgrades it. Picking the wrong one is the + // classic "something went wrong": relay() against a 587 port hangs the TLS + // handshake on a server waiting for plaintext EHLO. + let mut builder = if smtp.port == 465 { + lettre::AsyncSmtpTransport::::relay(&smtp.host) + } else { + lettre::AsyncSmtpTransport::::starttls_relay(&smtp.host) + } + .map_err(|e| MailError::Backend(e.to_string()))? + .port(smtp.port); if !smtp.username.is_empty() { builder = builder.credentials(lettre::transport::smtp::authentication::Credentials::new( smtp.username.clone(), diff --git a/crates/im-web/src/pages.rs b/crates/im-web/src/pages.rs index d5fd246..d34f948 100644 --- a/crates/im-web/src/pages.rs +++ b/crates/im-web/src/pages.rs @@ -30,6 +30,7 @@ pub fn error_text(code: &str) -> &'static str { "password_personal" => "The password can't contain your address or your name.", "passwords_differ" => "The two passwords don't match.", "enroll_first" => "Set up your second factor first.", + "smtp_test" => "The test mail could not be sent.", _ => "Something went wrong. Try again.", } }