DEVELOPMENT ENVIRONMENT

~liljamo/canwa

ref: efb58ac0c57a9d4dc8e6f8ea6776696bd93e9979 canwa/src/service/email.rs -rw-r--r-- 2.5 KiB
efb58ac0Jonni Liljamo feat: route helpers 10 days ago
                                                                                
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

use async_trait::async_trait;
use lettre::{
    Message, SmtpTransport, Transport,
    message::{Mailbox, header::ContentType},
    transport::smtp::authentication::Credentials,
};
use serde::{Deserialize, Serialize};

use crate::{
    config::{ServiceConfig, deserialize_token},
    routes::message::MessageForm,
};

use super::Service;

#[derive(Clone, Deserialize, Serialize)]
pub struct EmailConfig {
    from_name: Option<String>,
    from: String,
    to_name: Option<String>,
    to: String,
    username: String,
    #[serde(deserialize_with = "deserialize_token")]
    password: String,
    smtp_host: String,
}

#[typetag::serde(name = "email")]
impl ServiceConfig for EmailConfig {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

pub struct EmailService {
    config: EmailConfig,
    mailer: SmtpTransport,
}

impl EmailService {
    pub fn new(
        _client: reqwest::Client,
        config: EmailConfig,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let credentials = Credentials::new(config.username.clone(), config.password.clone());
        let mailer = SmtpTransport::relay(&config.smtp_host)?
            .credentials(credentials)
            .build();
        Ok(Self { config, mailer })
    }
}

#[async_trait]
impl Service for EmailService {
    async fn send(
        &self,
        form: &MessageForm,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let message = Message::builder()
            .from(Mailbox::new(
                self.config.from_name.clone(),
                self.config.from.parse()?,
            ))
            .to(Mailbox::new(
                self.config.to_name.clone(),
                self.config.to.parse()?,
            ))
            .subject(form.title.clone())
            .header(ContentType::TEXT_PLAIN)
            .body(form.message.clone())?;

        self.mailer.send(&message)?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::config::Config;

    use super::*;

    #[tokio::test]
    async fn config() {
        Config::from_str(
            r#"
        [services.e]
        type = "email"
        from = "a@a.com"
        to = "b@b.com"
        username = "a"
        password = "123"
        smtp_host = "smtp.a.com"
        "#,
        )
        .await
        .unwrap()
        .services["e"]
            .as_any()
            .downcast_ref::<EmailConfig>()
            .unwrap();
    }
}