turn project into workspace

This commit is contained in:
Anton Livaja 2024-06-08 19:01:51 -04:00
parent 8e7557a36e
commit 4fa5617112
Signed by: anton
GPG Key ID: 44A86CFF1FDF0E85
4 changed files with 31 additions and 20 deletions

View File

@ -1,14 +1,6 @@
[package]
name = "website-form"
version = "0.1.0"
edition = "2021"
[workspace]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
lettre = "0.11.7"
axum = "0.7.5"
serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117"
axum-server = "0.6.0"
resolver = "2"
members = [
"crates/webform"
]

View File

@ -0,0 +1,14 @@
[package]
name = "website-form"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
lettre = "0.11.7"
axum = "0.7.5"
serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117"
axum-server = "0.6.0"

View File

@ -2,12 +2,11 @@ use axum::{
routing::post,
Router,
Form,
response::IntoResponse,
response::{IntoResponse, Redirect},
};
use serde::Deserialize;
use lettre::{Message, SmtpTransport, Transport};
use lettre::{Message, AsyncSmtpTransport, Transport};
use lettre::message::Mailbox;
use std::error::Error;
use std::net::SocketAddr;
@ -33,18 +32,24 @@ async fn main() {
async fn handle_form(Form(form_data): Form<FormData>) -> impl IntoResponse {
match send_email(form_data).await {
Ok(_) => format!("Form submitted successfully"),
Err(_) => format!("Error sending email."),
Err(_) => Redirect::to("/contact.html"),
}
}
async fn send_email(form_data: FormData) -> Result<(), Box<dyn Error>> {
let email = Message::builder()
.from(form_data.email.parse::<Mailbox>()?)
.from("webmail@distrust.co".parse()?)
.reply_to(form_data.email.parse::<Mailbox>()?)
.to("sales@distrust.co".parse()?)
.subject("New Website Form Inquiry")
.body(format!("Name: {}/nEmail: {}/nMessage: {}", form_data.name, form_data.email, form_data.message))?;
.body(format!(
"Name: {}/nEmail: {}/nMessage: {}",
form_data.name,
form_data.email,
form_data.message
))?;
let mailer = SmtpTransport::relay("smtp.distrust.co")?
let mailer = AsyncSmtpTransport::relay("smtp.distrust.co")?
.build();
mailer.send(&email)?;