tfcconnection/src/api/church_form.rs

192 lines
6 KiB
Rust

use actix_multipart::form::{text::Text, MultipartForm};
use actix_web::{post, HttpResponse};
use lettre::{
message::MultiPart,
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
#[derive(Debug, MultipartForm, Default)]
struct ChurchForm {
#[multipart(rename = "firstname")]
first_name: Option<Text<String>>,
#[multipart(rename = "lastname")]
last_name: Option<Text<String>>,
#[multipart(rename = "studentfirstname")]
student_first_name: Option<Text<String>>,
#[multipart(rename = "studentlastname")]
student_last_name: Option<Text<String>>,
relationship: Option<Text<String>>,
#[multipart(rename = "walk-with-jesus")]
walk: Option<Text<String>>,
commitment: Option<Text<String>>,
teachable: Option<Text<String>>,
#[multipart(rename = "pos-characteristics")]
positive: Option<Text<String>>,
#[multipart(rename = "neg-characteristics")]
negative: Option<Text<String>>,
#[multipart(rename = "extra-info")]
notes: Option<Text<String>>,
}
#[post("/church-form")]
pub async fn church_form(MultipartForm(form): MultipartForm<ChurchForm>) -> 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 Church Reference form for {}!",
first, last, student
);
let relationship = form
.relationship
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let walk = form
.walk
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let positive = form
.positive
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let negative = form
.negative
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let commitment = form
.commitment
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let teachable = form
.teachable
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let notes = form
.notes
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
log::info!("{first} {last} filled out a Church Reference form!");
let email = markup::new! {
@markup::doctype()
html {
head {
title { @format!("{} {} filled out a Church Reference 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!("Church 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 { "Walk with Jesus" }
td { @walk }
}
tr {
th { "Commitment to church" }
td { @commitment }
}
tr {
th { "Teachable spirit" }
td { @teachable }
}
tr {
th { "Positive Attributes" }
td { @positive }
}
tr {
th { "Negaitive Attributes" }
td { @negative }
}
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 <no-reply@mail.tfcconnection.org>"
.parse()
.unwrap(),
)
.to("Chris Cochrun <chris@tfcconnection.org>".parse().unwrap())
.to("Ethan Rose <ethan@tfcconnection.org>".parse().unwrap())
.subject(email_subject)
.multipart(multi)
{
let sender = SmtpTransport::relay("mail.tfcconnection.org")
.ok()
.unwrap()
.credentials(Credentials::new(
"no-reply@mail.tfcconnection.org".to_owned(),
"r9f36mNZFtiW4f".to_owned(),
))
.authentication(vec![Mechanism::Plain])
.build();
match sender.send(&m) {
Ok(res) => log::info!("{:?}", res),
Err(e) => log::info!("{e}"),
}
} else {
log::info!("Email incorrect");
}
HttpResponse::Ok().body("hi")
}