diff options
author | pennae <github@quasiparticle.net> | 2022-07-25 20:36:15 +0200 |
---|---|---|
committer | pennae <github@quasiparticle.net> | 2022-07-25 20:36:15 +0200 |
commit | 38e3b847f5f3e1bd40fa6261c3b313f4bab7af85 (patch) | |
tree | 2f0753230f41f603d5480e3dfa19958c339210ba | |
parent | a0e8836d9c25ecb29aa6f8b701b8f9cf72b09607 (diff) | |
download | minor-skulk-38e3b847f5f3e1bd40fa6261c3b313f4bab7af85.tar.gz minor-skulk-38e3b847f5f3e1bd40fa6261c3b313f4bab7af85.tar.xz minor-skulk-38e3b847f5f3e1bd40fa6261c3b313f4bab7af85.zip |
add support for smtp authentication
-rw-r--r-- | Rocket.toml | 14 | ||||
-rw-r--r-- | src/lib.rs | 8 | ||||
-rw-r--r-- | src/mailer.rs | 27 |
3 files changed, 38 insertions, 11 deletions
diff --git a/Rocket.toml b/Rocket.toml index 26cc045..476daf9 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -95,6 +95,20 @@ limits.bytes = "128 KiB" # #mail_port = 25 +# mail user (optional) +# +# user name to use in smtp auth with `mail_host`. no authentication +# will be attempted if unset. +# +#mail_user = user + +# mail password (optional) +# +# password to use in smtp auth with `mail_host`. no authentication +# will be attempted if unset. +# +#mail_password = verysecretpassword + # mail starttls support (optional) # # whether or not to use starttls for mail connections. @@ -7,7 +7,7 @@ use std::{ use anyhow::Context; use db::Db; use futures::Future; -use lettre::message::Mailbox; +use lettre::{message::Mailbox, transport::smtp::authentication::Credentials}; use mailer::Mailer; use push::PushClient; use rocket::{ @@ -71,6 +71,8 @@ struct Config { mail_from: Mailbox, mail_host: Option<String>, mail_port: Option<u16>, + mail_user: Option<String>, + mail_password: Option<String>, #[serde(default)] mail_starttls: bool, @@ -253,6 +255,10 @@ pub async fn build( config.mail_host.as_deref().unwrap_or("localhost"), config.mail_port.unwrap_or(25), config.mail_starttls, + match (config.mail_user.clone(), config.mail_password.clone()) { + (Some(user), Some(pw)) => Some(Credentials::new(user, pw)), + _ => None, + }, config.location.clone(), ) .context("setting up mail notifications")?, diff --git a/src/mailer.rs b/src/mailer.rs index 7f7ad9f..8e48db0 100644 --- a/src/mailer.rs +++ b/src/mailer.rs @@ -2,7 +2,10 @@ use std::time::Duration; use lettre::{ message::Mailbox, - transport::smtp::client::{Tls, TlsParameters}, + transport::smtp::{ + authentication::Credentials, + client::{Tls, TlsParameters}, + }, AsyncSmtpTransport, Message, Tokio1Executor, }; use rocket::http::uri::Absolute; @@ -22,20 +25,24 @@ impl Mailer { host: &str, port: u16, starttls: bool, + credentials: Option<Credentials>, verify_base: Absolute<'static>, ) -> anyhow::Result<Self> { + let transport = AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(host) + .port(port) + .tls(if starttls { + Tls::Required(TlsParameters::new(host.to_string())?) + } else { + Tls::None + }) + .timeout(Some(Duration::from_secs(5))); Ok(Mailer { from, verify_base, - transport: AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(host) - .port(port) - .tls(if starttls { - Tls::Required(TlsParameters::new(host.to_string())?) - } else { - Tls::None - }) - .timeout(Some(Duration::from_secs(5))) - .build(), + transport: match credentials { + None => transport.build(), + Some(c) => transport.credentials(c).build(), + }, }) } |