|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DaveismynameLaravel\MsGraph\Api; |
| 4 | + |
| 5 | +trait Emails { |
| 6 | + |
| 7 | + public function emails($limit = 25, $skip = 0, $folderId = null) |
| 8 | + { |
| 9 | + $messageQueryParams = array ( |
| 10 | + "\$skip" => $skip, |
| 11 | + "\$top" => $limit, |
| 12 | + "\$count" => "true", |
| 13 | + ); |
| 14 | + |
| 15 | + if ($folderId == null) { |
| 16 | + $folder = 'Inbox'; |
| 17 | + } else { |
| 18 | + $folder = $folderId; |
| 19 | + } |
| 20 | + |
| 21 | + //get inbox from folders list |
| 22 | + $folder = self::get("me/mailFolders?\$filter=startswith(displayName,'$folder')"); |
| 23 | + |
| 24 | + //folder id |
| 25 | + $inbox = $folder['value'][0]['id']; |
| 26 | + |
| 27 | + //get messages from inbox folder |
| 28 | + $emails = self::get("me/mailFolders/$inbox/messages?".http_build_query($messageQueryParams)); |
| 29 | + |
| 30 | + $total = $emails['@odata.count']; |
| 31 | + $previous = null; |
| 32 | + $next = null; |
| 33 | + if (isset($emails['@odata.nextLink'])) { |
| 34 | + $first = explode('$skip=', $emails['@odata.nextLink']); |
| 35 | + |
| 36 | + $skip = explode('&', $first[1]); |
| 37 | + $previous = $skip[0]-$limit; |
| 38 | + $next = $skip[0]; |
| 39 | + |
| 40 | + if ($previous < 0) { |
| 41 | + $previous = 0; |
| 42 | + } |
| 43 | + |
| 44 | + if ($next == $total) { |
| 45 | + $next = null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return [ |
| 50 | + 'emails' => $emails, |
| 51 | + 'total' => $total, |
| 52 | + 'previous' => $previous, |
| 53 | + 'next' => $next |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + public function emailAttachments($email_id) |
| 58 | + { |
| 59 | + return self::get("me/messages/".$email_id."/attachments"); |
| 60 | + } |
| 61 | + |
| 62 | + public function emailInlineAttachments($email) |
| 63 | + { |
| 64 | + $attachments = self::emailAttachments($email['id']); |
| 65 | + |
| 66 | + //replace every case of <img='cid:' with the base64 image |
| 67 | + $email['body']['content'] = preg_replace_callback( |
| 68 | + '~cid.*?"~', |
| 69 | + function($m) use($attachments) { |
| 70 | + |
| 71 | + //remove the last quote |
| 72 | + $parts = explode('"',$m[0]); |
| 73 | + |
| 74 | + //remove cid: |
| 75 | + $contentId = str_replace('cid:', '', $parts[0]); |
| 76 | + |
| 77 | + //loop over the attachments |
| 78 | + foreach ($attachments['value'] as $file) { |
| 79 | + //if there is a match |
| 80 | + if ($file['contentId'] == $contentId) { |
| 81 | + //return a base64 image with a quote |
| 82 | + return "data:".$file['contentType'].";base64,".$file['contentBytes'].'"'; |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + $email['body']['content'] |
| 87 | + ); |
| 88 | + |
| 89 | + return $email; |
| 90 | + } |
| 91 | + |
| 92 | + public function emailSend($subject, $message, $to, $cc, $bcc, $attachments = null) |
| 93 | + { |
| 94 | + //send an email to a draft |
| 95 | + $draft = self::post('me/messages', self::emailPrepare($subject, $message, $to, $cc, $bcc)); |
| 96 | + |
| 97 | + if ($attachments != null) { |
| 98 | + foreach($attachments as $file) { |
| 99 | + //create an attachment and send to the draft message based on the message id |
| 100 | + $attachment = self::post('me/messages/'.$draft['id'].'/attachments', $file); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + //send the draft message now it's complete |
| 105 | + return self::post('me/messages/'.$draft['id'].'/send', []); |
| 106 | + } |
| 107 | + |
| 108 | + public function emailSendReply($id, $message, $to, $cc, $bcc, $attachments = null) |
| 109 | + { |
| 110 | + //send an email to a draft |
| 111 | + $draft = self::post("me/messages/$id/createReplyAll", self::prepareReply($message, $to, $cc, $bcc)); |
| 112 | + |
| 113 | + if ($attachments != null) { |
| 114 | + foreach($attachments as $file) { |
| 115 | + //create an attachment and send to the draft message based on the message id |
| 116 | + $attachment = self::post('me/messages/'.$draft['id'].'/attachments', $file); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + //send the draft message now it's complete |
| 121 | + self::post('me/messages/'.$draft['id'].'/send', []); |
| 122 | + } |
| 123 | + |
| 124 | + public function emailSendForward($id, $message, $to, $cc, $bcc, $attachments = null) |
| 125 | + { |
| 126 | + //send an email to a draft |
| 127 | + $draft = self::post("me/messages/$id/createForward", self::emailPrepareReply($message, $to, $cc, $bcc)); |
| 128 | + |
| 129 | + if ($attachments != null) { |
| 130 | + foreach($attachments as $file) { |
| 131 | + //create an attachment and send to the draft message based on the message id |
| 132 | + $attachment = self::post('me/messages/'.$draft['id'].'/attachments', $file); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + //send the draft message now it's complete |
| 137 | + self::post('me/messages/'.$draft['id'].'/send', []); |
| 138 | + } |
| 139 | + |
| 140 | + protected static function emailPrepare($subject, $message, $to, $cc = null, $bcc = null) |
| 141 | + { |
| 142 | + |
| 143 | + $parts = explode(',', $to); |
| 144 | + $toArray = []; |
| 145 | + foreach($parts as $to) { |
| 146 | + $toArray[]["emailAddress"] = ["address" => $to]; |
| 147 | + } |
| 148 | + |
| 149 | + $ccArray = []; |
| 150 | + if ($cc != null) { |
| 151 | + $parts = explode(',', $cc); |
| 152 | + foreach($parts as $cc) { |
| 153 | + $ccArray[]["emailAddress"] = ["address" => $cc]; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + $bccArray = []; |
| 158 | + if ($bcc != null) { |
| 159 | + $parts = explode(',', $bcc); |
| 160 | + foreach($parts as $bcc) { |
| 161 | + $bccArray[]["emailAddress"] = ["address" => $bcc]; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return [ |
| 166 | + "subject" => $subject, |
| 167 | + "body" => [ |
| 168 | + "contentType" => "html", |
| 169 | + "content" => $message |
| 170 | + ], |
| 171 | + "toRecipients" => $toArray, |
| 172 | + "ccRecipients" => $ccArray, |
| 173 | + "bccRecipients" => $bccArray |
| 174 | + ]; |
| 175 | + } |
| 176 | + |
| 177 | + protected static function emailPrepareReply($message, $to, $cc = null, $bcc = null) |
| 178 | + { |
| 179 | + |
| 180 | + $parts = explode(',', $to); |
| 181 | + $toArray = []; |
| 182 | + foreach($parts as $to) { |
| 183 | + $toArray[]["emailAddress"] = ["address" => $to]; |
| 184 | + } |
| 185 | + |
| 186 | + $ccArray = []; |
| 187 | + if ($cc != null) { |
| 188 | + $parts = explode(',', $cc); |
| 189 | + foreach($parts as $cc) { |
| 190 | + $ccArray[]["emailAddress"] = ["address" => $cc]; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + $bccArray = []; |
| 195 | + if ($bcc != null) { |
| 196 | + $parts = explode(',', $bcc); |
| 197 | + foreach($parts as $bcc) { |
| 198 | + $bccArray[]["emailAddress"] = ["address" => $bcc]; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return [ |
| 203 | + "comment" => $message, |
| 204 | + "toRecipients" => $toArray, |
| 205 | + "ccRecipients" => $ccArray, |
| 206 | + "bccRecipients" => $bccArray |
| 207 | + ]; |
| 208 | + } |
| 209 | +} |
0 commit comments