torrust_index/config/v2/
mail.rs

1use lettre::message::Mailbox;
2use serde::{Deserialize, Serialize};
3
4/// SMTP configuration.
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6pub struct Mail {
7    /// The email address to send emails from.
8    #[serde(default = "Mail::default_from")]
9    pub from: Mailbox,
10
11    /// The email address to reply to.
12    #[serde(default = "Mail::default_reply_to")]
13    pub reply_to: Mailbox,
14
15    /// The SMTP server configuration.
16    #[serde(default = "Mail::default_smtp")]
17    pub smtp: Smtp,
18}
19
20impl Default for Mail {
21    fn default() -> Self {
22        Self {
23            from: Self::default_from(),
24            reply_to: Self::default_reply_to(),
25            smtp: Self::default_smtp(),
26        }
27    }
28}
29
30impl Mail {
31    fn default_from() -> Mailbox {
32        "example@email.com".parse().expect("valid mailbox")
33    }
34
35    fn default_reply_to() -> Mailbox {
36        "noreply@email.com".parse().expect("valid mailbox")
37    }
38
39    fn default_smtp() -> Smtp {
40        Smtp::default()
41    }
42}
43
44/// SMTP configuration.
45#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
46pub struct Smtp {
47    /// The SMTP port to use.
48    #[serde(default = "Smtp::default_port")]
49    pub port: u16,
50    /// The SMTP server to use.
51    #[serde(default = "Smtp::default_server")]
52    pub server: String,
53    /// The SMTP server credentials.
54    #[serde(default = "Smtp::default_credentials")]
55    pub credentials: Credentials,
56}
57
58impl Default for Smtp {
59    fn default() -> Self {
60        Self {
61            server: Self::default_server(),
62            port: Self::default_port(),
63            credentials: Self::default_credentials(),
64        }
65    }
66}
67
68impl Smtp {
69    fn default_server() -> String {
70        String::default()
71    }
72
73    fn default_port() -> u16 {
74        25
75    }
76
77    fn default_credentials() -> Credentials {
78        Credentials::default()
79    }
80}
81
82/// SMTP configuration.
83#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
84pub struct Credentials {
85    /// The password to use for SMTP authentication.
86    #[serde(default = "Credentials::default_password")]
87    pub password: String,
88    /// The username to use for SMTP authentication.
89    #[serde(default = "Credentials::default_username")]
90    pub username: String,
91}
92
93impl Default for Credentials {
94    fn default() -> Self {
95        Self {
96            username: Self::default_username(),
97            password: Self::default_password(),
98        }
99    }
100}
101
102impl Credentials {
103    fn default_username() -> String {
104        String::default()
105    }
106
107    fn default_password() -> String {
108        String::default()
109    }
110}