rss2email_lib/email/
resend.rs

1//! [`EmailProvider`] implementation using [`Resend`](https://resend.com/).
2
3use resend_rs::{mail::Mail, resend_client::ResendClient};
4
5use crate::info;
6
7use super::{email_provider::EmailProvider, error::EmailError, EnvLoader};
8
9#[derive(Default, Debug)]
10pub struct Resend {
11  api_key: Option<String>,
12}
13
14impl Resend {
15  pub(crate) fn new(env_loader: &EnvLoader) -> Self {
16    Self {
17      api_key: env_loader.api_key.clone(),
18    }
19  }
20}
21
22impl EmailProvider for Resend {
23  fn send_email(
24    &self,
25    from_address: &str,
26    recipient_addresses: Vec<&str>,
27    subject: &str,
28    contents: &str,
29  ) -> Result<(), EmailError> {
30    let api_key = self
31      .api_key
32      .as_ref()
33      .ok_or_else(|| EmailError::Config("Cannot use Resend without API_KEY".to_owned()))
34      .cloned()?;
35
36    let client = ResendClient::new(api_key);
37
38    let mail = Mail::new(from_address, &recipient_addresses, subject, contents);
39
40    match client.send(mail) {
41      Ok(()) => {
42        info!("Email request sent");
43      }
44      Err(e) => return Err(EmailError::from(e)),
45    }
46
47    Ok(())
48  }
49}