2018-08-15 08:32:19 +02:00
|
|
|
use std::error::Error;
|
|
|
|
use native_tls::TlsConnector;
|
|
|
|
use native_tls::{Protocol};
|
|
|
|
use lettre::{EmailTransport, SmtpTransport, ClientTlsParameters, ClientSecurity};
|
|
|
|
use lettre::smtp::{ConnectionReuseParameters, SmtpTransportBuilder};
|
2018-08-15 17:00:55 +02:00
|
|
|
use lettre::smtp::authentication::Credentials;
|
2018-08-15 08:32:19 +02:00
|
|
|
use lettre_email::EmailBuilder;
|
|
|
|
|
|
|
|
use MailConfig;
|
|
|
|
|
|
|
|
fn mailer(config: &MailConfig) -> SmtpTransport {
|
|
|
|
let client_security = if config.smtp_ssl {
|
|
|
|
let mut tls_builder = TlsConnector::builder().unwrap();
|
2018-08-15 17:00:55 +02:00
|
|
|
tls_builder.supported_protocols(&[Protocol::Tlsv11, Protocol::Tlsv12]).unwrap();
|
2018-08-15 08:32:19 +02:00
|
|
|
ClientSecurity::Required(
|
|
|
|
ClientTlsParameters::new(config.smtp_host.to_owned(), tls_builder.build().unwrap())
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
ClientSecurity::None
|
|
|
|
};
|
|
|
|
|
2018-08-15 17:00:55 +02:00
|
|
|
let smtp_transport = SmtpTransportBuilder::new(
|
|
|
|
(config.smtp_host.to_owned().as_str(), config.smtp_port),
|
|
|
|
client_security
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
let smtp_transport = match (&config.smtp_username, &config.smtp_password) {
|
|
|
|
(Some(username), Some(password)) => {
|
|
|
|
smtp_transport.credentials(Credentials::new(username.to_owned(), password.to_owned()))
|
|
|
|
},
|
|
|
|
(_, _) => smtp_transport,
|
|
|
|
};
|
|
|
|
|
|
|
|
smtp_transport
|
2018-08-15 08:32:19 +02:00
|
|
|
.smtp_utf8(true)
|
2018-08-15 17:00:55 +02:00
|
|
|
.connection_reuse(ConnectionReuseParameters::NoReuse)
|
2018-08-15 08:32:19 +02:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_password_hint(address: &str, hint: &str, config: &MailConfig) -> Result<(), String> {
|
2018-08-15 10:17:05 +02:00
|
|
|
let body = format!(
|
|
|
|
"You (or someone) recently requested your master password hint.\n\n\
|
|
|
|
Your hint is: \"{}\"\n\n\
|
|
|
|
If you did not request your master password hint you can safely ignore this email.\n",
|
|
|
|
hint);
|
|
|
|
|
2018-08-15 08:32:19 +02:00
|
|
|
let email = EmailBuilder::new()
|
|
|
|
.to(address)
|
|
|
|
.from((config.smtp_from.to_owned(), "Bitwarden-rs"))
|
|
|
|
.subject("Your Master Password Hint")
|
2018-08-15 10:17:05 +02:00
|
|
|
.body(body)
|
2018-08-15 08:32:19 +02:00
|
|
|
.build().unwrap();
|
|
|
|
|
|
|
|
match mailer(config).send(&email) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(e) => Err(e.description().to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|