From 6f902e210708db6deb4f425599fcb065972eec2f Mon Sep 17 00:00:00 2001 From: Erb3 <49862976+Erb3@users.noreply.github.com> Date: Tue, 22 Apr 2025 13:29:14 +0200 Subject: [PATCH] feat(labrinth): environment variables for more customizable SMTP (#2886) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: move .env to .env.example * refactor(labrinth): allow setting SMTP port and TLS This will help setting up labrinth for local development. You can now use a mock SMTP server such as smtp4dev. The TLS options will stay the same as before if set to `true`, and disabled when `false`. Depends on #2883 * chore(labrinth): lint * chore(labrinth): conflicts * chore(labrinth): conflicts * fix: use TLS port by default Co-authored-by: AlexTMjugador Co-authored-by: Alejandro González <7822554+AlexTMjugador@users.noreply.github.com> Signed-off-by: Erb3 <49862976+Erb3@users.noreply.github.com> * fix(labrinth): correct deafult SMTP port in .env * feat(labrinth): expose all SMTP TLS settings Replaced if/else with a switch statement. The new values for `SMPT_TLS` are `none`, `opportunistic_start_tls`, `requires_start_tls`, `tls`. When none of these values are supplied, it defaults to full TLS (`tls`), and throws a warning. Resolves PR review * fix(labrinth): correct SMTP TLS example .env setting Signed-off-by: Erb3 <49862976+Erb3@users.noreply.github.com> * fix(labrinth) SMTP tls env var check Co-authored-by: Alejandro González <7822554+AlexTMjugador@users.noreply.github.com> Signed-off-by: Erb3 <49862976+Erb3@users.noreply.github.com> --------- Signed-off-by: Erb3 <49862976+Erb3@users.noreply.github.com> Co-authored-by: Alejandro González <7822554+AlexTMjugador@users.noreply.github.com> --- apps/labrinth/.env | 2 ++ apps/labrinth/src/auth/email/mod.rs | 23 ++++++++++++++++++++++- apps/labrinth/src/lib.rs | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/apps/labrinth/.env b/apps/labrinth/.env index 8ee8fd259..1fbb8eebd 100644 --- a/apps/labrinth/.env +++ b/apps/labrinth/.env @@ -84,6 +84,8 @@ HCAPTCHA_SECRET=none SMTP_USERNAME=none SMTP_PASSWORD=none SMTP_HOST=none +SMTP_PORT=465 +SMTP_TLS=tls SITE_VERIFY_EMAIL_PATH=none SITE_RESET_PASSWORD_PATH=none diff --git a/apps/labrinth/src/auth/email/mod.rs b/apps/labrinth/src/auth/email/mod.rs index 80c8bb8e1..77cbea3f2 100644 --- a/apps/labrinth/src/auth/email/mod.rs +++ b/apps/labrinth/src/auth/email/mod.rs @@ -1,8 +1,10 @@ use lettre::message::header::ContentType; use lettre::message::Mailbox; use lettre::transport::smtp::authentication::Credentials; +use lettre::transport::smtp::client::{Tls, TlsParameters}; use lettre::{Address, Message, SmtpTransport, Transport}; use thiserror::Error; +use tracing::warn; #[derive(Error, Debug)] pub enum MailError { @@ -34,9 +36,28 @@ pub fn send_email_raw( let username = dotenvy::var("SMTP_USERNAME")?; let password = dotenvy::var("SMTP_PASSWORD")?; let host = dotenvy::var("SMTP_HOST")?; + let port = dotenvy::var("SMTP_PORT")?.parse::().unwrap_or(465); let creds = Credentials::new(username, password); + let tls_setting = match dotenvy::var("SMTP_TLS")?.as_str() { + "none" => Tls::None, + "opportunistic_start_tls" => { + Tls::Opportunistic(TlsParameters::new(host.to_string())?) + } + "requires_start_tls" => { + Tls::Required(TlsParameters::new(host.to_string())?) + } + "tls" => Tls::Wrapper(TlsParameters::new(host.to_string())?), + _ => { + warn!("Unrecognized SMTP TLS setting. Defaulting to TLS."); + Tls::Wrapper(TlsParameters::new(host.to_string())?) + } + }; - let mailer = SmtpTransport::relay(&host)?.credentials(creds).build(); + let mailer = SmtpTransport::relay(&host)? + .port(port) + .tls(tls_setting) + .credentials(creds) + .build(); mailer.send(&email)?; diff --git a/apps/labrinth/src/lib.rs b/apps/labrinth/src/lib.rs index 18b94a724..61b5839a9 100644 --- a/apps/labrinth/src/lib.rs +++ b/apps/labrinth/src/lib.rs @@ -425,6 +425,8 @@ pub fn check_env_vars() -> bool { failed |= check_var::("SMTP_USERNAME"); failed |= check_var::("SMTP_PASSWORD"); failed |= check_var::("SMTP_HOST"); + failed |= check_var::("SMTP_PORT"); + failed |= check_var::("SMTP_TLS"); failed |= check_var::("SITE_VERIFY_EMAIL_PATH"); failed |= check_var::("SITE_RESET_PASSWORD_PATH");