Skip to main content

u_sdk/email/
send_email.rs

1use super::Client;
2use super::Error;
3use super::utils::parse_json_response;
4use bon::Builder;
5use serde::{Deserialize, Serialize};
6use u_sdk_common::helper::into_header_map;
7use u_sdk_common::open_api_sign::{SignParams, get_openapi_request_header};
8
9//region response
10#[derive(Deserialize, Debug)]
11#[serde(rename_all = "PascalCase")]
12pub struct SingleSendEmailResult {
13    pub env_id: String,
14    pub request_id: String,
15}
16//endregion
17
18#[serde_with::skip_serializing_none]
19#[derive(Builder, Serialize)]
20#[serde(rename_all = "PascalCase")]
21pub struct SingleSendEmail<'a> {
22    #[builder(start_fn)]
23    #[serde(skip_serializing)]
24    client: &'a Client,
25
26    account_name: &'a str,
27    address_type: &'a str,
28    reply_to_address: &'a str,
29    subject: &'a str,
30    to_address: &'a str,
31    click_trace: Option<&'a str>,
32    from_alias: Option<&'a str>,
33    html_body: Option<&'a str>,
34    tag_name: Option<&'a str>,
35    text_body: Option<&'a str>,
36    reply_address: Option<&'a str>,
37    reply_address_alias: Option<&'a str>,
38}
39
40impl Client {
41    pub fn single_send_email(&self) -> SingleSendEmailBuilder<'_> {
42        SingleSendEmail::builder(self)
43    }
44}
45
46impl SingleSendEmail<'_> {
47    pub async fn send(&self) -> Result<SingleSendEmailResult, Error> {
48        // HtmlBody 和 TextBody 是针对不同类型的邮件内容,两者必须传其一
49        if self.html_body.is_none() && self.text_body.is_none() {
50            return Err(Error::Common(
51                "one of html_body or text_body must be set".to_owned(),
52            ));
53        }
54
55        let client = self.client;
56        let creds = client.credentials_provider.load().await?;
57
58        let sign_params = SignParams {
59            req_method: "GET",
60            host: &client.host,
61            query_map: self,
62            x_acs_action: "SingleSendMail",
63            x_acs_version: "2015-11-23",
64            x_acs_security_token: creds.sts_security_token.as_deref(),
65            request_body: None,
66            style: &client.style,
67        };
68
69        let (headers, url_) =
70            get_openapi_request_header(&creds.access_key_secret, &creds.access_key_id, sign_params)
71                .map_err(|e| Error::Common(format!("get_common_headers error: {}", e)))?;
72
73        let resp = self
74            .client
75            .http_client
76            .get(url_)
77            .headers(into_header_map(headers))
78            .send()
79            .await?;
80
81        let resp = parse_json_response(resp).await?;
82        Ok(resp)
83    }
84}