fix up errors

This commit is contained in:
Anton Livaja 2024-06-08 19:54:57 -04:00
parent f442a67b48
commit e92c4d1b0e
Signed by: anton
GPG Key ID: 44A86CFF1FDF0E85
2 changed files with 18 additions and 8 deletions

View File

@ -7,7 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
lettre = "0.11.7" lettre = { version = "0.11.7", features = ["smtp-transport", "tokio1", "tokio1-native-tls"] }
axum = "0.7.5" axum = { version = "0.7.5", features = ["macros"] }
serde = { version = "1.0.201", features = ["derive"] } serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117" serde_json = "1.0.117"

View File

@ -2,10 +2,13 @@ use axum::{
routing::post, routing::post,
Router, Router,
Form, 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 lettre::message::Mailbox;
use std::error::Error; use std::error::Error;
use std::net::SocketAddr; use std::net::SocketAddr;
@ -30,9 +33,16 @@ async fn main() {
async fn handle_form(Form(form_data): Form<FormData>) -> impl IntoResponse { async fn handle_form(Form(form_data): Form<FormData>) -> impl IntoResponse {
match send_email(form_data).await { match send_email(form_data).await {
Ok(_) => format!("Form submitted successfully"), Ok(_) => (
Err(_) => Redirect::to("/contact.html"), 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<dyn Error>> { async fn send_email(form_data: FormData) -> Result<(), Box<dyn Error>> {
@ -48,10 +58,10 @@ async fn send_email(form_data: FormData) -> Result<(), Box<dyn Error>> {
form_data.message form_data.message
))?; ))?;
let mailer = AsyncSmtpTransport::relay("smtp.distrust.co")? let mailer = AsyncSmtpTransport::<Tokio1Executor>::relay("smtp.distrust.co")?
.build(); .build();
mailer.send(&email)?; mailer.send(email).await?;
Ok(()) Ok(())
} }