1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\SparkPost; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Symfony\Component\Mime\Email; |
8
|
|
|
use SilverStripe\Control\Director; |
9
|
|
|
use Symfony\Component\Mailer\Envelope; |
10
|
|
|
use SilverStripe\Assets\FileNameFilter; |
11
|
|
|
use Symfony\Component\Mailer\SentMessage; |
12
|
|
|
use LeKoala\SparkPost\Api\SparkPostApiClient; |
13
|
|
|
use Symfony\Component\Mime\Header\HeaderInterface; |
14
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
15
|
|
|
use Symfony\Component\Mime\Header\UnstructuredHeader; |
16
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
17
|
|
|
use Symfony\Component\Mime\Header\ParameterizedHeader; |
18
|
|
|
use Symfony\Component\HttpClient\Response\MockResponse; |
19
|
|
|
use Symfony\Component\Mailer\Transport\AbstractApiTransport; |
20
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
use Symfony\Component\Mailer\Event\MessageEvent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* We create our own class |
25
|
|
|
* We cannot extend easily due to private methods |
26
|
|
|
* |
27
|
|
|
* @link https://developers.sparkpost.com/api/transmissions/ |
28
|
|
|
* @link https://github.com/gam6itko/sparkpost-mailer/blob/master/src/Transport/SparkPostApiTransport.php |
29
|
|
|
* @author LeKoala <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class SparkPostApiTransport extends AbstractApiTransport |
32
|
|
|
{ |
33
|
|
|
private const HOST = 'api.sparkpost.com'; |
34
|
|
|
private const EU_HOST = 'api.eu.sparkpost.com'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SparkPostApiClient |
38
|
|
|
*/ |
39
|
|
|
private $apiClient; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array<mixed> |
43
|
|
|
*/ |
44
|
|
|
private $apiResult; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var EventDispatcherInterface |
48
|
|
|
*/ |
49
|
|
|
private $dispatcher = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param SparkPostApiClient $apiClient |
53
|
|
|
* @param HttpClientInterface|null $client |
54
|
|
|
* @param EventDispatcherInterface|null $dispatcher |
55
|
|
|
* @param LoggerInterface|null $logger |
56
|
|
|
*/ |
57
|
|
|
public function __construct(SparkPostApiClient $apiClient, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null) |
58
|
|
|
{ |
59
|
|
|
$this->apiClient = $apiClient; |
60
|
|
|
|
61
|
|
|
if ($apiClient->getEuEndpoint()) { |
62
|
|
|
$this->setHost(self::EU_HOST); |
63
|
|
|
} else { |
64
|
|
|
$this->setHost(self::HOST); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// We need our own reference |
68
|
|
|
$this->dispatcher = $dispatcher; |
69
|
|
|
parent::__construct($client, $dispatcher, $logger); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function __toString(): string |
73
|
|
|
{ |
74
|
|
|
return sprintf('sparkpost+api://%s', $this->getEndpoint()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function dispatchEvent(Email $email, Envelope $envelope = null): void |
78
|
|
|
{ |
79
|
|
|
if (!$this->dispatcher) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
$sender = $email->getSender()[0] ?? $email->getFrom()[0] ?? null; |
83
|
|
|
$recipients = $email->getTo(); |
84
|
|
|
$envelope ??= new Envelope($sender, $recipients); |
|
|
|
|
85
|
|
|
$event = new MessageEvent($email, $envelope, $this); |
86
|
|
|
$this->dispatcher->dispatch($event); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface |
90
|
|
|
{ |
91
|
|
|
$this->dispatchEvent($email, $envelope); |
92
|
|
|
|
93
|
|
|
$disableSending = $email->getHeaders()->has('X-SendingDisabled') || !SparkPostHelper::getSendingEnabled(); |
94
|
|
|
|
95
|
|
|
// We don't really care about the actual response |
96
|
|
|
$response = new MockResponse(); |
97
|
|
|
|
98
|
|
|
$to = $email->getTo(); |
99
|
|
|
|
100
|
|
|
// check .local addresses |
101
|
|
|
foreach ($to as $addr) { |
102
|
|
|
if (str_ends_with($addr->getAddress(), '.local')) { |
103
|
|
|
$disableSending = true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($disableSending) { |
108
|
|
|
$result = [ |
109
|
|
|
'total_rejected_recipients' => 0, |
110
|
|
|
'total_accepted_recipients' => count($to), |
111
|
|
|
'id' => 'fake-' . uniqid(), |
112
|
|
|
'disabled' => true, |
113
|
|
|
]; |
114
|
|
|
} else { |
115
|
|
|
$payload = $this->getPayload($email, $envelope); |
116
|
|
|
$result = $this->apiClient->createTransmission($payload); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Add email |
120
|
|
|
$result['email'] = implode('; ', array_map(function ($recipient) { |
121
|
|
|
return $recipient->toString(); |
122
|
|
|
}, $to)); |
123
|
|
|
|
124
|
|
|
$this->apiResult = $result; |
125
|
|
|
|
126
|
|
|
$messageId = $result['id'] ?? null; |
127
|
|
|
if ($messageId) { |
128
|
|
|
$sentMessage->setMessageId($messageId); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (SparkPostHelper::getLoggingEnabled()) { |
132
|
|
|
$this->logMessageContent($email, $result); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $response; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array<mixed> |
140
|
|
|
*/ |
141
|
|
|
public function getApiResult(): array |
142
|
|
|
{ |
143
|
|
|
return $this->apiResult; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
private function getEndpoint(): string |
150
|
|
|
{ |
151
|
|
|
return ($this->host ?: self::HOST) . ($this->port ? ':' . $this->port : ''); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param Email $email |
156
|
|
|
* @return array<array<mixed>> |
157
|
|
|
*/ |
158
|
|
|
private function buildAttachments(Email $email): array |
159
|
|
|
{ |
160
|
|
|
$result = []; |
161
|
|
|
foreach ($email->getAttachments() as $attachment) { |
162
|
|
|
$preparedHeaders = $attachment->getPreparedHeaders(); |
163
|
|
|
/** @var ParameterizedHeader $file */ |
164
|
|
|
$file = $preparedHeaders->get('Content-Disposition'); |
165
|
|
|
/** @var ParameterizedHeader $type */ |
166
|
|
|
$type = $preparedHeaders->get('Content-Type'); |
167
|
|
|
|
168
|
|
|
$result[] = [ |
169
|
|
|
'name' => $file->getParameter('filename'), |
170
|
|
|
'type' => $type->getValue(), |
171
|
|
|
'data' => base64_encode($attachment->getBody()), |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $result; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param Email $email |
180
|
|
|
* @param Envelope $envelope |
181
|
|
|
* @return array<string,mixed> |
182
|
|
|
*/ |
183
|
|
|
public function getPayload(Email $email, Envelope $envelope): array |
184
|
|
|
{ |
185
|
|
|
$from = $envelope->getSender(); |
186
|
|
|
|
187
|
|
|
$fromFirstEmail = $from->getAddress(); |
188
|
|
|
$fromFirstName = $from->getName(); |
189
|
|
|
if (SparkPostHelper::config()->override_admin_email && SparkPostHelper::isAdminEmail($fromFirstEmail)) { |
190
|
|
|
$fromFirstEmail = SparkPostHelper::resolveDefaultFromEmail(); |
191
|
|
|
} |
192
|
|
|
if (SparkPostHelper::getEnvForceSender()) { |
193
|
|
|
$fromFirstEmail = SparkPostHelper::getEnvForceSender(); |
194
|
|
|
} |
195
|
|
|
if (!$fromFirstName) { |
196
|
|
|
$fromFirstName = EmailUtils::get_displayname_from_rfc_email($fromFirstEmail); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$toAddresses = []; |
200
|
|
|
$ccAddresses = []; |
201
|
|
|
$bccAddresses = []; |
202
|
|
|
|
203
|
|
|
foreach ($envelope->getRecipients() as $recipient) { |
204
|
|
|
$type = 'to'; |
205
|
|
|
if (\in_array($recipient, $email->getBcc(), true)) { |
206
|
|
|
$type = 'bcc'; |
207
|
|
|
} elseif (\in_array($recipient, $email->getCc(), true)) { |
208
|
|
|
$type = 'cc'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$recipientEmail = $recipient->getAddress(); |
212
|
|
|
|
213
|
|
|
// This is always going to be empty because of Envelope:77 |
214
|
|
|
// $this->recipients[] = new Address($recipient->getAddress()); |
215
|
|
|
$recipientName = $recipient->getName(); |
216
|
|
|
if (!$recipientName) { |
217
|
|
|
$recipientName = EmailUtils::get_displayname_from_rfc_email($recipientEmail); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
switch ($type) { |
221
|
|
|
case 'to': |
222
|
|
|
$toAddresses[$recipientEmail] = $recipientName; |
223
|
|
|
break; |
224
|
|
|
case 'cc': |
225
|
|
|
$ccAddresses[$recipientEmail] = $recipientName; |
226
|
|
|
break; |
227
|
|
|
case 'bcc': |
228
|
|
|
$bccAddresses[$recipientEmail] = $recipientName; |
229
|
|
|
break; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$recipients = []; |
234
|
|
|
$cc = []; |
235
|
|
|
$bcc = []; |
236
|
|
|
$headers = []; |
237
|
|
|
$tags = []; |
238
|
|
|
$metadata = []; |
239
|
|
|
$inlineCss = null; |
240
|
|
|
|
241
|
|
|
// Mandrill compatibility |
242
|
|
|
// Data is merged with transmission and removed from headers |
243
|
|
|
// @link https://mailchimp.com/developer/transactional/docs/tags-metadata/#tags |
244
|
|
|
$emailHeaders = $email->getHeaders(); |
245
|
|
|
if ($emailHeaders->has('X-MC-Tags')) { |
246
|
|
|
$tagsHeader = $emailHeaders->get('X-MC-Tags'); |
247
|
|
|
$tags = explode(',', self::getHeaderValue($tagsHeader)); |
248
|
|
|
$emailHeaders->remove('X-MC-Tags'); |
249
|
|
|
} |
250
|
|
|
if ($emailHeaders->has('X-MC-Metadata')) { |
251
|
|
|
$metadataHeader = $emailHeaders->get('X-MC-Metadata'); |
252
|
|
|
$metadata = json_decode(self::getHeaderValue($metadataHeader), true); |
253
|
|
|
$emailHeaders->remove('X-MC-Metadata'); |
254
|
|
|
} |
255
|
|
|
if ($emailHeaders->has('X-MC-InlineCSS')) { |
256
|
|
|
$inlineHeader = $emailHeaders->get('X-MC-InlineCSS'); |
257
|
|
|
$inlineCss = self::getHeaderValue($inlineHeader); |
258
|
|
|
$emailHeaders->remove('X-MC-InlineCSS'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// Handle MSYS headers |
262
|
|
|
// Data is merge with transmission and removed from headers |
263
|
|
|
// @link https://developers.sparkpost.com/api/smtp-api.html |
264
|
|
|
$msysHeader = []; |
265
|
|
|
if ($emailHeaders->has('X-MSYS-API')) { |
266
|
|
|
$msysHeaderObj = $emailHeaders->get('X-MSYS-API'); |
267
|
|
|
$msysHeader = json_decode(self::getHeaderValue($msysHeaderObj), true); |
268
|
|
|
if (!empty($msysHeader['tags'])) { |
269
|
|
|
$tags = array_merge($tags, $msysHeader['tags']); |
270
|
|
|
} |
271
|
|
|
if (!empty($msysHeader['metadata'])) { |
272
|
|
|
$metadata = array_merge($metadata, $msysHeader['metadata']); |
273
|
|
|
} |
274
|
|
|
$emailHeaders->remove('X-MSYS-API'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// Build recipients list |
278
|
|
|
// @link https://developers.sparkpost.com/api/recipient-lists.html |
279
|
|
|
$primaryEmail = null; |
280
|
|
|
foreach ($toAddresses as $toEmail => $toName) { |
281
|
|
|
if ($primaryEmail === null) { |
282
|
|
|
$primaryEmail = $toEmail; |
283
|
|
|
} |
284
|
|
|
if (!$toName) { |
285
|
|
|
$toName = $toEmail; |
286
|
|
|
} |
287
|
|
|
$recipient = array( |
288
|
|
|
'address' => array( |
289
|
|
|
'email' => $toEmail, |
290
|
|
|
'name' => $toName, |
291
|
|
|
) |
292
|
|
|
); |
293
|
|
|
if (!empty($tags)) { |
294
|
|
|
$recipient['tags'] = $tags; |
295
|
|
|
} |
296
|
|
|
// TODO: metadata are not valid? |
297
|
|
|
if (!empty($metadata)) { |
298
|
|
|
$recipient['metadata'] = $metadata; |
299
|
|
|
} |
300
|
|
|
$recipients[] = $recipient; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
// @link https://www.sparkpost.com/docs/faq/cc-bcc-with-rest-api/ |
304
|
|
|
foreach ($ccAddresses as $ccEmail => $ccName) { |
305
|
|
|
$cc[] = array( |
306
|
|
|
'email' => $ccEmail, |
307
|
|
|
'name' => $ccName, |
308
|
|
|
'header_to' => $primaryEmail ? $primaryEmail : $ccEmail, |
309
|
|
|
); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
foreach ($bccAddresses as $bccEmail => $bccName) { |
313
|
|
|
$bcc[] = array( |
314
|
|
|
'email' => $bccEmail, |
315
|
|
|
'name' => $bccName, |
316
|
|
|
'header_to' => $primaryEmail ? $primaryEmail : $bccEmail, |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$bodyHtml = (string)$email->getHtmlBody(); |
321
|
|
|
$bodyText = (string)$email->getTextBody(); |
322
|
|
|
|
323
|
|
|
if ($bodyHtml) { |
324
|
|
|
// If we ask to provide plain, use our custom method instead of the provided one |
325
|
|
|
if (SparkPostHelper::config()->provide_plain) { |
326
|
|
|
$bodyText = EmailUtils::convert_html_to_text($bodyHtml); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Should we inline css |
330
|
|
|
if (!$inlineCss && SparkPostHelper::config()->inline_styles) { |
331
|
|
|
$bodyHtml = EmailUtils::inline_styles($bodyHtml); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// Custom unsubscribe list |
336
|
|
|
if ($emailHeaders->has('List-Unsubscribe')) { |
337
|
|
|
$unsubHeader = $emailHeaders->get('List-Unsubscribe'); |
338
|
|
|
$headers['List-Unsubscribe'] = self::getHeaderValue($unsubHeader); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$defaultParams = SparkPostHelper::config()->default_params; |
342
|
|
|
if ($inlineCss !== null) { |
343
|
|
|
$defaultParams['inline_css'] = $inlineCss; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
// Build base transmission. Keep in mind that parameters are mapped by the sdk |
347
|
|
|
// @link @link https://developers.sparkpost.com/api/transmissions/#transmissions-post-send-inline-content |
348
|
|
|
$sparkPostMessage = [ |
349
|
|
|
'recipients' => $recipients, |
350
|
|
|
'content' => [ |
351
|
|
|
'from' => [ |
352
|
|
|
'name' => $fromFirstName, |
353
|
|
|
'email' => $fromFirstEmail, |
354
|
|
|
], |
355
|
|
|
'subject' => $email->getSubject(), |
356
|
|
|
'html' => $bodyHtml, |
357
|
|
|
'text' => $bodyText, |
358
|
|
|
], |
359
|
|
|
]; |
360
|
|
|
if ($email->getReplyTo()) { |
361
|
|
|
$sparkPostMessage['reply_to'] = $email->getReplyTo(); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// Add default params |
365
|
|
|
$sparkPostMessage = array_merge($defaultParams, $sparkPostMessage); |
366
|
|
|
if ($msysHeader) { |
367
|
|
|
$sparkPostMessage = array_merge($sparkPostMessage, $msysHeader); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
// Add remaining elements |
371
|
|
|
if (!empty($cc)) { |
372
|
|
|
$sparkPostMessage['headers.CC'] = $cc; |
373
|
|
|
} |
374
|
|
|
if (!empty($headers)) { |
375
|
|
|
$sparkPostMessage['customHeaders'] = $headers; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
$attachments = $this->buildAttachments($email); |
379
|
|
|
if (count($attachments) > 0) { |
380
|
|
|
$sparkPostMessage['attachments'] = $attachments; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return $sparkPostMessage; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @param HeaderInterface|UnstructuredHeader|null $header |
388
|
|
|
* @return string |
389
|
|
|
*/ |
390
|
|
|
protected static function getHeaderValue(HeaderInterface $header = null) |
391
|
|
|
{ |
392
|
|
|
if (!$header) { |
393
|
|
|
return ''; |
394
|
|
|
} |
395
|
|
|
if ($header instanceof UnstructuredHeader) { |
396
|
|
|
return $header->getValue(); |
397
|
|
|
} |
398
|
|
|
return $header->getBody(); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Log message content |
404
|
|
|
* |
405
|
|
|
* @param Email $message |
406
|
|
|
* @param array<mixed> $results Results from the api |
407
|
|
|
* @throws Exception |
408
|
|
|
* @return void |
409
|
|
|
*/ |
410
|
|
|
protected function logMessageContent(Email $message, $results = []) |
411
|
|
|
{ |
412
|
|
|
// Folder not set |
413
|
|
|
$logFolder = SparkPostHelper::getLogFolder(); |
414
|
|
|
if (!$logFolder) { |
415
|
|
|
return; |
416
|
|
|
} |
417
|
|
|
// Logging disabled |
418
|
|
|
if (!SparkPostHelper::getLoggingEnabled()) { |
419
|
|
|
return; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
$logContent = ""; |
423
|
|
|
|
424
|
|
|
$subject = $message->getSubject(); |
425
|
|
|
if ($message->getHtmlBody()) { |
426
|
|
|
$logContent .= (string)$message->getHtmlBody(); |
427
|
|
|
} else { |
428
|
|
|
$logContent .= $message->getBody()->toString(); |
429
|
|
|
} |
430
|
|
|
$logContent .= "<hr/>"; |
431
|
|
|
$emailHeaders = $message->getHeaders(); |
432
|
|
|
|
433
|
|
|
// Append some extra information at the end |
434
|
|
|
$logContent .= '<pre>Headers:' . "\n\n"; |
435
|
|
|
$logContent .= $emailHeaders->toString(); |
436
|
|
|
|
437
|
|
|
// Store subsite |
438
|
|
|
if (class_exists(\SilverStripe\Subsites\Model\Subsite::class)) { |
|
|
|
|
439
|
|
|
$state = \SilverStripe\Subsites\State\SubsiteState::singleton(); |
|
|
|
|
440
|
|
|
$logContent .= "Subsite ID: " . $state->getSubsiteId() . "\n"; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
$logContent .= "\n" . 'Results:' . "\n"; |
444
|
|
|
$logContent .= print_r($results, true) . "\n"; |
|
|
|
|
445
|
|
|
$logContent .= '</pre>'; |
446
|
|
|
|
447
|
|
|
// Generate filename |
448
|
|
|
$filter = new FileNameFilter(); |
449
|
|
|
$title = substr($filter->filter($subject), 0, 35); |
450
|
|
|
$logName = date('Ymd_His') . '_' . bin2hex(random_bytes(2)) . '_' . $title; |
451
|
|
|
|
452
|
|
|
// Store attachments if any |
453
|
|
|
$attachments = $message->getAttachments(); |
454
|
|
|
if (!empty($attachments)) { |
455
|
|
|
$logContent .= '<hr />'; |
456
|
|
|
foreach ($attachments as $attachment) { |
457
|
|
|
$attachmentDestination = $logFolder . '/' . $logName . '_' . $attachment->getFilename(); |
458
|
|
|
file_put_contents($attachmentDestination, $attachment->getBody()); |
459
|
|
|
$logContent .= 'File : <a href="' . $attachmentDestination . '">' . $attachment->getFilename() . '</a><br/>'; |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
// Store it |
464
|
|
|
$r = file_put_contents($logFolder . '/' . $logName . '.html', $logContent); |
465
|
|
|
|
466
|
|
|
if (!$r && Director::isDev()) { |
467
|
|
|
throw new Exception('Failed to store email in ' . $logFolder); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|