-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresend.rs
49 lines (39 loc) · 1.07 KB
/
resend.rs
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
48
49
//! [`EmailProvider`] implementation using [`Resend`](https://resend.com/).
use resend_rs::{types::SendEmail, Client};
use crate::info;
use super::{email_provider::EmailProvider, error::EmailError, EnvLoader};
#[derive(Default, Debug)]
pub struct Resend {
api_key: Option<String>,
}
impl Resend {
pub(crate) fn new(env_loader: &EnvLoader) -> Self {
Self {
api_key: env_loader.api_key.clone(),
}
}
}
impl EmailProvider for Resend {
fn send_email(
&self,
from_address: &str,
recipient_addresses: Vec<&str>,
subject: &str,
contents: &str,
) -> Result<(), EmailError> {
let api_key = self
.api_key
.as_ref()
.ok_or_else(|| EmailError::Config("Cannot use Resend without API_KEY".to_owned()))
.cloned()?;
let resend = Client::new(&api_key);
let email = SendEmail::new(from_address, recipient_addresses, subject).with_html(contents);
match resend.emails.send(email) {
Ok(_id) => {
info!("Email request sent");
}
Err(e) => return Err(EmailError::from(e)),
}
Ok(())
}
}