From b06baabb8c2695c48bb7e73a864d3032bbfc7003 Mon Sep 17 00:00:00 2001 From: Franz Dietrich Date: Sun, 5 Feb 2023 14:06:46 +0100 Subject: [PATCH] add template path to environment --- .env | 1 + terminwahl_back/src/api/write.rs | 101 ++++++++++++++++++------------- terminwahl_back/src/main.rs | 18 +++--- 3 files changed, 70 insertions(+), 50 deletions(-) diff --git a/.env b/.env index 4c7260c..f2757b4 100644 --- a/.env +++ b/.env @@ -4,3 +4,4 @@ SMTP_USER="SMTP_USERNAME" SMTP_PASSWORD="SMTP_PASSWORD" PORT="8087" PATH_TO_STATICS="terminwahl_front/dist/" +PATH_TO_TEMPLATES="terminwahl_back/templates/" diff --git a/terminwahl_back/src/api/write.rs b/terminwahl_back/src/api/write.rs index 9180776..939b2ca 100644 --- a/terminwahl_back/src/api/write.rs +++ b/terminwahl_back/src/api/write.rs @@ -3,7 +3,9 @@ use std::error::Error; use actix_web::{error, web, HttpResponse}; use futures::future; use handlebars::Handlebars; -use lettre::{message::header::ContentType, Message, SmtpTransport, Transport}; +use lettre::{ + message::header::ContentType, AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor, +}; use log::debug; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use serde::Serialize; @@ -35,7 +37,7 @@ impl FullAppointment { pub async fn save_appointments_json( pool: web::Data, - mailer: web::Data, + mailer: web::Data>, handlebars: web::Data>, input: web::Json<(Vec, Nutzer)>, ) -> Result { @@ -55,6 +57,7 @@ pub async fn save_appointments_json( .await .map_err(error::ErrorInternalServerError)?; + debug!("Loading the appointments"); let full_appointments = future::try_join_all(appointments.into_iter().map( |PlannedAppointment { teacher_id, @@ -63,24 +66,25 @@ pub async fn save_appointments_json( )) .await .expect("Failed to get full list of appointments"); - + debug!("Send the email"); let mail_result = send_confirmation_request( &full_appointments, &nutzer, &validation_key, - &handlebars, + handlebars.as_ref(), &mailer, - ); + ) + .await; Ok(HttpResponse::Ok().json(mail_result)) } -pub fn send_confirmation_request( +pub async fn send_confirmation_request<'a>( appointments: &Vec, nutzer: &Nutzer, validation_key: &str, - handlebars: &Handlebars, - mailer: &SmtpTransport, + handlebars: &Handlebars<'a>, + mailer: &AsyncSmtpTransport, ) -> RequestState { let data = json! { { @@ -89,43 +93,56 @@ pub fn send_confirmation_request( "validation_key": validation_key }}; debug!("{:?}", handlebars.get_templates()); - if let Ok(email_text) = handlebars.render("email_confirm", &data) { - let email = match Message::builder() - .from( - "Franz Dietrich " - .parse() - .expect("Should not fail"), - ) - .to( - match format!("{} <{}>", nutzer.name, nutzer.email).parse() { - Ok(v) => v, - Err(_) => return RequestState::Error, - }, - ) - .subject("Elternsprechtag: Bestätigen Sie Ihre Termine") - .header(ContentType::TEXT_PLAIN) - .body( - match lettre::message::Body::new_with_encoding( - email_text, - lettre::message::header::ContentTransferEncoding::Base64, + match handlebars.render("email_confirm", &data) { + Ok(email_text) => { + let email = match Message::builder() + .from( + "Franz Dietrich " + .parse() + .expect("Should not fail"), + ) + .to( + match format!("{} <{}>", nutzer.name, nutzer.email).parse() { + Ok(v) => v, + Err(e) => { + debug!("Error while sending email {e}"); + return RequestState::Error; + } + }, + ) + .subject("Elternsprechtag: Bestätigen Sie Ihre Termine") + .header(ContentType::TEXT_PLAIN) + .body( + match lettre::message::Body::new_with_encoding( + email_text, + lettre::message::header::ContentTransferEncoding::Base64, + ) { + Ok(body) => body, + Err(e) => { + debug!("Error while sending email {e:?}"); + return RequestState::Error; + } + }, ) { - Ok(body) => body, - Err(_) => return RequestState::Error, - }, - ) { - Ok(message) => message, - Err(_) => return RequestState::Error, - }; + Ok(message) => message, + Err(e) => { + debug!("Error while sending email {e}"); + return RequestState::Error; + } + }; - // Send the email - match mailer.send(&email) { - Ok(_) => RequestState::Success, - Err(e) => { - debug!("Failed to send: {e}"); - RequestState::Error + // Send the email + match mailer.send(email).await { + Ok(_) => RequestState::Success, + Err(e) => { + debug!("Failed to send: {e}"); + RequestState::Error + } } } - } else { - RequestState::Error + Err(e) => { + debug!("Failed to send email: Template did not render {e}"); + RequestState::Error + } } } diff --git a/terminwahl_back/src/main.rs b/terminwahl_back/src/main.rs index 6cf6f93..8d77abc 100644 --- a/terminwahl_back/src/main.rs +++ b/terminwahl_back/src/main.rs @@ -8,7 +8,7 @@ use actix_web::{ }; use dotenv::dotenv; use handlebars::Handlebars; -use lettre::{transport::smtp::authentication::Credentials, SmtpTransport}; +use lettre::{transport::smtp::authentication::Credentials, AsyncSmtpTransport, Tokio1Executor}; use log::debug; use std::env; use terminwahl_back::{api, db, handlebars_helper::TimeOfDate, views, CssPath}; @@ -25,22 +25,24 @@ async fn main() -> std::io::Result<()> { let smtp_user = env::var("SMTP_USER").expect("Failed to get smtp user"); let smtp_password = env::var("SMTP_PASSWORD").expect("Failed to get smtp password"); let wasm_statics = env::var("PATH_TO_STATICS").expect("Failed to get statics path"); + let handlebars_templates = env::var("PATH_TO_TEMPLATES").expect("Failed to get templates path"); let port: u16 = env::var("PORT") .expect("Failed to get port") .parse() .expect("Not a Portnumber"); let credentials = Credentials::new(smtp_user, smtp_password); - let smtp_pool = SmtpTransport::relay("smtp.1und1.de") - .expect("Failed to connect to smtp") - // Add credentials for authentication - .credentials(credentials) - // Connection pool settings - .build(); + let smtp_pool: AsyncSmtpTransport = + AsyncSmtpTransport::::relay("smtp.1und1.de") + .expect("Failed to connect to smtp") + // Add credentials for authentication + .credentials(credentials) + // Connection pool settings + .build(); let mut handlebars = Handlebars::new(); handlebars.register_helper("time_of", Box::new(TimeOfDate)); handlebars - .register_templates_directory(".hbs", "terminwahl_back/templates") + .register_templates_directory(".hbs", handlebars_templates) .unwrap(); log::info!("starting HTTP server at http://localhost:8080");