2018-08-15 06:32:19 +00:00
|
|
|
use std::error::Error;
|
2018-08-15 15:25:59 +00:00
|
|
|
use native_tls::{Protocol, TlsConnector};
|
2018-09-19 19:45:50 +00:00
|
|
|
use lettre::{Transport, SmtpTransport, SmtpClient, ClientTlsParameters, ClientSecurity};
|
|
|
|
use lettre::smtp::ConnectionReuseParameters;
|
2018-08-15 15:00:55 +00:00
|
|
|
use lettre::smtp::authentication::Credentials;
|
2018-08-15 06:32:19 +00:00
|
|
|
use lettre_email::EmailBuilder;
|
|
|
|
|
|
|
|
use MailConfig;
|
|
|
|
|
|
|
|
fn mailer(config: &MailConfig) -> SmtpTransport {
|
|
|
|
let client_security = if config.smtp_ssl {
|
2018-09-19 19:45:50 +00:00
|
|
|
let mut tls_builder = TlsConnector::builder();
|
|
|
|
tls_builder.min_protocol_version(Some(Protocol::Tlsv11));
|
2018-08-15 06:32:19 +00:00
|
|
|
ClientSecurity::Required(
|
|
|
|
ClientTlsParameters::new(config.smtp_host.to_owned(), tls_builder.build().unwrap())
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
ClientSecurity::None
|
|
|
|
};
|
|
|
|
|
2018-09-19 19:45:50 +00:00
|
|
|
let smtp_client = SmtpClient::new(
|
2018-08-15 15:00:55 +00:00
|
|
|
(config.smtp_host.to_owned().as_str(), config.smtp_port),
|
|
|
|
client_security
|
|
|
|
).unwrap();
|
|
|
|
|
2018-09-19 19:45:50 +00:00
|
|
|
let smtp_client = match (&config.smtp_username, &config.smtp_password) {
|
2018-08-15 15:00:55 +00:00
|
|
|
(Some(username), Some(password)) => {
|
2018-09-19 19:45:50 +00:00
|
|
|
smtp_client.credentials(Credentials::new(username.to_owned(), password.to_owned()))
|
2018-08-15 15:00:55 +00:00
|
|
|
},
|
2018-09-19 19:45:50 +00:00
|
|
|
(_, _) => smtp_client,
|
2018-08-15 15:00:55 +00:00
|
|
|
};
|
|
|
|
|
2018-09-19 19:45:50 +00:00
|
|
|
smtp_client
|
2018-08-15 06:32:19 +00:00
|
|
|
.smtp_utf8(true)
|
2018-08-15 15:00:55 +00:00
|
|
|
.connection_reuse(ConnectionReuseParameters::NoReuse)
|
2018-09-19 19:45:50 +00:00
|
|
|
.transport()
|
2018-08-15 06:32:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 11:04:34 +00:00
|
|
|
pub fn send_password_hint(address: &str, hint: Option<String>, config: &MailConfig) -> Result<(), String> {
|
|
|
|
let (subject, body) = if let Some(hint) = hint {
|
|
|
|
("Your master password hint",
|
|
|
|
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))
|
|
|
|
} else {
|
|
|
|
("Sorry, you have no password hint...",
|
|
|
|
"Sorry, you have not specified any password hint...\n".to_string())
|
|
|
|
};
|
2018-08-15 08:17:05 +00:00
|
|
|
|
2018-08-15 06:32:19 +00:00
|
|
|
let email = EmailBuilder::new()
|
|
|
|
.to(address)
|
|
|
|
.from((config.smtp_from.to_owned(), "Bitwarden-rs"))
|
2018-09-11 11:04:34 +00:00
|
|
|
.subject(subject)
|
2018-08-15 08:17:05 +00:00
|
|
|
.body(body)
|
2018-08-15 06:32:19 +00:00
|
|
|
.build().unwrap();
|
|
|
|
|
2018-09-19 19:45:50 +00:00
|
|
|
match mailer(config).send(email.into()) {
|
2018-08-15 06:32:19 +00:00
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(e) => Err(e.description().to_string()),
|
|
|
|
}
|
|
|
|
}
|