use actix_multipart::form::{text::Text, MultipartForm}; use actix_web::{post, HttpResponse}; use lettre::{message::MultiPart, Message}; use tracing::info; use crate::email::send_email; #[derive(Debug, MultipartForm, Default)] struct TeacherForm { #[multipart(rename = "firstname")] first_name: Option>, #[multipart(rename = "lastname")] last_name: Option>, #[multipart(rename = "studentfirstname")] student_first_name: Option>, #[multipart(rename = "studentlastname")] student_last_name: Option>, relationship: Option>, attitudes: Option>, positive: Option>, negative: Option>, #[multipart(rename = "team-challenges")] team: Option>, #[multipart(rename = "extra-info")] notes: Option>, } #[post("/teacher-form")] pub async fn teacher_form(MultipartForm(form): MultipartForm) -> HttpResponse { let first = form.first_name.as_ref().unwrap().0.clone(); let last = form.last_name.as_ref().unwrap().0.clone(); let student = format!( "{} {}", form.student_first_name.as_ref().unwrap().0.clone(), form.student_last_name.as_ref().unwrap().0.clone() ); let email_subject = format!( "{} {} filled out a teacher form for {}!", first, last, student ); let relationship = form .relationship .as_ref() .unwrap_or(&Text(String::from(""))) .0 .clone(); let attitudes = form .attitudes .as_ref() .unwrap_or(&Text(String::from(""))) .0 .clone(); let positive = form .positive .as_ref() .unwrap_or(&Text(String::from(""))) .0 .clone(); let negative = form .negative .as_ref() .unwrap_or(&Text(String::from(""))) .0 .clone(); let team = form .team .as_ref() .unwrap_or(&Text(String::from(""))) .0 .clone(); let notes = form .notes .as_ref() .unwrap_or(&Text(String::from(""))) .0 .clone(); info!("{first} {last} filled out a teacher form!"); let email = markup::new! { @markup::doctype() html { head { title { @format!("{} {} filled out a teacher form for {}!", first, last, student) } style { "table { border-collapse: collapse; width: 100% }" "td, th { padding: 8px }" "td { text-align: left; width: 70%; word-wrap: break-word }" "th { text-align: right; border-right: 1px solid #ddd }" "tr { border-bottom: 1px solid #ddd }" "h1 { text-align: center }" } } body { h1 { @format!("Teacher Reference form for {}!", student) } hr; table { tr { th { "Name" } td { @format!("{} {}", first, last) } } tr { th { "Student" } td { @student } } tr { th { "Relationship to teen" } td { @relationship } } tr { th { "Attitude in classroom" } td { @attitudes } } tr { th { "Positive Attributes" } td { @positive } } tr { th { "Negaitive Attributes" } td { @negative } } tr { th { "Teamwork" } td { @team } } tr { th { "Other Notes" } td { @notes } } } } } }; let multi = MultiPart::alternative_plain_html(String::from("Testing"), email.to_string()); if let Ok(m) = Message::builder() .from( "TFC ADMIN " .parse() .unwrap(), ) .to("Chris Cochrun ".parse().unwrap()) .to("Ethan Rose ".parse().unwrap()) .subject(email_subject) .multipart(multi) { let _ = send_email(m); } else { info!("Email incorrect"); } HttpResponse::Ok().body("hi") }