    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
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152

    use lettre::{
        transport::smtp::authentication::Credentials,
        AsyncSmtpTransport,
        AsyncTransport,
        Tokio1Executor,
    //    Address,
        message::{
           Mailbox,
           header::ContentType,
           MultiPart,
           SinglePart,
        },
        Message,
    };

    use crate::defs::AppDBs;
    use pasetoken_lib::ConfigPaSeToken;
    use serde_json::json;

    #[derive(Clone,Debug)]
    pub struct MailMessage {
        pub from: Mailbox,
        pub to: Mailbox,
        pub reply_to: Mailbox,
    }

    impl MailMessage {
         pub fn new(from: &str, to: &str, reply: &str) -> anyhow::Result<Self>  {
            let reply_to = if reply.is_empty() {
               to
            } else {
                reply
            };
            Ok(
                Self {
                    from: from.parse()?,
                    to: to.parse()?,
                    reply_to: reply_to.parse()?,
                }
            )
        }
        pub fn check(app_dbs: &AppDBs) -> String {
            if app_dbs.config.smtp.is_empty() {
                String::from("Error: no mail server")
            } else if app_dbs.config.mail_from.is_empty() {
                String::from("Error: no mail from address")
            } else {
                String::from("")
            }
        }
        #[allow(dead_code)]
        pub async fn send_message(&self, subject: &str, body: &str, app_dbs: &AppDBs) -> std::io::Result<()> {
            match  Message::builder()
                .from(self.from.to_owned())
                .reply_to(self.reply_to.to_owned())
                .to(self.to.to_owned())
                .subject(subject)
                .header(ContentType::TEXT_PLAIN)
                .body(body.to_owned())
            {
                Ok(message) => self.mail_message(message, app_dbs).await,
                Err(e) =>
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::NotFound,
                        format!("ERROR: Invalid mail: {}",e)
                    ))
            }
        }
        pub async fn send_html_message(&self, subject: &str, body: &str, html_body: &str, app_dbs: &AppDBs) -> std::io::Result<()> {
            match  Message::builder()
                .from(self.from.to_owned())
                .reply_to(self.reply_to.to_owned())
                .to(self.to.to_owned())
                .subject(subject)
                .multipart(
                MultiPart::alternative() // This is composed of two parts.
                    .singlepart(
                        SinglePart::builder()
                            .header(ContentType::TEXT_PLAIN)
                            .body(body.to_owned()),
                    )
                    .singlepart(
                        SinglePart::builder()
                            .header(ContentType::TEXT_HTML)
                            .body(html_body.to_owned()),
                    ),
                )
            {
                Ok(message) => self.mail_message(message, app_dbs).await,
                Err(e) =>
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::NotFound,
                        format!("ERROR: Invalid mail: {}",e)
                    ))
            }
        }
        pub async fn mail_message(&self, message: Message, app_dbs: &AppDBs) -> std::io::Result<()> {
            let mail_cred = crate::defs::MailMessage::get_credentials(&app_dbs.config.smtp_auth, &app_dbs.config.paseto);
            if ! mail_cred.contains("|") {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    format!("ERROR: Invalid mail credentials")
                ));
            }
            let auth_data: Vec<String> = mail_cred.split("|").map(|s| s.to_string()).collect();
            if auth_data.len() < 2 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    format!("ERROR: Invalid mail credentials")
                ));
            }
            let creds = Credentials::new(auth_data[0].to_owned(), auth_data[1].to_owned());
            // Open a remote connection to gmail
            let mailer: AsyncSmtpTransport<Tokio1Executor> =
                AsyncSmtpTransport::<Tokio1Executor>::relay(&app_dbs.config.smtp)
                    .unwrap()
                    .credentials(creds)
                    .build();

            // Send the email
            match mailer.send(message).await {
                Ok(_) => Ok(()),
                Err(e) => Err(std::io::Error::new(
                    std::io::ErrorKind::NotConnected,
                    format!("ERROR: Could not send email: {e:?}")
                ))
            }
        }
        pub fn get_credentials(token: &str, paseto_config: &ConfigPaSeToken) -> String {
            match paseto_config.pasetoken() {
                Ok(paseto) => {
                    match paseto.trusted(token, false) {
                        Ok(trusted_token) => {
                            if let Some(claims) = trusted_token.payload_claims() {
                                claims.get_claim("smtp_auth").unwrap_or(&json!("")).to_string().replace("\"","")
                            } else {
                                String::from("")
                            }
                        },
                        Err(e) => {
                            println!("Token not trusted: {}",e);
                            String::from("")
                        },
                    }
                },
                Err(e) => {
                    println!("Error collecting notify data: {}",e);
                    String::from("")
                }
            }
        }
    }