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
use common::models::status_report::TeamReport;
use config::Config;
use lettre::message::header::ContentType;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};

use log::{debug, error, info};

/// Send a confirmation email for team report
/// There is many todo's for this
pub fn send_confirmation_email(
    to_who: String,
    report_data: TeamReport,
    secret: &Config,
    server: &Config,
) -> Result<String, String> {
    info!("Sending team report confirmation email {}", to_who);
    let email_result = Message::builder()
        // "NoBody <nobody@domain.tld>"
        .from(
            format!(
                "Status Reports Server <{}>",
                server
                    .get::<String>("smtp_email")
                    .expect("Missing sender email")
            )
            .parse()
            .unwrap(),
        )
        .reply_to(
            format!(
                "no-reply <{}>",
                server
                    .get::<String>("smtp_email")
                    .expect("Missing sender email")
            )
            .parse()
            .unwrap(),
        )
        // .to(format!("{} <{}>", to_who, server.get::<String>("admin_email").expect("Missing admin email")).parse().unwrap())
        .to(to_who.parse().unwrap())
        .subject("Team Report Confirmation Email")
        .header(ContentType::TEXT_PLAIN)
        .body(String::from(format!(
            "A team report was successfully submitted by {to_who}.\n\
            Sprint {} Team Report:\n\
            Understand - Easiest: {}\n\
            Understand - Hardest: {}\n\
            Approach - Easiest: {}\n\
            Approach - Hardest: {}\n\
            Solve - Easiest: {}\n\
            Solve - Hardest: {}\n\
            Evaluate - Easiest: {}\n\
            Evaluate - Hardest: {}\n\
            Completion: {}\n\
            Contact: {}\n\
            Comments: {}",
            report_data.sprint_num,
            report_data.understand_easiest,
            report_data.understand_hardest,
            report_data.approach_easiest,
            report_data.approach_hardest,
            report_data.solve_easiest,
            report_data.solve_hardest,
            report_data.evaluate_easiest,
            report_data.evaluate_hardest,
            report_data.completion,
            report_data.contact,
            report_data.comments
        )));
    let email = match email_result {
        Ok(em) => em,
        Err(e) => {
            error!("Unable to make email: {:?}", e);
            return Err(format!("Could not make email: {:?}", e));
        }
    };

    debug!("email made");

    let username = secret.get("smtp_username").expect("Missing email username");
    let password = secret.get("smtp_password").expect("Missing email password");

    let creds = Credentials::new(username, password);

    let server_url: String = server.get("smtp_server").expect("Missing email server url");

    // Open a remote connection to gmail
    let mailer = SmtpTransport::starttls_relay(&server_url)
        .unwrap()
        .credentials(creds)
        .build();

    debug!("mailer ready");

    // Send the email
    match mailer.send(&email) {
        Ok(_) => {
            info!("Email sent");
            Ok("Email sent successfully!".to_string())
        }
        Err(e) => {
            error!("Email not sent");
            Err(format!("Could not send email: {:?}", e))
        }
    }
}