2019-03-19 13:37:56 +00:00
|
|
|
#![warn(clippy::too_many_arguments)]
|
2019-02-27 12:29:26 +00:00
|
|
|
use lettre_email::Email;
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
pub use self::mailer::*;
|
|
|
|
|
|
|
|
#[cfg(feature = "debug-mailer")]
|
|
|
|
mod mailer {
|
2022-01-03 08:28:37 +00:00
|
|
|
use plume_models::smtp::{SendableEmail, Transport};
|
2019-03-20 16:56:17 +00:00
|
|
|
use std::io::Read;
|
2019-02-27 12:29:26 +00:00
|
|
|
|
|
|
|
pub struct DebugTransport;
|
|
|
|
|
|
|
|
impl<'a> Transport<'a> for DebugTransport {
|
|
|
|
type Result = Result<(), ()>;
|
|
|
|
|
|
|
|
fn send(&mut self, email: SendableEmail) -> Self::Result {
|
|
|
|
println!(
|
|
|
|
"{}: from=<{}> to=<{:?}>\n{:#?}",
|
|
|
|
email.message_id().to_string(),
|
2019-03-20 16:56:17 +00:00
|
|
|
email
|
|
|
|
.envelope()
|
|
|
|
.from()
|
|
|
|
.map(ToString::to_string)
|
|
|
|
.unwrap_or_default(),
|
2019-02-27 12:29:26 +00:00
|
|
|
email.envelope().to().to_vec(),
|
|
|
|
{
|
|
|
|
let mut message = String::new();
|
2019-03-20 16:56:17 +00:00
|
|
|
email
|
|
|
|
.message()
|
|
|
|
.read_to_string(&mut message)
|
|
|
|
.map_err(|_| ())?;
|
2019-02-27 12:29:26 +00:00
|
|
|
message
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Mailer = Option<DebugTransport>;
|
|
|
|
|
|
|
|
pub fn init() -> Mailer {
|
|
|
|
Some(DebugTransport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "debug-mailer"))]
|
|
|
|
mod mailer {
|
2022-01-03 08:28:37 +00:00
|
|
|
use plume_models::smtp::{
|
|
|
|
authentication::{Credentials, Mechanism},
|
|
|
|
extension::ClientId,
|
|
|
|
ConnectionReuseParameters, SmtpClient, SmtpTransport,
|
2019-02-27 12:29:26 +00:00
|
|
|
};
|
2022-01-03 06:50:04 +00:00
|
|
|
use plume_models::CONFIG;
|
2019-02-27 12:29:26 +00:00
|
|
|
|
|
|
|
pub type Mailer = Option<SmtpTransport>;
|
|
|
|
|
|
|
|
pub fn init() -> Mailer {
|
2022-01-03 06:50:04 +00:00
|
|
|
let config = CONFIG.mail.as_ref()?;
|
|
|
|
let mail = SmtpClient::new_simple(&config.server)
|
2019-03-20 16:56:17 +00:00
|
|
|
.unwrap()
|
2022-01-03 06:50:04 +00:00
|
|
|
.hello_name(ClientId::Domain(config.helo_name.clone()))
|
|
|
|
.credentials(Credentials::new(
|
|
|
|
config.username.clone(),
|
|
|
|
config.password.clone(),
|
|
|
|
))
|
2019-02-27 12:29:26 +00:00
|
|
|
.smtp_utf8(true)
|
|
|
|
.authentication_mechanism(Mechanism::Plain)
|
|
|
|
.connection_reuse(ConnectionReuseParameters::NoReuse)
|
|
|
|
.transport();
|
|
|
|
Some(mail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_mail(dest: String, subject: String, body: String) -> Option<Email> {
|
|
|
|
Email::builder()
|
2019-03-20 16:56:17 +00:00
|
|
|
.from(
|
|
|
|
env::var("MAIL_ADDRESS")
|
|
|
|
.or_else(|_| {
|
|
|
|
Ok(format!(
|
|
|
|
"{}@{}",
|
|
|
|
env::var("MAIL_USER")?,
|
|
|
|
env::var("MAIL_SERVER")?
|
|
|
|
)) as Result<_, env::VarError>
|
|
|
|
})
|
2019-04-02 13:08:07 +00:00
|
|
|
.expect("The email server is not configured correctly"),
|
2019-03-20 16:56:17 +00:00
|
|
|
)
|
2019-02-27 12:29:26 +00:00
|
|
|
.to(dest)
|
|
|
|
.subject(subject)
|
|
|
|
.text(body)
|
|
|
|
.build()
|
|
|
|
.ok()
|
|
|
|
}
|