Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
99 / 99 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
Office365 | |
100.00% |
99 / 99 |
|
100.00% |
1 / 1 |
22 | |
100.00% |
1 / 1 |
send | |
100.00% |
99 / 99 |
|
100.00% |
1 / 1 |
22 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Mail\Transport; |
15 | |
16 | use Pop\Http; |
17 | use Pop\Mail\Api\AbstractOffice365; |
18 | use Pop\Mail\Message; |
19 | use Pop\Mail\Message\Html; |
20 | use Pop\Mail\Message\Text; |
21 | use Pop\Mail\Message\Attachment; |
22 | |
23 | /** |
24 | * Mailgun API transport class |
25 | * |
26 | * @category Pop |
27 | * @package Pop\Mail |
28 | * @author Nick Sagona, III <dev@nolainteractive.com> |
29 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
30 | * @license http://www.popphp.org/license New BSD License |
31 | * @version 4.0.0 |
32 | */ |
33 | class Office365 extends AbstractOffice365 implements TransportInterface |
34 | { |
35 | |
36 | /** |
37 | * Send the message |
38 | * |
39 | * @param Message $message |
40 | * @return mixed |
41 | */ |
42 | public function send(Message $message): mixed |
43 | { |
44 | $headers = $message->getHeaders(); |
45 | $parts = $message->getParts(); |
46 | $personalHeaders = ['From', 'Reply-To', 'Subject', 'To', 'CC', 'BCC', 'MIME-Version']; |
47 | $fields = [ |
48 | 'message' => [ |
49 | 'subject' => $message->getSubject(), |
50 | 'body' => [] |
51 | ], |
52 | ]; |
53 | |
54 | $toAddresses = $message->getTo(); |
55 | $ccAddresses = $message->getCc(); |
56 | $bccAddresses = $message->getBcc(); |
57 | $replyToAddress = $message->getReplyTo(); |
58 | $senderAddress = $message->getSender(); |
59 | |
60 | if (!empty($toAddresses)) { |
61 | $fields['message']['toRecipients'] = []; |
62 | foreach ($toAddresses as $email => $name) { |
63 | if (!empty($name)) { |
64 | $fields['message']['toRecipients'][] = [ |
65 | 'emailAddress' => [ |
66 | 'address' => $email, |
67 | 'name' => $name |
68 | ] |
69 | ]; |
70 | } else { |
71 | $fields['message']['toRecipients'][] = [ |
72 | 'emailAddress' => [ |
73 | 'address' => $email |
74 | ] |
75 | ]; |
76 | } |
77 | } |
78 | } |
79 | |
80 | if (!empty($ccAddresses)) { |
81 | $fields['message']['ccRecipients'] = []; |
82 | foreach ($ccAddresses as $email => $name) { |
83 | if (!empty($name)) { |
84 | $fields['message']['ccRecipients'][] = [ |
85 | 'emailAddress' => [ |
86 | 'address' => $email, |
87 | 'name' => $name |
88 | ] |
89 | ]; |
90 | } else { |
91 | $fields['message']['ccRecipients'][] = [ |
92 | 'emailAddress' => [ |
93 | 'address' => $email |
94 | ] |
95 | ]; |
96 | } |
97 | } |
98 | } |
99 | |
100 | if (!empty($bccAddresses)) { |
101 | $fields['message']['bccRecipients'] = []; |
102 | foreach ($bccAddresses as $email => $name) { |
103 | if (!empty($name)) { |
104 | $fields['message']['bccRecipients'][] = [ |
105 | 'emailAddress' => [ |
106 | 'address' => $email, |
107 | 'name' => $name |
108 | ] |
109 | ]; |
110 | } else { |
111 | $fields['message']['bccRecipients'][] = [ |
112 | 'emailAddress' => [ |
113 | 'address' => $email |
114 | ] |
115 | ]; |
116 | } |
117 | } |
118 | } |
119 | |
120 | if (!empty($replyToAddress)) { |
121 | $fields['message']['replyTo'] = ['emailAddress' => ['address' => array_key_first($replyToAddress)]]; |
122 | } |
123 | if (!empty($senderAddress)) { |
124 | $fields['message']['sender'] = ['emailAddress' => ['address' => array_key_first($senderAddress)]]; |
125 | } |
126 | |
127 | if (!empty($headers)) { |
128 | foreach ($headers as $header => $value) { |
129 | if (!in_array($header, $personalHeaders)) { |
130 | if (!isset($fields['message']['internetMessageHeaders'])) { |
131 | $fields['message']['internetMessageHeaders'] = []; |
132 | } |
133 | $fields['message']['internetMessageHeaders'][$header] = $value; |
134 | } |
135 | } |
136 | } |
137 | |
138 | foreach ($parts as $part) { |
139 | if ($part instanceof Text) { |
140 | $fields['message']['body'] = [ |
141 | 'contentType' => 'text', |
142 | 'content' => $part->getBody() |
143 | ]; |
144 | } else if ($part instanceof Html) { |
145 | $fields['message']['body'] = [ |
146 | 'contentType' => 'html', |
147 | 'content' => $part->getBody() |
148 | ]; |
149 | } else if ($part instanceof Attachment) { |
150 | if (!isset($fields['message']['attachments'])) { |
151 | $fields['message']['attachments'] = []; |
152 | } |
153 | $contentType = $part->getContentType(); |
154 | if (str_contains($contentType, ';')) { |
155 | $contentType = trim(substr($contentType, 0, strpos($contentType, ';'))); |
156 | } |
157 | $fields['message']['attachments'][] = [ |
158 | '@odata.type' => "#microsoft.graph.fileAttachment", |
159 | 'contentBytes' => base64_encode(file_get_contents($part->getFilename())), |
160 | 'contentType' => $contentType, |
161 | 'name' => $part->getBasename(), |
162 | ]; |
163 | } |
164 | } |
165 | |
166 | $this->verifyToken(); |
167 | |
168 | $this->client->setAuth(Http\Auth::createBearer($this->token)); |
169 | $this->client->addOption('method', 'POST'); |
170 | $this->client->addOption('type', Http\Client\Request::JSON); |
171 | $this->client->addOption('auto', true); |
172 | |
173 | $this->client->setData($fields); |
174 | return $this->client->send('/' . $this->accountId . '/sendmail'); |
175 | } |
176 | |
177 | } |