Skip to content

Commit

Permalink
Merge pull request #1789 from fastn-stack/feat/mail-in-fastnds
Browse files Browse the repository at this point in the history
move mail.rs to fastn-ds
  • Loading branch information
Arpita-Jaiswal authored Mar 13, 2024
2 parents bd42f8d + cf96ba8 commit 2e77e6d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,17 @@ pub(crate) async fn create_and_send_confirmation_email(

tracing::info!("confirmation link: {}", &confirmation_link);

fastn_core::mail::Mailer::send_raw(
req_config
.config
.ds
.env_bool("FASTN_ENABLE_EMAIL", true)
.await
.unwrap_or(true),
&req_config.config.ds,
format!("{} <{}>", name, email)
.parse::<lettre::message::Mailbox>()
.unwrap(),
"Verify your email",
confirmation_mail_body(html, &confirmation_link),
)
.await
.map_err(|e| fastn_core::Error::generic(format!("failed to send email: {e}")))?;
req_config
.config
.ds
.send_email(
(&name, &email),
"Verify your email",
confirmation_mail_body(html, &confirmation_link),
fastn_ds::mail::EmailKind::AccountVerification,
)
.await
.map_err(|e| fastn_core::Error::generic(format!("failed to send email: {e}")))?;

Ok((confirmation_link, session_id))
}
22 changes: 11 additions & 11 deletions fastn-core/src/auth/email_password/set_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,17 @@ pub(crate) async fn forgot_password_request(
println!("RESET LINK: {}", &reset_link);
}

fastn_core::mail::Mailer::send_raw(
enable_email,
&req_config.config.ds,
format!("{} <{}>", user.name, email.0)
.parse::<lettre::message::Mailbox>()
.unwrap(),
"Reset your password",
html,
)
.await
.map_err(|e| fastn_core::Error::generic(format!("failed to send email: {e}")))?;
req_config
.config
.ds
.send_email(
(&user.name, &email.0),
"Reset your password",
html,
fastn_ds::mail::EmailKind::PasswordReset,
)
.await
.map_err(|e| fastn_core::Error::generic(format!("failed to send email: {e}")))?;

let resp_body = serde_json::json!({
"success": true,
Expand Down
1 change: 0 additions & 1 deletion fastn-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ mod version;
pub mod catch_panic;
pub(crate) mod google_sheets;
mod library2022;
mod mail;

pub(crate) use auto_import::AutoImport;
pub use commands::{
Expand Down
3 changes: 2 additions & 1 deletion fastn-ds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ thiserror.workspace = true
ftd.workspace = true
actix-web.workspace = true
camino.workspace = true
async-trait = "0.1"
async-trait.workspace = true
ignore.workspace = true
dirs.workspace = true
tracing.workspace = true
lettre.workspace = true
reqwest.workspace = true
once_cell.workspace = true
url.workspace = true
Expand Down
28 changes: 28 additions & 0 deletions fastn-ds/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate self as fastn_ds;

pub mod http;
pub mod mail;
mod utils;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -279,6 +280,33 @@ impl DocumentStore {
pub async fn env(&self, key: &str) -> Result<String, EnvironmentError> {
std::env::var(key).map_err(|_| EnvironmentError::NotSet(key.to_string()))
}
/// Send an email
/// to: (name, email)
pub async fn send_email(
&self,
to: (&str, &str),
subject: &str,
body_html: String,
mkind: fastn_ds::mail::EmailKind,
) -> Result<(), fastn_ds::mail::MailError> {
let enable_email = self
.env_bool("FASTN_ENABLE_EMAIL", true)
.await
.unwrap_or(true);

let (name, email) = to;

tracing::info!("sending mail of {mkind}");

fastn_ds::mail::Mailer::send_raw(
enable_email,
self,
format!("{} <{}>", name, email).parse()?,
subject,
body_html,
)
.await
}

// This method will connect client request to the out of the world
#[tracing::instrument(skip(req, extra_headers))]
Expand Down
14 changes: 14 additions & 0 deletions fastn-core/src/mail.rs → fastn-ds/src/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,17 @@ impl Mailer {
Ok(())
}
}

pub enum EmailKind {
AccountVerification,
PasswordReset,
}

impl std::fmt::Display for EmailKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EmailKind::AccountVerification => write!(f, "account-verification"),
EmailKind::PasswordReset => write!(f, "password-reset"),
}
}
}

0 comments on commit 2e77e6d

Please sign in to comment.