diff --git a/crates/web-form/Cargo.toml b/crates/web-form/Cargo.toml index 3ab6d6d..6090d62 100644 --- a/crates/web-form/Cargo.toml +++ b/crates/web-form/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] } -lettre = "0.11.7" -axum = "0.7.5" +lettre = { version = "0.11.7", features = ["smtp-transport", "tokio1", "tokio1-native-tls"] } +axum = { version = "0.7.5", features = ["macros"] } serde = { version = "1.0.201", features = ["derive"] } serde_json = "1.0.117" diff --git a/crates/web-form/src/main.rs b/crates/web-form/src/main.rs index 3548ad0..b7f5d7f 100644 --- a/crates/web-form/src/main.rs +++ b/crates/web-form/src/main.rs @@ -2,10 +2,13 @@ use axum::{ routing::post, Router, Form, - response::{IntoResponse, Redirect}, + response::{IntoResponse, Redirect, AppendHeaders}, + http::header::SET_COOKIE, }; -use lettre::{Message, AsyncSmtpTransport, Transport}; +use serde::Deserialize; +use lettre::{Message, AsyncTransport, Tokio1Executor}; +use lettre::transport::smtp::AsyncSmtpTransport; use lettre::message::Mailbox; use std::error::Error; use std::net::SocketAddr; @@ -30,9 +33,16 @@ async fn main() { async fn handle_form(Form(form_data): Form) -> impl IntoResponse { match send_email(form_data).await { - Ok(_) => format!("Form submitted successfully"), - Err(_) => Redirect::to("/contact.html"), + Ok(_) => ( + AppendHeaders([(SET_COOKIE, "is=ok")]), + Redirect::to("/contact.html"), + ), + Err(_) => ( + AppendHeaders([(SET_COOKIE, "is=err")]), + Redirect::to("/contact.html"), + ) } + } async fn send_email(form_data: FormData) -> Result<(), Box> { @@ -48,10 +58,10 @@ async fn send_email(form_data: FormData) -> Result<(), Box> { form_data.message ))?; - let mailer = AsyncSmtpTransport::relay("smtp.distrust.co")? + let mailer = AsyncSmtpTransport::::relay("smtp.distrust.co")? .build(); - mailer.send(&email)?; + mailer.send(email).await?; Ok(()) }