Skip to main content

casdoor_rust_sdk/
sms.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 SmsForm<'a> {
22    content: &'a str,
23    receivers: &'a [&'a str],
24}
25
26impl Client {
27    /// Send an SMS with the default SMS provider of the organization.
28    pub async fn send_sms(&self, content: &str, receivers: &[&str]) -> Result<()> {
29        self.do_send_sms(content, receivers, &[]).await
30    }
31
32    /// Send an SMS with the given SMS provider.
33    pub async fn send_sms_by_provider(
34        &self,
35        content: &str,
36        provider: &str,
37        receivers: &[&str],
38    ) -> Result<()> {
39        self.do_send_sms(content, receivers, &[("provider", provider)])
40            .await
41    }
42
43    async fn do_send_sms(
44        &self,
45        content: &str,
46        receivers: &[&str],
47        query: &[(&str, &str)],
48    ) -> Result<()> {
49        let post_bytes = serde_json::to_vec(&SmsForm { content, receivers })?;
50
51        self.do_post("send-sms", query, PostBody::Raw(post_bytes))
52            .await?;
53
54        Ok(())
55    }
56}