email.rs
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
use crate::configuration::{ Configuration };
use crate::errors::{ Result };
use mail_builder::{ MessageBuilder };
use mail_send::{ SmtpClientBuilder };
pub(crate) enum Client {
MailSend { from: String, smtp_host: String, smtp_port: u16 },
Tracing { from: String },
}
impl Client {
pub(crate) async fn send(&self, to: &str, subject: &str, body: &str) -> Result<()> {
match *self {
Client::MailSend { ref from, ref smtp_host, smtp_port } => {
let email = MessageBuilder::new()
.from(from.clone())
.to(to)
.subject(subject)
.text_body(body);
SmtpClientBuilder::new(smtp_host, smtp_port)
.implicit_tls(false) // Use STARTTLS
.connect()
.await?
.send(email)
.await?;
Ok(())
},
Client::Tracing { ref from } => {
tracing::info!("Request to send the following email:\nFrom: {}\nTo: {}\nSubject: {}\n{}", from, to, subject, body);
Ok(())
}
}
}
}
pub(crate) fn create_client(configuration: &Configuration) -> Client {
if configuration.email_live {
Client::MailSend {
from: configuration.email_from.clone(),
smtp_host: configuration.email_smtp_host.clone(),
smtp_port: configuration.email_smtp_port
}
} else {
Client::Tracing {
from: configuration.email_from.clone(),
}
}
}