WIP email sending for registration

This commit is contained in:
2023-09-27 23:11:52 -04:00
parent 8d1bffc899
commit f938a6b46b
8 changed files with 135 additions and 4 deletions

View File

@@ -1,13 +1,16 @@
use axum::response::{IntoResponse, Response};
use axum::TypedHeader;
use axum::{extract::State, Form};
use lettre::message::header::ContentType;
use lettre::message::{Mailbox, Message};
use lettre::{SmtpTransport, Transport};
use maud::html;
use serde::Deserialize;
use serde_with::{serde_as, NoneAsEmptyString};
use sqlx::PgPool;
use crate::error::{Error, Result};
use crate::htmx::{HXTarget, HXRedirect};
use crate::htmx::{HXRedirect, HXTarget};
use crate::models::user::{AuthContext, CreateUser, User};
use crate::partials::layout::Layout;
use crate::partials::register_form::{register_form, RegisterFormProps};
@@ -38,6 +41,7 @@ pub async fn get(hx_target: Option<TypedHeader<HXTarget>>, layout: Layout) -> Re
pub async fn post(
State(pool): State<PgPool>,
State(mailer): State<SmtpTransport>,
mut auth: AuthContext,
Form(register): Form<Register>,
) -> Result<Response> {
@@ -111,6 +115,28 @@ pub async fn post(
return Err(err);
}
};
// TODO: don't 500 error on email send failure, render form with error message instead
let mailbox = Mailbox::new(
user.name.clone(),
user.email.parse().map_err(|_| Error::InternalServerError)?,
);
let email = Message::builder()
// TODO: make from address configurable and store in config already parsed
.from("crawlnicle <accounts@mail.crawlnicle.com>".parse().unwrap())
.to(mailbox)
.subject("Welcome to crawlnicle, please confirm your email address")
.header(ContentType::TEXT_PLAIN)
// TODO: fill in email body, use maud to create HTML body
.body(String::from("TODO"))
.map_err(|_| Error::InternalServerError)?;
// TODO: do email sending in a background async task
// TODO: notify the user that email has been sent somehow
mailer
.send(&email)
.map_err(|_| Error::InternalServerError)?;
auth.login(&user)
.await
.map_err(|_| Error::InternalServerError)?;