forked from ts-navghane/sparkpost-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSparkpostTransport.php
442 lines (380 loc) · 15 KB
/
SparkpostTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
declare(strict_types=1);
namespace MauticPlugin\SparkpostBundle\Mailer\Transport;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\EmailBundle\Helper\MailHelper;
use Mautic\EmailBundle\Mailer\Message\MauticMessage;
use Mautic\EmailBundle\Mailer\Transport\TokenTransportInterface;
use Mautic\EmailBundle\Mailer\Transport\TokenTransportTrait;
use Mautic\EmailBundle\Model\TransportCallback;
use Mautic\LeadBundle\Entity\DoNotContact;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\ParameterizedHeader;
use Symfony\Component\Mime\Header\UnstructuredHeader;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class SparkpostTransport extends AbstractApiTransport implements TokenTransportInterface
{
use TokenTransportTrait;
public const MAUTIC_SPARKPOST_API_SCHEME = 'mautic+sparkpost+api';
public const SPARK_POST_HOSTS = ['us' => 'api.sparkpost.com', 'eu' => 'api.eu.sparkpost.com'];
private const STD_HEADER_KEYS = [
'MIME-Version',
'received',
'dkim-signature',
'Content-Type',
'Content-Transfer-Encoding',
'To',
'From',
'Subject',
'Reply-To',
'CC',
'BCC',
];
public function __construct(
private string $apiKey,
string $region,
private TransportCallback $callback,
private CoreParametersHelper $coreParametersHelper,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null,
LoggerInterface $logger = null,
) {
parent::__construct($client, $dispatcher, $logger);
$this->host = self::SPARK_POST_HOSTS[$region] ?? self::SPARK_POST_HOSTS['us'];
}
public function __toString(): string
{
return sprintf(self::MAUTIC_SPARKPOST_API_SCHEME.'://%s', $this->host);
}
/**
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
{
try {
$payload = $this->getSparkpostPayload($sentMessage);
$this->checkTemplateIsValid($payload);
$response = $this->getSparkpostResponse('transmissions', $payload);
$this->handleError($response);
if ($errorMessage = $this->getErrorMessageFromResponseBody($response->toArray())) {
/** @var MauticMessage $message */
$message = $sentMessage->getOriginalMessage();
$this->processImmediateSendFeedback($payload, $response->toArray(), $message->getMetadata());
throw new TransportException($errorMessage);
}
return $response;
} catch (\Exception $e) {
throw new TransportException($e->getMessage());
}
}
/**
* @return array<mixed>
*/
private function getSparkpostPayload(SentMessage $message): array
{
$email = $message->getOriginalMessage();
if (!$email instanceof MauticMessage) {
throw new TransportException('Message must be an instance of '.MauticMessage::class);
}
$metadata = $email->getMetadata();
$mergeVars = [];
$metadataSet = [];
// Sparkpost uses {{ name }} for tokens so Mautic's need to be converted;
// although using their {{{ }}} syntax to prevent HTML escaping
if (!empty($metadata)) {
$metadataSet = reset($metadata);
$tokens = (!empty($metadataSet['tokens'])) ? $metadataSet['tokens'] : [];
$mauticTokens = array_keys($tokens);
$mergeVarPlaceholders = [];
foreach ($mauticTokens as $token) {
$mergeVars[$token] = strtoupper(preg_replace('/[^a-z0-9]+/i', '', $token));
$mergeVarPlaceholders[$token] = '{{{ '.$mergeVars[$token].' }}}';
}
if (!empty($mauticTokens)) {
MailHelper::searchReplaceTokens($mauticTokens, $mergeVarPlaceholders, $email);
}
}
$trackingEnabled = (bool) $this->coreParametersHelper->get('sparkpost_tracking_enabled', false);
return [
'content' => $this->buildContent($email),
'recipients' => $this->buildRecipients($email, $metadata, $mergeVars),
'inline_css' => $email->getHeaders()->get('X-MC-InlineCSS')
? $email->getHeaders()->get('X-MC-InlineCSS')->getBody()
: null,
'tags' => $email->getHeaders()->get('X-MC-Tags')
? $email->getHeaders()->get('X-MC-Tags')->getBody()
: [],
'campaign_id' => $this->getCampaignId($metadata, $metadataSet),
'options' => [
'open_tracking' => $trackingEnabled,
'click_tracking' => $trackingEnabled,
],
];
}
/**
* @return array<mixed>
*/
private function buildContent(MauticMessage $message): array
{
$fromAddress = current($message->getFrom());
$replyTo = current($message->getReplyTo()) ?: $fromAddress;
return [
'from' => !empty($fromAddress->getName())
? $fromAddress->getName().' <'.$fromAddress->getAddress().'>'
: $fromAddress->getAddress(),
'subject' => $message->getSubject(),
'headers' => $this->buildHeaders($message) ?: new \stdClass(),
'html' => $message->getHtmlBody(),
'text' => $message->getTextBody(),
'reply_to' => $replyTo->getAddress(),
'attachments' => $this->buildAttachments($message),
];
}
/**
* @return array<mixed>
*/
private function buildHeaders(MauticMessage $message): array
{
$result = [];
$headers = $message->getHeaders()->all();
foreach ($headers as $header) {
if ($header instanceof UnstructuredHeader && !in_array($header->getName(), self::STD_HEADER_KEYS)) {
$result[$header->getName()] = $header->getBody();
}
}
return $result;
}
/**
* @return array<mixed>
*/
private function buildAttachments(MauticMessage $message): array
{
$result = [];
foreach ($message->getAttachments() as $attachment) {
/** @var ParameterizedHeader $file */
$file = $attachment->getPreparedHeaders()->get('Content-Disposition');
/** @var ParameterizedHeader $type */
$type = $attachment->getPreparedHeaders()->get('Content-Type');
$result[] = [
'name' => $file->getParameter('filename'),
'type' => $type->getValue(),
'data' => base64_encode($attachment->getBody()),
];
}
return $result;
}
/**
* @param array<mixed> $metadata
* @param array<mixed> $mergeVars
*
* @return array<mixed>
*/
private function buildRecipients(MauticMessage $message, array $metadata, array $mergeVars): array
{
$recipients = [];
foreach ($message->getTo() as $to) {
$recipient = $this->buildRecipient($to, $metadata, $mergeVars);
$recipients[] = $recipient;
// CC and BCC fields need to be included as a normal TO address with token duplication
// https://www.sparkpost.com/docs/faq/cc-bcc-with-rest-api/ - token duplication is not mentioned here
// See test for CC and BCC too
foreach ($message->getCc() as $cc) {
$recipients[] = $this->buildCopyRecipient($to, $cc, $recipient);
}
foreach ($message->getBcc() as $bcc) {
$recipients[] = $this->buildCopyRecipient($to, $bcc, $recipient);
}
}
return $recipients;
}
/**
* @param array<mixed> $metadata
* @param array<mixed> $mergeVars
*
* @return array<mixed>
*/
private function buildRecipient(Address $to, array $metadata, array $mergeVars): array
{
$recipient = [
'address' => [
'email' => $to->getAddress(),
'name' => $to->getName(),
],
'substitution_data' => [],
'metadata' => [],
];
$email = $to->getAddress();
if (isset($metadata[$email]['tokens'])) {
foreach ($metadata[$email]['tokens'] as $token => $value) {
$recipient['substitution_data'][$mergeVars[$token]] = $value;
}
unset($metadata[$email]['tokens']);
$recipient['metadata'] = $metadata[$email];
}
// Sparkpost requires substitution_data which can be by-passed by using
// MailHelper::setTo() rather than a Lead via MailHelper::setLead()
// Without it, Sparkpost returns the error: "field 'substitution_data' is required"
// But, it can't be an empty array or Sparkpost will return error: field 'substitution_data'
// is of type 'json', but needs to be of type 'json_object'
if (empty($recipient['substitution_data'])) {
$recipient['substitution_data'] = new \stdClass();
}
// Sparkpost doesn't like empty metadata
if (empty($recipient['metadata'])) {
unset($recipient['metadata']);
}
return $recipient;
}
/**
* @param array<mixed> $recipient
*
* @return array<mixed>
*/
private function buildCopyRecipient(Address $to, Address $copy, array $recipient): array
{
$copyRecipient = [
'address' => ['email' => $copy->getAddress()],
'header_to' => $to->getAddress(),
];
if (!empty($recipient['substitution_data'])) {
$copyRecipient['substitution_data'] = $recipient['substitution_data'];
}
return $copyRecipient;
}
/**
* @param array<mixed> $metadata
* @param array<mixed> $metadataSet
*/
private function getCampaignId(array $metadata, array $metadataSet): string
{
$campaignId = '';
if (!empty($metadata)) {
$id = '';
if (!empty($metadataSet['utmTags']['utmCampaign'])) {
$id = $metadataSet['utmTags']['utmCampaign'];
} elseif (!empty($metadataSet['emailId']) && !empty($metadataSet['emailName'])) {
$id = $metadataSet['emailId'].':'.$metadataSet['emailName'];
} elseif (!empty($metadataSet['emailId'])) {
$id = $metadataSet['emailId'];
}
$campaignId = mb_strcut($id, 0, 64);
}
return $campaignId;
}
/**
* @param array<mixed> $payload
*
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
private function checkTemplateIsValid(array $payload): void
{
// Take substitution_data from the first recipient.
if (
empty($payload['substitution_data'])
&& isset($payload['recipients'][0]['substitution_data'])
) {
$payload['substitution_data'] = $payload['recipients'][0]['substitution_data'];
unset($payload['recipients']);
}
$response = $this->getSparkpostResponse('utils/content-previewer', $payload);
if (403 === $response->getStatusCode()) {
// We cannot fail as it would be a BC break. Throw a warning and continue.
$this->getLogger()->warning(
'The permission "Templates: Preview" is not enabled. '.
'Enable it to let Mautic check email template validity before send.'
);
}
$this->handleError($response);
}
/**
* @param array<mixed> $payload
*
* @throws TransportExceptionInterface
*/
private function getSparkpostResponse(
string $endpoint,
array $payload,
string $method = Request::METHOD_POST,
): ResponseInterface {
return $this->client->request(
$method,
sprintf('https://%1$s/api/v1/%2$s/', $this->host, $endpoint),
[
'headers' => [
'Authorization' => $this->apiKey,
'Content-Type' => 'application/json',
],
'json' => $payload,
]
);
}
/**
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
private function handleError(ResponseInterface $response): void
{
if (200 === $response->getStatusCode()) {
return;
}
$data = json_decode($response->getContent(false), true);
$this->getLogger()->error('SparkpostApiTransport error response', $data);
throw new HttpTransportException(json_encode($data['errors']), $response, $response->getStatusCode());
}
/**
* @param array<mixed> $response
*/
private function getErrorMessageFromResponseBody(array $response): string
{
return $response['errors'][0]['description'] ?? $response['errors'][0]['message'] ?? '';
}
/**
* @param array<mixed> $message
* @param array<mixed> $response
* @param array<mixed> $metadata
*/
private function processImmediateSendFeedback(array $message, array $response, array $metadata): void
{
if (!empty($response['errors'][0]['code']) && 1902 === (int) $response['errors'][0]['code']) {
$comments = $this->getErrorMessageFromResponseBody($response);
$emailAddress = $message['recipients'][0]['address']['email'];
if (isset($metadata[$emailAddress]['leadId'])) {
$emailId = $metadata[$emailAddress]['emailId'] ?? null;
$this->callback->addFailureByContactId(
$metadata[$emailAddress]['leadId'],
$comments,
DoNotContact::BOUNCED,
$emailId
);
}
}
}
public function getMaxBatchLimit(): int
{
return 5000;
}
}