Skip to main content

email_clients/
email.rs

1#[cfg(feature = "smtp")]
2use crate::errors::EmailError;
3#[cfg(feature = "smtp")]
4use lettre::message::Mailbox;
5use std::fmt::Display;
6
7#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default, PartialOrd, PartialEq)]
8pub struct EmailAddress {
9    pub name: String,
10    pub email: String,
11}
12
13impl Display for EmailAddress {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        if self.name.is_empty() {
16            write!(f, "{}", self.email)
17        } else {
18            write!(f, "{} <{}>", self.name, self.email)
19        }
20    }
21}
22
23#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default)]
24pub struct EmailObject {
25    pub sender: EmailAddress,
26    pub to: Vec<EmailAddress>,
27    pub subject: String,
28    pub plain: String,
29    pub html: String,
30}
31
32#[cfg(feature = "smtp")]
33impl TryInto<Mailbox> for EmailAddress {
34    type Error = EmailError;
35
36    fn try_into(self) -> Result<Mailbox, Self::Error> {
37        Ok(Mailbox {
38            name: Some(self.name),
39            email: self.email.parse()?,
40        })
41    }
42}
43
44impl From<&str> for EmailAddress {
45    fn from(value: &str) -> Self {
46        Self {
47            name: "".to_string(),
48            email: value.to_string(),
49        }
50    }
51}