22
33namespace InlineStudio \MailConnectors \Mailers \O365 ;
44
5+ use GuzzleHttp \Client as GuzzleClient ;
6+ use Illuminate \Support \Str ;
57use Microsoft \Graph \Graph ;
68use Microsoft \Graph \Model \Message ;
79use Microsoft \Graph \Model \UploadSession ;
8- use Symfony \Component \Mime \Email ;
910use Symfony \Component \Mime \Address ;
10- use Illuminate \Support \Str ;
11- use GuzzleHttp \Client as GuzzleClient ;
11+ use Symfony \Component \Mime \Email ;
1212
1313class Office365Connector
1414{
@@ -18,7 +18,9 @@ class Office365Connector
1818 protected Graph $ client ;
1919
2020 protected string $ clientId ;
21+
2122 protected string $ clientSecret ;
23+
2224 protected string $ tenant ;
2325
2426 protected const BYTE_TO_MB = 1048576 ;
@@ -38,8 +40,8 @@ public function __construct(string $clientId, string $clientSecret, string $tena
3840
3941 protected function getAccessToken (): string
4042 {
41- $ guzzle = new GuzzleClient () ;
42- $ url = 'https://login.microsoftonline.com/ ' . $ this ->tenant . '/oauth2/v2.0/token ' ;
43+ $ guzzle = new GuzzleClient ;
44+ $ url = 'https://login.microsoftonline.com/ ' . $ this ->tenant . '/oauth2/v2.0/token ' ;
4345 $ response = $ guzzle ->post (
4446 $ url ,
4547 [
@@ -75,20 +77,20 @@ public function sendMessageRequest(Email $message): Message
7577 $ this ->uploadLargeAttachments ($ message , $ draft ->getId ());
7678
7779 // Send the message
78- return $ this ->client ->createRequest (" POST " , " /users/ " . (current ($ message ->getFrom ())->getAddress ()) . " /messages/ " . $ draft ->getId () . " /send " )
80+ return $ this ->client ->createRequest (' POST ' , ' /users/ ' . (current ($ message ->getFrom ())->getAddress ()). ' /messages/ ' . $ draft ->getId (). ' /send ' )
7981 ->setReturnType (Message::class)
8082 ->execute ();
8183 }
8284
83- return $ this ->client ->createRequest (" POST " , " /users/ " . (current ($ message ->getFrom ())->getAddress ()) . " /sendmail " )
85+ return $ this ->client ->createRequest (' POST ' , ' /users/ ' . (current ($ message ->getFrom ())->getAddress ()). ' /sendmail ' )
8486 ->attachBody ($ this ->getBody ($ message , true ))
8587 ->setReturnType (Message::class)
8688 ->execute ();
8789 }
8890
8991 protected function createDraftMessage (Email $ message ): Message
9092 {
91- return $ this ->client ->createRequest (" POST " , " /users/ " . (current ($ message ->getFrom ())->getAddress ()) . " /messages " )
93+ return $ this ->client ->createRequest (' POST ' , ' /users/ ' . (current ($ message ->getFrom ())->getAddress ()). ' /messages ' )
9294 ->attachBody ($ this ->getBody ($ message , false , true ))
9395 ->setReturnType (Message::class)
9496 ->execute ();
@@ -100,7 +102,7 @@ protected function uploadLargeAttachments(Email $message, string $draftId): void
100102 $ fileName = $ attachment ->getPreparedHeaders ()->getHeaderParameter ('Content-Disposition ' , 'filename ' );
101103 $ content = $ attachment ->getBody ();
102104 $ fileSize = strlen ($ content );
103- $ size = $ fileSize / self ::BYTE_TO_MB ; //byte -> mb
105+ $ size = $ fileSize / self ::BYTE_TO_MB ; // byte -> mb
104106 $ id = Str::random (10 );
105107
106108 if ($ size <= 3 ) {
@@ -109,10 +111,10 @@ protected function uploadLargeAttachments(Email $message, string $draftId): void
109111 'name ' => $ fileName ,
110112 'contentType ' => $ attachment ->getPreparedHeaders ()->get ('Content-Type ' )->getBody (),
111113 'contentBytes ' => base64_encode ($ attachment ->getBody ()),
112- 'contentId ' => $ id
114+ 'contentId ' => $ id,
113115 ];
114116
115- $ this ->client ->createRequest (" POST " , " /users/ " . (current ($ message ->getFrom ())->getAddress ()) . " /messages/ " . $ draftId . " /attachments " )
117+ $ this ->client ->createRequest (' POST ' , ' /users/ ' . (current ($ message ->getFrom ())->getAddress ()). ' /messages/ ' . $ draftId. ' /attachments ' )
116118 ->attachBody ($ attachmentBody )
117119 ->setReturnType (UploadSession::class)
118120 ->execute ();
@@ -129,19 +131,19 @@ protected function chunkUpload(Email $message, string $draftId, string $fileName
129131 'attachmentType ' => 'file ' ,
130132 'name ' => $ fileName ,
131133 'size ' => $ fileSize ,
132- ]
134+ ],
133135 ];
134136
135- $ uploadSession = $ this ->client ->createRequest (" POST " , " /users/ " . (current ($ message ->getFrom ())->getAddress ()) . " /messages/ " . $ draftId . " /attachments/createUploadSession " )
137+ $ uploadSession = $ this ->client ->createRequest (' POST ' , ' /users/ ' . (current ($ message ->getFrom ())->getAddress ()). ' /messages/ ' . $ draftId. ' /attachments/createUploadSession ' )
136138 ->attachBody ($ attachmentMessage )
137139 ->setReturnType (UploadSession::class)
138140 ->execute ();
139141
140- $ fragSize = 1024 * 1024 * 4 ; //4mb at once...
142+ $ fragSize = 1024 * 1024 * 4 ; // 4mb at once...
141143 $ numFragments = ceil ($ fileSize / $ fragSize );
142144 $ contentChunked = str_split ($ content , $ fragSize );
143145 $ bytesRemaining = $ fileSize ;
144- $ guzzle = new GuzzleClient () ;
146+ $ guzzle = new GuzzleClient ;
145147
146148 $ i = 0 ;
147149 while ($ i < $ numFragments ) {
@@ -157,14 +159,14 @@ protected function chunkUpload(Email $message, string $draftId, string $fileName
157159 $ contentRange = "bytes {$ start }- {$ end }/ {$ fileSize }" ;
158160 $ headers = [
159161 'Content-Length ' => $ numBytes ,
160- 'Content-Range ' => $ contentRange
162+ 'Content-Range ' => $ contentRange,
161163 ];
162164
163165 $ guzzle ->put ($ uploadSession ->getUploadUrl (), [
164- 'headers ' => $ headers ,
165- 'body ' => $ data ,
166+ 'headers ' => $ headers ,
167+ 'body ' => $ data ,
166168 'allow_redirects ' => false ,
167- 'timeout ' => 1000
169+ 'timeout ' => 1000 ,
168170 ]);
169171
170172 $ bytesRemaining = $ bytesRemaining - $ chunkSize ;
@@ -177,7 +179,7 @@ protected function getBodySize(Email $message): float
177179 $ messageBody = $ this ->getBody ($ message , true );
178180 $ messageBodyLength = mb_strlen (json_encode ($ messageBody , JSON_NUMERIC_CHECK ), '8bit ' );
179181
180- return $ messageBodyLength / self ::BYTE_TO_MB ; //byte -> mb
182+ return $ messageBodyLength / self ::BYTE_TO_MB ; // byte -> mb
181183 }
182184
183185 /**
@@ -190,7 +192,7 @@ protected function getBody(Email $message, bool $withAttachments = false, bool $
190192 'emailAddress ' => [
191193 'address ' => current ($ message ->getFrom ())->getAddress (),
192194 'name ' => current ($ message ->getFrom ())->getName (),
193- ]
195+ ],
194196 ],
195197 'toRecipients ' => $ this ->getTo ($ message ),
196198 'ccRecipients ' => $ this ->getCc ($ message ),
@@ -199,23 +201,23 @@ protected function getBody(Email $message, bool $withAttachments = false, bool $
199201 'subject ' => $ message ->getSubject (),
200202 'body ' => [
201203 'contentType ' => $ message ->getHtmlBody () ? 'html ' : 'text ' ,
202- 'content ' => $ message ->getHtmlBody () ?: $ message ->getTextBody ()
203- ]
204+ 'content ' => $ message ->getHtmlBody () ?: $ message ->getTextBody (),
205+ ],
204206 ];
205207
206- if (!$ isDraft ) {
208+ if (! $ isDraft ) {
207209 $ messageData = ['message ' => $ messageData ];
208210 }
209211
210212 if (count ($ message ->getAttachments ()) > 0 && $ withAttachments ) {
211213 $ attachments = [];
212214 foreach ($ message ->getAttachments () as $ attachment ) {
213215 $ attachments [] = [
214- " @odata.type " => " #microsoft.graph.fileAttachment " ,
215- " name " => $ attachment ->getFilename (),
216- " contentType " => $ attachment ->getContentType (),
217- " contentBytes " => base64_encode ($ attachment ->getBody ()),
218- 'contentId ' => $ attachment ->getContentId ()
216+ ' @odata.type ' => ' #microsoft.graph.fileAttachment ' ,
217+ ' name ' => $ attachment ->getFilename (),
218+ ' contentType ' => $ attachment ->getContentType (),
219+ ' contentBytes ' => base64_encode ($ attachment ->getBody ()),
220+ 'contentId ' => $ attachment ->getContentId (),
219221 ];
220222 }
221223 $ messageData ['message ' ]['attachments ' ] = $ attachments ;
@@ -234,7 +236,7 @@ protected function getTo(Email $message): array
234236 'emailAddress ' => [
235237 'address ' => $ recipient ->getAddress (),
236238 'name ' => $ recipient ->getName (),
237- ]
239+ ],
238240 ]
239241 )->values ()->toArray ();
240242 }
@@ -245,11 +247,11 @@ protected function getTo(Email $message): array
245247 protected function getCc (Email $ message ): array
246248 {
247249 return collect ($ message ->getCc ())->map (
248- fn (Address $ cc ) => [
250+ fn (Address $ cc ) => [
249251 'emailAddress ' => [
250252 'address ' => $ cc ->getAddress (),
251253 'name ' => $ cc ->getName (),
252- ]
254+ ],
253255 ]
254256 )->values ()->toArray ();
255257 }
@@ -260,12 +262,11 @@ protected function getCc(Email $message): array
260262 protected function getReplyTo (Email $ message ): array
261263 {
262264 return collect ($ message ->getReplyTo ())->map (
263- fn (Address $ replyTo ) =>
264- [
265+ fn (Address $ replyTo ) => [
265266 'emailAddress ' => [
266267 'address ' => $ replyTo ->getAddress (),
267268 'name ' => $ replyTo ->getName (),
268- ]
269+ ],
269270 ]
270271 )->values ()->toArray ();
271272 }
@@ -280,7 +281,7 @@ protected function getBcc(Email $message): array
280281 'emailAddress ' => [
281282 'address ' => $ bcc ->getAddress (),
282283 'name ' => $ bcc ->getName (),
283- ]
284+ ],
284285 ]
285286 )->values ()->toArray ();
286287 }
0 commit comments