rustango_cms/notify/channels/
email.rs1use crate::notify::{ConfigField, DeliverError, Delivery, Notification, NotificationChannel};
4
5#[derive(Default)]
9pub struct EmailChannel;
10
11#[async_trait::async_trait]
12impl NotificationChannel for EmailChannel {
13 fn kind(&self) -> &'static str {
14 "email"
15 }
16 fn label(&self) -> &'static str {
17 "Email"
18 }
19 fn help(&self) -> &'static str {
20 "Sends through this site's configured mailer. Needs a mailer wired by the host app."
21 }
22 fn config_fields(&self) -> Vec<ConfigField> {
23 vec![ConfigField {
24 name: "recipients",
25 label: "Recipients",
26 help: "Comma-separated addresses.",
27 secret: false,
28 required: true,
29 }]
30 }
31
32 async fn deliver(&self, d: &Delivery<'_>, msg: &Notification) -> Result<(), DeliverError> {
33 let Some(mailer) = d.mailer else {
34 return Err(DeliverError::Permanent(
38 "no mailer configured on this site".to_owned(),
39 ));
40 };
41 let recipients: Vec<&str> = d
42 .config
43 .get("recipients")
44 .map(String::as_str)
45 .unwrap_or_default()
46 .split(',')
47 .map(str::trim)
48 .filter(|s| !s.is_empty())
49 .collect();
50 if recipients.is_empty() {
51 return Err(DeliverError::Permanent("no recipients set".to_owned()));
52 }
53 let body = msg.as_text();
54 let mut last_err: Option<String> = None;
55 for to in recipients {
56 let email = rustango::email::Email::new()
57 .to(to.to_owned())
58 .from(d.mailer_from.to_owned())
59 .subject(msg.title.clone())
60 .body(body.clone())
61 .header(
62 "X-Rustango-Cms-Notification".to_owned(),
63 msg.event.clone(),
64 );
65 if let Err(e) = email.send(mailer).await {
66 last_err = Some(e.to_string());
67 }
68 }
69 match last_err {
73 None => Ok(()),
74 Some(e) => Err(DeliverError::Transient(e)),
75 }
76 }
77}
78
79crate::register_notification_channel!(EmailChannel);