io_msgraph/v1/rest/users/
send_mail.rs1use alloc::format;
7
8use base64::{Engine, engine::general_purpose::STANDARD};
9use io_http::rfc6750::bearer::HttpAuthBearer;
10use log::{debug, trace};
11use serde::Serialize;
12use url::Url;
13
14use crate::{
15 coroutine::*,
16 msgraph_try,
17 v1::{
18 rest::users::messages::MsgraphMessage,
19 send::{
20 MSGRAPH_API_BASE, MsgraphNoResponse, MsgraphSend, MsgraphSendError, MsgraphSendOutput,
21 user_path,
22 },
23 },
24};
25
26#[derive(Debug, Serialize)]
27#[serde(rename_all = "camelCase")]
28struct MsgraphMailSendRequest<'a> {
29 message: &'a MsgraphMessage,
30 save_to_sent_items: bool,
31}
32
33pub struct MsgraphMailSend {
35 send: MsgraphSend<MsgraphNoResponse>,
36}
37
38impl MsgraphMailSend {
39 pub fn new(
42 auth: &HttpAuthBearer,
43 user_id: &str,
44 message: &MsgraphMessage,
45 save_to_sent_items: bool,
46 ) -> Result<Self, MsgraphSendError> {
47 debug!("prepare microsoft graph mail send (json)");
48 trace!("message: {message:?}");
49 trace!("save_to_sent_items: {save_to_sent_items:?}");
50
51 let url = mail_url(user_id)?;
52 let body = MsgraphMailSendRequest {
53 message,
54 save_to_sent_items,
55 };
56 let send = MsgraphSend::post_json(auth, url, &body)?;
57
58 Ok(Self { send })
59 }
60}
61
62impl MsgraphCoroutine for MsgraphMailSend {
63 type Yield = MsgraphYield;
64 type Return = Result<MsgraphSendOutput<MsgraphNoResponse>, MsgraphSendError>;
65
66 fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
67 let out = msgraph_try!(&mut self.send, arg);
68 debug!("mail sent (json)");
69 trace!("out: {out:?}");
70 MsgraphCoroutineState::Complete(Ok(out))
71 }
72}
73
74pub struct MsgraphMailSendMime {
77 send: MsgraphSend<MsgraphNoResponse>,
78}
79
80impl MsgraphMailSendMime {
81 pub fn new(auth: &HttpAuthBearer, user_id: &str, raw: &[u8]) -> Result<Self, MsgraphSendError> {
83 debug!("prepare microsoft graph mail send (mime)");
84 trace!("raw len: {}", raw.len());
85
86 let url = mail_url(user_id)?;
87 let body = STANDARD.encode(raw).into_bytes();
88 let send = MsgraphSend::post_text(auth, url, body);
89
90 Ok(Self { send })
91 }
92}
93
94impl MsgraphCoroutine for MsgraphMailSendMime {
95 type Yield = MsgraphYield;
96 type Return = Result<MsgraphSendOutput<MsgraphNoResponse>, MsgraphSendError>;
97
98 fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
99 let out = msgraph_try!(&mut self.send, arg);
100 debug!("mail sent (mime)");
101 trace!("out: {out:?}");
102 MsgraphCoroutineState::Complete(Ok(out))
103 }
104}
105
106fn mail_url(user_id: &str) -> Result<Url, MsgraphSendError> {
107 let user = user_path(user_id);
108 let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/sendMail"))?;
109 Ok(url)
110}