io_msgraph/v1/rest/users/
send_mail.rs1use alloc::{format, vec::Vec};
7
8use base64_codec::codec::Base64Codec;
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 mut body = Vec::new();
88 Base64Codec::default().encode_vec(raw, &mut body);
89
90 let send = MsgraphSend::post_text(auth, url, body);
91
92 Ok(Self { send })
93 }
94}
95
96impl MsgraphCoroutine for MsgraphMailSendMime {
97 type Yield = MsgraphYield;
98 type Return = Result<MsgraphSendOutput<MsgraphNoResponse>, MsgraphSendError>;
99
100 fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
101 let out = msgraph_try!(&mut self.send, arg);
102 debug!("mail sent (mime)");
103 trace!("out: {out:?}");
104 MsgraphCoroutineState::Complete(Ok(out))
105 }
106}
107
108fn mail_url(user_id: &str) -> Result<Url, MsgraphSendError> {
109 let user = user_path(user_id);
110 let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/sendMail"))?;
111 Ok(url)
112}