remove bkp

This commit is contained in:
Anton Livaja 2024-05-31 00:06:01 -04:00
parent 9e38b07589
commit 8e7557a36e
Signed by: anton
GPG Key ID: 44A86CFF1FDF0E85
1 changed files with 0 additions and 81 deletions

81
src/bkp
View File

@ -1,81 +0,0 @@
use axum::{
extract::Form,
handler::post,
response::Html,
Router,
};
use axum_server;
use lettre::message::Message;
use lettre::transport::smtp::SmtpTransport;
use lettre::{AsyncSmtpTransport, AsyncTransport, Tokio1Executor};
use serde::Deserialize;
use std::net::SocketAddr;
#[derive(Deserialize)]
struct FormData {
name: String,
email: String,
message: String,
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/submit-email", post(handle_form));
let addr = SocketAddr::from(([127, 0, 0, 1], 6900));
println!("Listening on {}", addr);
axum_server::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap()
}
async fn handle_form(Form(form): Form<FormData>) -> Html<String> {
match send_email(form).await {
Ok(_) => Html("Form submitted successfully.".to_string()),
Err(e) => Html(format!("Error sending email.")),
}
}
async fn send_email(form: FormData) -> Result<(), Box<dyn std::error::Error>> {
let email = Message::builder()
.from(form.email.parse()?)
.to("sales@distrust.co".parse()?)
.subject("New Website Form Inquiry")
.body(format!("Name {}\nEmail: {}\nMessage: {}", form.name, form.email, form.message))?;
let mailer: AsyncSmtpTransport<Tokio1Executor> = SmtpTransport::builder_dangerous.build();
mailer.send(email).await {
Ok(_) -> Ok(());
Err(e) -> {
println!("Failed to send email: {}", e);
Err(Box::new(e) as Box<dyn std::error:Error>)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use axum::Router;
use tower::ServiceExt;
#[tokio::test]
async fn test_handle_form() {
let app = Router::new().route("/submit-email", post(handle_form));
let response = app
.oneshot(
Request::post("submit-email")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from("name=Lisan Al Ghaib&email=paul@atreides.com&message=The spice must flow."))
.unwrap(),
)
.await
.unwrap();
assert_eq(response.status(), StatusCode::OK);
}
}