aboutsummaryrefslogtreecommitdiff
path: root/crates/secd/src/client/email/mod.rs
blob: 915d18caca70bac4420556be14a87f50e62ff82d (plain)
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
use email_address::EmailAddress;
use lettre::Transport;
use log::error;
use std::collections::HashMap;

#[derive(Debug, thiserror::Error, derive_more::Display)]
pub enum EmailMessengerError {
    FailedToSendEmail,
}

pub struct EmailValidationMessage {
    pub recipient: EmailAddress,
    pub subject: String,
    pub body: String,
}

#[async_trait::async_trait]
pub(crate) trait EmailMessenger {
    async fn send_email(
        &self,
        email_address: &EmailAddress,
        template: &str,
        template_vars: HashMap<&str, &str>,
    ) -> Result<(), EmailMessengerError>;
}

pub(crate) struct LocalMailer {}

#[async_trait::async_trait]
impl EmailMessenger for LocalMailer {
    async fn send_email(
        &self,
        email_address: &EmailAddress,
        template: &str,
        template_vars: HashMap<&str, &str>,
    ) -> Result<(), EmailMessengerError> {
        todo!()
    }
}

#[async_trait::async_trait]
pub(crate) trait Sendable {
    async fn send(&self) -> Result<(), EmailMessengerError>;
}

#[async_trait::async_trait]
impl Sendable for EmailValidationMessage {
    // TODO: We need to break this up as before, especially so we can feature
    // gate unwanted things like Lettre...
    async fn send(&self) -> Result<(), EmailMessengerError> {
        // TODO: Get these things from the template...
        let email = lettre::Message::builder()
            .from("BranchControl <iam@branchcontrol.com>".parse().unwrap())
            .reply_to("BranchControl <iam@branchcontrol.com>".parse().unwrap())
            .to(self.recipient.to_string().parse().unwrap())
            .subject(self.subject.clone())
            .body(self.body.clone())
            .unwrap();

        let mailer = lettre::SmtpTransport::unencrypted_localhost();

        mailer.send(&email).map_err(|e| {
            error!("failed to send email {:?}", e);
            EmailMessengerError::FailedToSendEmail
        })?;
        Ok(())
    }
}