forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_service.rs
More file actions
47 lines (41 loc) · 1.38 KB
/
Copy pathemail_service.rs
File metadata and controls
47 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::utils::error::AppError;
use reqwest::Client;
use serde_json::json;
#[derive(Clone)]
pub struct EmailService {
api_key: String,
client: Client,
}
impl EmailService {
pub fn new(api_key: String) -> Self {
Self {
api_key,
client: Client::new(),
}
}
pub async fn send_otp_email(&self, email: &str, otp: &str) -> Result<(), AppError> {
let body = json!({
"sender": { "email": "no-reply@txio-backend.com", "name": "txio Team" },
"to": [{ "email": email }],
"subject": "Your txio OTP",
"htmlContent": format!("<p>Your verification code is: <strong>{}</strong></p><p>This code will expire in 10 minutes.</p>", otp)
});
let response = self
.client
.post("https://api.brevo.com/v3/smtp/email")
.header("api-key", &self.api_key)
.header("Content-Type", "application/json")
.json(&body)
.send()
.await
.map_err(|e| AppError::ExternalService(format!("Failed to send email: {}", e)))?;
if !response.status().is_success() {
let error_text = response.text().await.unwrap_or_default();
return Err(AppError::ExternalService(format!(
"Brevo API error: {}",
error_text
)));
}
Ok(())
}
}