Skip to content

Commit f687afa

Browse files
committed
feat: Replace tokio::sync::mpsc with AsyncSmtpTransport
1 parent 54d34f7 commit f687afa

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

app/services/email/mod.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ lazy_static! {
3030

3131
#[derive(Clone)]
3232
pub struct EmailEvent {
33-
tx : Arc<tokio::sync::mpsc::UnboundedSender<EmailType>>,
33+
tx : Arc<AsyncSmtpTransport<Tokio1Executor>>,
3434
}
3535

3636
impl EmailEvent {
@@ -42,35 +42,23 @@ impl EmailEvent {
4242
.credentials(creds)
4343
.build();
4444

45-
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
46-
tokio::spawn(async move {
47-
EmailEvent::listen(rx, mailer).await;
48-
});
49-
Ok(EmailEvent { tx : Arc::new(tx) })
45+
Ok(EmailEvent { tx : Arc::new(mailer) })
5046
}
5147
pub async fn send(&self, email_type : EmailType) {
52-
self.tx.send(email_type).ok();
53-
}
54-
pub async fn listen(
55-
mut rx : tokio::sync::mpsc::UnboundedReceiver<EmailType>,
56-
mailer : AsyncSmtpTransport<Tokio1Executor>,
57-
) {
58-
while let Some(email_type) = rx.recv().await {
59-
info!("Will Send Email to {:?}",email_type);
60-
match email_type {
61-
EmailType::Captcha(x) => {
62-
let email = Message::builder()
63-
.from(SMTP_USERNAME.to_owned().parse().unwrap())
64-
.reply_to(SMTP_USERNAME.to_owned().parse().unwrap())
65-
.to(x.email.parse().unwrap())
66-
.subject("GitData Code")
67-
.header(ContentType::TEXT_HTML)
68-
.body(CAPTCHA.replace("123456", &*x.code))
69-
.unwrap();
70-
match mailer.send(email).await {
71-
Ok(_) => info!("Email sent {} successfully!", x.email),
72-
Err(e) => error!("Could not send email: {e:?}"),
73-
}
48+
info!("Will Send Email to {:?}",email_type);
49+
match email_type {
50+
EmailType::Captcha(x) => {
51+
let email = Message::builder()
52+
.from(SMTP_USERNAME.to_owned().parse().unwrap())
53+
.reply_to(SMTP_USERNAME.to_owned().parse().unwrap())
54+
.to(x.email.parse().unwrap())
55+
.subject("GitData Code")
56+
.header(ContentType::TEXT_HTML)
57+
.body(CAPTCHA.replace("123456", &*x.code))
58+
.unwrap();
59+
match self.tx.send(email).await {
60+
Ok(_) => info!("Email sent {} successfully!", x.email),
61+
Err(e) => error!("Could not send email: {e:?}"),
7462
}
7563
}
7664
}

0 commit comments

Comments
 (0)