Skip to content

Commit

Permalink
refactor(labrinth): allow setting SMTP port and TLS
Browse files Browse the repository at this point in the history
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 modrinth#2883
  • Loading branch information
Erb3 committed Nov 3, 2024
1 parent 447d3d1 commit e0ddaf4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion apps/labrinth/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,4 +107,4 @@ STRIPE_WEBHOOK_SECRET=none

ADITUDE_API_KEY=none

PYRO_API_KEY=none
PYRO_API_KEY=none
14 changes: 13 additions & 1 deletion apps/labrinth/src/auth/email/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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::<u16>().unwrap_or(25);
let creds = Credentials::new(username, password);
let tls_settings =
if dotenvy::var("SMTP_TLS")?.parse::<bool>().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)?;

Expand Down
2 changes: 2 additions & 0 deletions apps/labrinth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ pub fn check_env_vars() -> bool {
failed |= check_var::<String>("SMTP_USERNAME");
failed |= check_var::<String>("SMTP_PASSWORD");
failed |= check_var::<String>("SMTP_HOST");
failed |= check_var::<u16>("SMTP_PORT");
failed |= check_var::<bool>("SMTP_TLS");

failed |= check_var::<String>("SITE_VERIFY_EMAIL_PATH");
failed |= check_var::<String>("SITE_RESET_PASSWORD_PATH");
Expand Down

0 comments on commit e0ddaf4

Please sign in to comment.