tfcconnection-zola/src/api/camp_form.rs

287 lines
8.1 KiB
Rust

use std::fs;
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
use actix_web::{post, HttpResponse};
use lettre::{
message::{header::ContentType, Attachment, MultiPart, SinglePart},
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
#[derive(Debug, MultipartForm, Default)]
struct CampForm {
#[multipart(rename = "firstname")]
first_name: Option<Text<String>>,
#[multipart(rename = "lastname")]
last_name: Option<Text<String>>,
#[multipart(rename = "parentfirstname")]
parent_first_name: Option<Text<String>>,
#[multipart(rename = "parentlastname")]
parent_last_name: Option<Text<String>>,
birthdate: Option<Text<String>>,
gender: Option<Text<String>>,
street: Option<Text<String>>,
city: Option<Text<String>>,
state: Option<Text<String>>,
zip: Option<Text<i32>>,
parentphone: Option<Text<String>>,
parentemail: Option<Text<String>>,
grade: Option<Text<String>>,
shirt: Option<Text<String>>,
allergies: Option<Text<String>>,
week: Option<Text<String>>,
registration: Option<Text<String>>,
}
#[post("/camp-form")]
pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResponse {
let first = form
.first_name
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let last = form
.last_name
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let email_subject = format!("{} {} signed up for camp!", first, last);
let parent = format!(
"{} {}",
form.parent_first_name
.as_ref()
.unwrap_or(&Text {
0: String::from("")
})
.0
.clone(),
form.parent_last_name
.as_ref()
.unwrap_or(&Text {
0: String::from("")
})
.0
.clone()
);
let birthdate = form
.birthdate
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let gender = form
.gender
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let street = form
.street
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let city = form
.city
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let state = form
.state
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let zip = form.zip.as_ref().unwrap_or(&Text { 0: 0 }).0.clone();
let parentphone = form
.parentphone
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let parentemail = form
.parentemail
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let grade = form
.grade
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let shirt = form
.shirt
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let allergies = form
.allergies
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let week = form
.week
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let registration = form
.registration
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
log::info!("{first} {last} signed up for camp!");
let email = markup::new! {
@markup::doctype()
html {
head {
title { @format!("{} {} signed up for camp!", first, last) }
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!("Camp form for {} {}!", first, last) }
hr;
table {
tr {
th { "Name" }
td { @format!("{} {}", first, last) }
}
tr {
th { "Parent" }
td { @parent }
}
tr {
th { "Birthdate" }
td { @birthdate }
}
tr {
th { "Gender" }
td { @gender }
}
tr {
th { "Street" }
td { @street }
}
tr {
th { "City" }
td { @city }
}
tr {
th { "State" }
td { @state }
}
tr {
th { "Zip" }
td { @zip }
}
tr {
th { "Parent Phone" }
td { @parentphone }
}
tr {
th { "Parent Email" }
td { @parentemail }
}
tr {
th { "Grade" }
td { @grade }
}
tr {
th { "Camper Allergies" }
td { @allergies }
}
tr {
th { "T-Shirt Size" }
td { @shirt }
}
tr {
th { "Week Choice" }
td { @week }
}
tr {
th { "Registration" }
td { @registration }
}
}
}
}
};
let multi = MultiPart::alternative_plain_html(
String::from("A camp form was filled out!"),
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("thankyou")
}