remove bkp
This commit is contained in:
parent
9e38b07589
commit
8e7557a36e
81
src/bkp
81
src/bkp
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue