moving all forms to use new logger and email abstraction

This commit is contained in:
Chris Cochrun 2024-11-08 07:14:07 -06:00
parent e8dbb324f3
commit ebaf770d81
7 changed files with 55 additions and 103 deletions

View file

@ -10,6 +10,7 @@ use lettre::{
use maud::html; use maud::html;
use maud::DOCTYPE; use maud::DOCTYPE;
use reqwest::{Client, Error}; use reqwest::{Client, Error};
use tracing::info;
use crate::email::send_email; use crate::email::send_email;
@ -45,7 +46,7 @@ struct CampForm {
#[post("/camp-form")] #[post("/camp-form")]
pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResponse { pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResponse {
log::info!("a new form"); info!("a new form");
let first = form let first = form
.first_name .first_name
.as_ref() .as_ref()
@ -154,9 +155,9 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.clone(); .clone();
let reg = registration.clone(); let reg = registration.clone();
log::info!("Sending post to database"); info!("Sending post to database");
log::info!("{first} {last} signed up for camp!"); info!("{first} {last} signed up for camp!");
let email = html! { let email = html! {
(DOCTYPE) (DOCTYPE)
meta charset="utf-8"; meta charset="utf-8";
@ -262,17 +263,17 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
{ {
let _ = send_email(m); let _ = send_email(m);
} else { } else {
log::info!("Email incorrect"); info!("Email incorrect");
} }
match store_camp_form(form).await { match store_camp_form(form).await {
Ok(_) => log::info!("Successfully posted to nextcloud tables"), Ok(_) => info!("Successfully posted to nextcloud tables"),
Err(e) => log::error!("Error in posting camp data: {:?}", e), Err(e) => log::error!("Error in posting camp data: {:?}", e),
} }
match health.as_str() { match health.as_str() {
"now" => { "now" => {
log::info!("Sending them to fill out the health form"); info!("Sending them to fill out the health form");
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*")) .insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header(( .insert_header((
@ -286,7 +287,7 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
} }
"later" => match reg.as_str() { "later" => match reg.as_str() {
"now" => { "now" => {
log::info!("Sending them to pay for registration now"); info!("Sending them to pay for registration now");
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*")) .insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header(( .insert_header((
@ -296,7 +297,7 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.finish() .finish()
} }
"full" => { "full" => {
log::info!("Sending them to pay for the full registration now"); info!("Sending them to pay for the full registration now");
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*")) .insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header(( .insert_header((
@ -306,7 +307,7 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.finish() .finish()
} }
"later" => { "later" => {
log::info!("{} would like to pay later", full_name); info!("{} would like to pay later", full_name);
let html = html! { let html = html! {
div class="mt-8" { div class="mt-8" {
h2 { h2 {

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism}, transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport, Message, SmtpTransport, Transport,
}; };
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)] #[derive(Debug, MultipartForm, Default)]
struct ChurchForm { struct ChurchForm {
@ -84,7 +87,7 @@ pub async fn church_form(MultipartForm(form): MultipartForm<ChurchForm>) -> Http
.unwrap_or(&Text(String::from(""))) .unwrap_or(&Text(String::from("")))
.0 .0
.clone(); .clone();
log::info!("{first} {last} filled out a Church Reference form!"); info!("{first} {last} filled out a Church Reference form!");
let email = markup::new! { let email = markup::new! {
@markup::doctype() @markup::doctype()
html { html {
@ -156,21 +159,9 @@ pub async fn church_form(MultipartForm(form): MultipartForm<ChurchForm>) -> Http
.subject(email_subject) .subject(email_subject)
.multipart(multi) .multipart(multi)
{ {
let sender = SmtpTransport::relay("mail.tfcconnection.org") let _ = send_email(m);
.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 { } else {
log::info!("Email incorrect"); info!("Email incorrect");
} }
HttpResponse::Ok().body("hi") HttpResponse::Ok().body("hi")

View file

@ -9,6 +9,9 @@ use lettre::{
}; };
use maud::html; use maud::html;
use reqwest::{Client, Error}; use reqwest::{Client, Error};
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)] #[derive(Debug, MultipartForm, Default)]
struct HealthForm { struct HealthForm {
@ -254,7 +257,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.unwrap_or(&Text(String::from(""))) .unwrap_or(&Text(String::from("")))
.0 .0
.clone(); .clone();
log::info!("{first} {last} filled out a health form!"); info!("{first} {last} filled out a health form!");
let email = html! { let email = html! {
html { html {
head { head {
@ -387,10 +390,10 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
let mut path: Option<String> = Some(String::from("")); let mut path: Option<String> = Some(String::from(""));
let mut file_exists = false; let mut file_exists = false;
let mut filename = String::from(""); let mut filename = String::from("");
log::info!("Does the file exist? {:?}", file_exists); info!("Does the file exist? {:?}", file_exists);
match store_health_form(&form).await { match store_health_form(&form).await {
Ok(_) => log::info!("Successfully posted the health form in nextcloud tables"), Ok(_) => info!("Successfully posted the health form in nextcloud tables"),
Err(e) => log::error!("Error posting health form to nextcloud tables: {:?}", e), Err(e) => log::error!("Error posting health form to nextcloud tables: {:?}", e),
} }
@ -403,10 +406,10 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
path = Some(format!("./tmp/{}", file)); path = Some(format!("./tmp/{}", file));
} }
// let path = format!("./tmp/{}", file); // let path = format!("./tmp/{}", file);
log::info!("saving to {}", path.clone().unwrap()); info!("saving to {}", path.clone().unwrap());
match f.file.persist(path.clone().unwrap()) { match f.file.persist(path.clone().unwrap()) {
Ok(f) => { Ok(f) => {
log::info!( info!(
"Hey! We got a file! {:?}", "Hey! We got a file! {:?}",
f.metadata().expect("Something bad happened mate") f.metadata().expect("Something bad happened mate")
); );
@ -414,7 +417,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
file_exists = true; file_exists = true;
} }
} }
Err(e) => log::info!("{:?}: Probably a missing image", e), Err(e) => info!("{:?}: Probably a missing image", e),
} }
} }
} }
@ -423,7 +426,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
let filebody = fs::read(path.clone().unwrap_or_default()); let filebody = fs::read(path.clone().unwrap_or_default());
let content_type = ContentType::parse("image/jpg").unwrap(); let content_type = ContentType::parse("image/jpg").unwrap();
let attachment = Attachment::new(filename).body(filebody.unwrap(), content_type); let attachment = Attachment::new(filename).body(filebody.unwrap(), content_type);
// log::info!("The attachment is: {:?}", attachment.headers()); // info!("The attachment is: {:?}", attachment.headers());
MultiPart::mixed() MultiPart::mixed()
.singlepart(SinglePart::html(email.into_string())) .singlepart(SinglePart::html(email.into_string()))
.singlepart(attachment) .singlepart(attachment)
@ -431,7 +434,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
MultiPart::alternative_plain_html(String::from("Testing"), email.into_string()) MultiPart::alternative_plain_html(String::from("Testing"), email.into_string())
}; };
// log::info!("{:?}", multi); // info!("{:?}", multi);
if let Ok(m) = Message::builder() if let Ok(m) = Message::builder()
.from( .from(
@ -444,26 +447,14 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.subject(email_subject) .subject(email_subject)
.multipart(multi) .multipart(multi)
{ {
let sender = SmtpTransport::relay("mail.tfcconnection.org") let _ = send_email(m);
.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 { } else {
log::info!("Email incorrect"); info!("Email incorrect");
} }
match registration.as_str() { match registration.as_str() {
"now" => { "now" => {
log::info!("Sending them to pay for registration now"); info!("Sending them to pay for registration now");
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*")) .insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header(( .insert_header((
@ -473,7 +464,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.finish() .finish()
} }
"full" => { "full" => {
log::info!("Sending them to pay for the full registration now"); info!("Sending them to pay for the full registration now");
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*")) .insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header(( .insert_header((
@ -483,7 +474,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.finish() .finish()
} }
"later" => { "later" => {
log::info!("{} would like to pay later", full_name); info!("{} would like to pay later", full_name);
let html = html! { let html = html! {
div class="mt-8" { div class="mt-8" {
h2 { h2 {

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism}, transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport, Message, SmtpTransport, Transport,
}; };
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)] #[derive(Debug, MultipartForm, Default)]
struct LocalForm { struct LocalForm {
@ -165,7 +168,7 @@ pub async fn local_form(MultipartForm(form): MultipartForm<LocalForm>) -> HttpRe
.unwrap_or(&Text(String::from(""))) .unwrap_or(&Text(String::from("")))
.0 .0
.clone(); .clone();
log::info!("{first} {last} signed up for the local mission trip!"); info!("{first} {last} signed up for the local mission trip!");
let email = markup::new! { let email = markup::new! {
@markup::doctype() @markup::doctype()
html { html {
@ -260,7 +263,7 @@ pub async fn local_form(MultipartForm(form): MultipartForm<LocalForm>) -> HttpRe
} }
} }
}; };
log::info!("{:?}", form); info!("{:?}", form);
let multi = MultiPart::alternative_plain_html(String::from("Testing"), email.to_string()); let multi = MultiPart::alternative_plain_html(String::from("Testing"), email.to_string());
if let Ok(m) = Message::builder() if let Ok(m) = Message::builder()
@ -274,21 +277,9 @@ pub async fn local_form(MultipartForm(form): MultipartForm<LocalForm>) -> HttpRe
.subject(email_subject) .subject(email_subject)
.multipart(multi) .multipart(multi)
{ {
let sender = SmtpTransport::relay("mail.tfcconnection.org") let _ = send_email(m);
.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 { } else {
log::info!("Email incorrect"); info!("Email incorrect");
} }
HttpResponse::Ok().body("thankyou") HttpResponse::Ok().body("thankyou")

View file

@ -1,17 +1,13 @@
use std::fs; use std::fs;
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm}; use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
use actix_rt::Runtime;
use actix_web::{post, HttpResponse}; use actix_web::{post, HttpResponse};
use color_eyre::Result;
use lettre::{ use lettre::{
message::{header::ContentType, Attachment, MultiPart, SinglePart}, message::{header::ContentType, Attachment, MultiPart, SinglePart},
transport::smtp::authentication::{Credentials, Mechanism}, Message,
Message, SmtpTransport, Transport,
}; };
use markup::DynRender;
use maud::{html, PreEscaped, DOCTYPE}; use maud::{html, PreEscaped, DOCTYPE};
use tracing::{error, info, warn}; use tracing::{error, info};
#[derive(Debug, MultipartForm)] #[derive(Debug, MultipartForm)]
struct MtForm { struct MtForm {
@ -228,7 +224,7 @@ pub async fn mt_form(MultipartForm(form): MultipartForm<MtForm>) -> HttpResponse
let last = form.last_name.clone(); let last = form.last_name.clone();
let email_subject = format!("{} {} signed up for mission trip!", first, last); let email_subject = format!("{} {} signed up for mission trip!", first, last);
let filename_noext = format!("{}_{}", first, last); let filename_noext = format!("{}_{}", first, last);
log::info!("{first} {last} signed up for mission trip!"); info!("{first} {last} signed up for mission trip!");
let email = form.build_email().await; let email = form.build_email().await;
let mut path = String::from(""); let mut path = String::from("");
let mut file_exists = false; let mut file_exists = false;
@ -250,7 +246,7 @@ pub async fn mt_form(MultipartForm(form): MultipartForm<MtForm>) -> HttpResponse
file_exists = true; file_exists = true;
} }
} }
Err(e) => log::info!("{:?}: Probably a missing image", e), Err(e) => info!("{:?}: Probably a missing image", e),
} }
} }

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism}, transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport, Message, SmtpTransport, Transport,
}; };
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)] #[derive(Debug, MultipartForm, Default)]
struct ParentForm { struct ParentForm {
@ -91,7 +94,7 @@ pub async fn parent_form(MultipartForm(form): MultipartForm<ParentForm>) -> Http
.unwrap_or(&Text(String::from(""))) .unwrap_or(&Text(String::from("")))
.0 .0
.clone(); .clone();
log::info!("{first} {last} filled out a parent form!"); info!("{first} {last} filled out a parent form!");
let email = markup::new! { let email = markup::new! {
@markup::doctype() @markup::doctype()
html { html {
@ -167,21 +170,9 @@ pub async fn parent_form(MultipartForm(form): MultipartForm<ParentForm>) -> Http
.subject(email_subject) .subject(email_subject)
.multipart(multi) .multipart(multi)
{ {
let sender = SmtpTransport::relay("mail.tfcconnection.org") let _ = send_email(m);
.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 { } else {
log::info!("Email incorrect"); info!("Email incorrect");
} }
HttpResponse::Ok().body("hi") HttpResponse::Ok().body("hi")

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism}, transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport, Message, SmtpTransport, Transport,
}; };
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)] #[derive(Debug, MultipartForm, Default)]
struct TeacherForm { struct TeacherForm {
@ -75,7 +78,7 @@ pub async fn teacher_form(MultipartForm(form): MultipartForm<TeacherForm>) -> Ht
.unwrap_or(&Text(String::from(""))) .unwrap_or(&Text(String::from("")))
.0 .0
.clone(); .clone();
log::info!("{first} {last} filled out a teacher form!"); info!("{first} {last} filled out a teacher form!");
let email = markup::new! { let email = markup::new! {
@markup::doctype() @markup::doctype()
html { html {
@ -143,21 +146,9 @@ pub async fn teacher_form(MultipartForm(form): MultipartForm<TeacherForm>) -> Ht
.subject(email_subject) .subject(email_subject)
.multipart(multi) .multipart(multi)
{ {
let sender = SmtpTransport::relay("mail.tfcconnection.org") let _ = send_email(m);
.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 { } else {
log::info!("Email incorrect"); info!("Email incorrect");
} }
HttpResponse::Ok().body("hi") HttpResponse::Ok().body("hi")