Skip to content

Commit

Permalink
✨ Async mail transport
Browse files Browse the repository at this point in the history
  • Loading branch information
wrenger committed Aug 18, 2024
1 parent d84d9f1 commit edb4480
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ hyper-util = "0.1"
lettre = { version = "0.11", default-features = false, features = [
"builder",
"smtp-transport",
"rustls-tls",
"tokio1",
"tokio1-rustls-tls",
] }
oauth2 = { version = "5.0.0-alpha.4" }
rand = "0.8"
Expand Down
14 changes: 8 additions & 6 deletions src/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use email_address::EmailAddress;
use lettre::message::header::ContentType;
use lettre::message::{Mailbox, SinglePartBuilder};
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Address, Message, SmtpTransport, Transport};
use lettre::{Address, AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor};
use tracing::{error, info};
use unicode_normalization::UnicodeNormalization;

Expand All @@ -15,7 +15,7 @@ pub fn account_is_valid(account: &str) -> bool {
EmailAddress::is_valid_local_part(account)
}

pub fn send(
pub async fn send(
host: &str,
password: &str,
from: &str,
Expand Down Expand Up @@ -46,13 +46,13 @@ pub fn send(
)?;

// Open tls encrypted smtp connection
let mailer = SmtpTransport::relay(host)?
let mailer = AsyncSmtpTransport::<Tokio1Executor>::relay(host)?
.credentials(Credentials::new(from.to_string(), password.to_string()))
.timeout(Some(Duration::from_secs(1)))
.build();

// Send the email
mailer.send(&email)?;
mailer.send(email).await?;
Ok(())
}

Expand All @@ -77,9 +77,10 @@ impl From<lettre::transport::smtp::Error> for Error {

#[cfg(test)]
mod tests {
#[test]
#[tokio::test]
#[ignore]
fn send_mail() {
async fn send_mail() {
crate::logging();
super::send(
&std::env::var("SBV_MAIL_HOST").unwrap(),
&std::env::var("SBV_MAIL_PASSWORD").unwrap(),
Expand All @@ -88,6 +89,7 @@ mod tests {
"Test Mail 🚧",
"Test Content 🚧",
)
.await
.unwrap();
}
}
3 changes: 2 additions & 1 deletion src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ async fn mail_notify(
account,
&subject,
&body,
)?;
)
.await?;
}
Ok(())
}

0 comments on commit edb4480

Please sign in to comment.