Skip to main content

casdoor_rust_sdk/
email.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::Serialize;
16
17use crate::client::{Client, PostBody};
18use crate::error::Result;
19
20#[derive(Serialize)]
21struct EmailForm<'a> {
22    title: &'a str,
23    content: &'a str,
24    sender: &'a str,
25    receivers: &'a [&'a str],
26}
27
28impl Client {
29    /// Send an email with the default email provider of the organization.
30    pub async fn send_email(
31        &self,
32        title: &str,
33        content: &str,
34        sender: &str,
35        receivers: &[&str],
36    ) -> Result<()> {
37        self.do_send_email(title, content, sender, receivers, &[])
38            .await
39    }
40
41    /// Send an email with the given email provider.
42    pub async fn send_email_by_provider(
43        &self,
44        title: &str,
45        content: &str,
46        sender: &str,
47        provider: &str,
48        receivers: &[&str],
49    ) -> Result<()> {
50        self.do_send_email(title, content, sender, receivers, &[("provider", provider)])
51            .await
52    }
53
54    async fn do_send_email(
55        &self,
56        title: &str,
57        content: &str,
58        sender: &str,
59        receivers: &[&str],
60        query: &[(&str, &str)],
61    ) -> Result<()> {
62        let post_bytes = serde_json::to_vec(&EmailForm {
63            title,
64            content,
65            sender,
66            receivers,
67        })?;
68
69        self.do_post("send-email", query, PostBody::Raw(post_bytes))
70            .await?;
71
72        Ok(())
73    }
74}