Something went wrong. Try again.
Microservice to bring 2FA to self hosted PDSes forked from pds.dad/pds-gatekeeper
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647use anyhow::Context;use lettre::{AsyncSendmailTransport, AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor};use std::env;use url::Url;
pub enum Mailer { Smtp(AsyncSmtpTransport<Tokio1Executor>), Sendmail(AsyncSendmailTransport<Tokio1Executor>),}
impl Mailer { pub async fn send(&self, msg: Message) -> anyhow::Result<()> { match self { Mailer::Smtp(m) => { m.send(msg).await.context("SMTP send failed")?; Ok(()) } Mailer::Sendmail(m) => { m.send(msg).await.context("sendmail send failed")?; Ok(()) } } }}
pub fn build_mailer_from_env() -> anyhow::Result<Mailer> { let raw = env::var("PDS_EMAIL_SMTP_URL") .context("PDS_EMAIL_SMTP_URL is not set in your pds.env file")?;
let url = Url::parse(&raw).context("PDS_EMAIL_SMTP_URL is not a valid URL")?;
let use_sendmail = url.scheme() == "sendmail" || url .query_pairs() .any(|(k, v)| k == "sendmail" && v == "true");
if use_sendmail { Ok(Mailer::Sendmail( AsyncSendmailTransport::<Tokio1Executor>::new(), )) } else { Ok(Mailer::Smtp( AsyncSmtpTransport::<Tokio1Executor>::from_url(raw.as_str())?.build(), )) }}