1 use crate::settings::structs::Settings;
3 message::{header, Mailbox, MultiPart, SinglePart},
5 authentication::Credentials,
6 client::{Tls, TlsParameters},
14 use std::str::FromStr;
22 ) -> Result<(), String> {
23 let email_config = settings.email.to_owned().ok_or("no_email_setup")?;
24 let domain = settings.hostname.to_owned();
26 let (smtp_server, smtp_port) = {
27 let email_and_port = email_config.smtp_server.split(':').collect::<Vec<&str>>();
32 .expect("email needs a port"),
36 let email = Message::builder()
41 .expect("email from address isn't valid"),
44 Some(to_username.to_string()),
45 Address::from_str(to_email).expect("email to address isn't valid"),
49 MultiPart::mixed().multipart(
50 MultiPart::alternative()
53 .header(header::ContentType::TEXT_PLAIN)
54 .body(html.to_string()),
57 MultiPart::related().singlepart(
59 .header(header::ContentType::TEXT_HTML)
60 .body(html.to_string()),
65 .expect("email built incorrectly");
67 // don't worry about 'dangeous'. it's just that leaving it at the default configuration
69 let mut builder = SmtpTransport::builder_dangerous(smtp_server).port(smtp_port);
72 if email_config.use_tls {
73 let tls_config = TlsParameters::new(smtp_server.to_string()).expect("the TLS backend is happy");
74 builder = builder.tls(Tls::Wrapper(tls_config));
77 // Set the creds if they exist
78 if let (Some(username), Some(password)) = (email_config.smtp_login, email_config.smtp_password) {
79 builder = builder.credentials(Credentials::new(username, password));
82 let mailer = builder.hello_name(ClientId::Domain(domain)).build();
84 let result = mailer.send(&email);
88 Err(e) => Err(e.to_string()),