1 use crate::settings::structs::Settings;
3 message::{header, Mailbox, MultiPart, SinglePart},
5 authentication::Credentials,
6 client::{Tls, TlsParameters},
14 use std::str::FromStr;
21 ) -> Result<(), String> {
22 let email_config = Settings::get().email.ok_or("no_email_setup")?;
23 let domain = Settings::get().hostname;
25 let (smtp_server, smtp_port) = {
26 let email_and_port = email_config.smtp_server.split(':').collect::<Vec<&str>>();
31 .expect("email needs a port"),
35 let email = Message::builder()
40 .expect("email from address isn't valid"),
43 Some(to_username.to_string()),
44 Address::from_str(to_email).expect("email to address isn't valid"),
48 MultiPart::mixed().multipart(
49 MultiPart::alternative()
52 .header(header::ContentType::TEXT_PLAIN)
53 .body(html.to_string()),
56 MultiPart::related().singlepart(
58 .header(header::ContentType::TEXT_HTML)
59 .body(html.to_string()),
64 .expect("email built incorrectly");
66 // don't worry about 'dangeous'. it's just that leaving it at the default configuration
68 let mut builder = SmtpTransport::builder_dangerous(smtp_server).port(smtp_port);
71 if email_config.use_tls {
72 let tls_config = TlsParameters::new(smtp_server.to_string()).expect("the TLS backend is happy");
73 builder = builder.tls(Tls::Wrapper(tls_config));
76 // Set the creds if they exist
77 if let (Some(username), Some(password)) = (email_config.smtp_login, email_config.smtp_password) {
78 builder = builder.credentials(Credentials::new(username, password));
81 let mailer = builder.hello_name(ClientId::Domain(domain)).build();
83 let result = mailer.send(&email);
87 Err(e) => Err(e.to_string()),