From e0ddaf414d5f737cef265c01f5bc4b2fbef50b2e Mon Sep 17 00:00:00 2001 From: Erb3 <49862976+Erb3@users.noreply.github.com> Date: Sun, 3 Nov 2024 10:30:49 +0100 Subject: [PATCH] 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 --- apps/labrinth/.env.example | 4 +++- apps/labrinth/src/auth/email/mod.rs | 14 +++++++++++++- apps/labrinth/src/lib.rs | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/labrinth/.env.example b/apps/labrinth/.env.example index 07dc02eec..dfafb0d77 100644 --- a/apps/labrinth/.env.example +++ b/apps/labrinth/.env.example @@ -81,6 +81,8 @@ TURNSTILE_SECRET=none SMTP_USERNAME=none SMTP_PASSWORD=none SMTP_HOST=none +SMTP_PORT=25 +SMTP_TLS=true SITE_VERIFY_EMAIL_PATH=none SITE_RESET_PASSWORD_PATH=none @@ -105,4 +107,4 @@ STRIPE_WEBHOOK_SECRET=none ADITUDE_API_KEY=none -PYRO_API_KEY=none \ No newline at end of file +PYRO_API_KEY=none diff --git a/apps/labrinth/src/auth/email/mod.rs b/apps/labrinth/src/auth/email/mod.rs index 80c8bb8e1..250222d13 100644 --- a/apps/labrinth/src/auth/email/mod.rs +++ b/apps/labrinth/src/auth/email/mod.rs @@ -1,6 +1,7 @@ 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; @@ -34,9 +35,20 @@ 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(25); let creds = Credentials::new(username, password); + let tls_settings = + if dotenvy::var("SMTP_TLS")?.parse::().unwrap_or(true) { + Tls::Wrapper(TlsParameters::new(host.clone().into())?) + } else { + Tls::None + }; - let mailer = SmtpTransport::relay(&host)?.credentials(creds).build(); + let mailer = SmtpTransport::relay(&host)? + .port(port) + .tls(tls_settings) + .credentials(creds) + .build(); mailer.send(&email)?; diff --git a/apps/labrinth/src/lib.rs b/apps/labrinth/src/lib.rs index 9bfc70ba5..765e6aead 100644 --- a/apps/labrinth/src/lib.rs +++ b/apps/labrinth/src/lib.rs @@ -455,6 +455,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");