66use LLoadout \Microsoftgraph \Traits \Authenticate ;
77use LLoadout \Microsoftgraph \Traits \Connect ;
88
9+ /**
10+ * Mail class for interacting with Microsoft Graph API's mail functionality
11+ *
12+ * This class provides methods to interact with Microsoft Graph API's mail features including:
13+ * - Sending emails with attachments
14+ * - Managing mail folders
15+ * - Reading and managing messages
16+ * - Moving messages between folders
17+ * - Retrieving message attachments
18+ *
19+ * @package LLoadout\Microsoftgraph
20+ */
921class Mail
1022{
1123 use Authenticate, Connect;
1224
25+ /**
26+ * Send an email using Microsoft Graph API
27+ *
28+ * @param mixed $mailable The mailable object containing email details
29+ * @return void
30+ */
1331 public function sendMail ($ mailable ): void
1432 {
1533 $ this ->post ('/me/sendMail ' , $ this ->getBody ($ mailable ));
1634 }
1735
36+ /**
37+ * Prepare the email body for sending
38+ *
39+ * @param mixed $mailable The mailable object
40+ * @return array The formatted email body
41+ */
1842 protected function getBody ($ mailable )
1943 {
2044 $ html = $ mailable ->getHtmlBody ();
@@ -40,6 +64,12 @@ protected function getBody($mailable)
4064 ]);
4165 }
4266
67+ /**
68+ * Format email recipients into Microsoft Graph API format
69+ *
70+ * @param mixed $recipients Single recipient or array of recipients
71+ * @return array Formatted recipients array
72+ */
4373 protected function formatRecipients ($ recipients ): array
4474 {
4575 $ addresses = [];
@@ -64,6 +94,12 @@ protected function formatRecipients($recipients): array
6494 return $ addresses ;
6595 }
6696
97+ /**
98+ * Format email content into Microsoft Graph API format
99+ *
100+ * @param string $html HTML content of the email
101+ * @return array Formatted content array
102+ */
67103 private function getContent ($ html ): array
68104 {
69105 return [
@@ -72,6 +108,12 @@ private function getContent($html): array
72108 ];
73109 }
74110
111+ /**
112+ * Convert attachments into Microsoft Graph API format
113+ *
114+ * @param array $attachments Array of attachment files
115+ * @return array Formatted attachments array
116+ */
75117 protected function toAttachmentCollection ($ attachments ): array
76118 {
77119 $ collection = [];
@@ -97,23 +139,43 @@ protected function toAttachmentCollection($attachments): array
97139 return $ collection ;
98140 }
99141
142+ /**
143+ * Get all mail folders for the authenticated user
144+ *
145+ * @return array Mail folders
146+ */
100147 public function getMailFolders ()
101148 {
102149 $ url ='/me/mailfolders ' ;
103150
104151 return $ this ->get ($ url );
105152 }
106153
154+ /**
155+ * Get subfolders for a specific mail folder
156+ *
157+ * @param string $id Parent folder ID
158+ * @return array Subfolders
159+ */
107160 public function getSubFolders ($ id )
108161 {
109162 $ url = '/me/mailfolders/ ' . $ id . '/childFolders ' ;
110163
111164 return $ this ->get ($ url );
112165 }
113166
167+ /**
168+ * Get messages from a specific mail folder
169+ *
170+ * @param string $folder Folder name (default: 'inbox')
171+ * @param bool $isRead Include read messages (default: true)
172+ * @param int $skip Number of messages to skip (default: 0)
173+ * @param int $limit Maximum number of messages to return (default: 20)
174+ * @return array Messages
175+ */
114176 public function getMailMessagesFromFolder ($ folder = 'inbox ' , $ isRead = true , $ skip = 0 , $ limit = 20 )
115177 {
116- $ url = '/me/mailfolders/ ' . $ folder . '/messages?$select=Id,ReceivedDateTime,Subject,Sender,ToRecipients,From,HasAttachments,InternetMessageHeaders&$skip= ' .$ skip .'&$top= ' .$ limit ;
178+ $ url = '/me/mailfolders/ ' . $ folder . '/messages?$select=Id,ReceivedDateTime,Subject,Sender,ToRecipients,From,Body, HasAttachments,InternetMessageHeaders&$skip= ' .$ skip .'&$top= ' .$ limit ;
117179 if (! $ isRead ) {
118180 $ url .= '&$filter=isRead ne true ' ;
119181 }
@@ -131,33 +193,60 @@ public function getMailMessagesFromFolder($folder = 'inbox', $isRead = true, $sk
131193 'from ' => $ mail ['from ' ]['emailAddress ' ],
132194 'to ' => ! blank ($ to ) ? $ to : optional ($ mail ['toRecipients ' ])[0 ]['emailAddress ' ]['address ' ],
133195 'attachements ' => $ mail ['hasAttachments ' ],
196+ 'body ' => $ mail ['body ' ]['content ' ],
134197 ];
135198 }
136199
137200 return $ mails ;
138201 }
139202
203+ /**
204+ * Update a message
205+ *
206+ * @param string $id Message ID
207+ * @param array $data Update data
208+ * @return mixed API response
209+ */
140210 public function updateMessage ($ id , $ data )
141211 {
142212 $ url = '/me/messages/ ' . $ id ;
143213
144214 return $ this ->patch ($ url , $ data );
145215 }
146216
217+ /**
218+ * Move a message to a different folder
219+ *
220+ * @param string $id Message ID
221+ * @param string $destinationId Destination folder ID
222+ * @return mixed API response
223+ */
147224 public function moveMessage ($ id , $ destinationId )
148225 {
149226 $ url = '/me/messages/ ' . $ id . '/move ' ;
150227
151228 return $ this ->post ($ url , ['destinationId ' => $ destinationId ]);
152229 }
153230
231+ /**
232+ * Get a specific message by ID
233+ *
234+ * @param string $id Message ID
235+ * @return mixed Message details
236+ */
154237 public function getMessage ($ id )
155238 {
156239 $ url = config ('socialite.office365.api_url ' ) . '/me/messages/ ' . $ id . '?$select=Id,ReceivedDateTime,createdDateTime,Subject,Sender,ToRecipients,From,HasAttachments,InternetMessageHeaders&$top=10&$skip=0 ' ;
157240
158241 return $ this ->get ($ url );
159242 }
160243
244+ /**
245+ * Get attachments for a specific message
246+ *
247+ * @param string $id Message ID
248+ * @return mixed Message attachments
249+ */
161250 public function getMessageAttachements ($ id )
162251 {
163252 $ url = '/me/messages/ ' . $ id . '/attachments ' ;
0 commit comments