1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use config::Config;
use lettre::message::header::ContentType;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use log::{debug, error, info};
pub fn send_test_email(to_who: String, secret: &Config, server: &Config) -> Result<String, String> {
info!("Sending test email");
let email_result = Message::builder()
.from(
format!(
"Status Reports Server <{}>",
server
.get::<String>("smtp_email")
.expect("Missing sender email")
)
.parse()
.unwrap(),
)
.reply_to(
format!(
"no-reply <{}>",
server
.get::<String>("smtp_email")
.expect("Missing sender email")
)
.parse()
.unwrap(),
)
.to(to_who.parse().unwrap())
.subject("Test Email from Status Reports Backend")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!"));
let email = match email_result {
Ok(em) => em,
Err(e) => {
error!("Unable to make email: {:?}", e);
return Err(format!("Could not make email: {:?}", e));
}
};
debug!("email made");
let username = secret.get("smtp_username").expect("Missing email username");
let password = secret.get("smtp_password").expect("Missing email password");
let creds = Credentials::new(username, password);
let server_url: String = server.get("smtp_server").expect("Missing email server url");
let mailer = SmtpTransport::starttls_relay(&server_url)
.unwrap()
.credentials(creds)
.build();
debug!("mailer ready");
match mailer.send(&email) {
Ok(_) => {
info!("Email sent");
Ok("Email sent successfully!".to_string())
}
Err(e) => {
error!("Email not sent");
Err(format!("Could not send email: {:?}", e))
}
}
}