feat(labrinth): environment variables for more customizable SMTP (#2886)
* 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<me@alegon.dev> 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>
This commit is contained in:
parent
ecb1379585
commit
6f902e2107
@ -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
|
||||
|
||||
@ -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::<u16>().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)?;
|
||||
|
||||
|
||||
@ -425,6 +425,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::<String>("SMTP_TLS");
|
||||
|
||||
failed |= check_var::<String>("SITE_VERIFY_EMAIL_PATH");
|
||||
failed |= check_var::<String>("SITE_RESET_PASSWORD_PATH");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user