1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license https://github.com/f500/swiftmailer-sparkpost/blob/master/LICENSE MIT |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SwiftSparkPost; |
8
|
|
|
|
9
|
|
|
use Swift_Attachment; |
10
|
|
|
use Swift_Mime_Header; |
11
|
|
|
use Swift_Mime_Message; |
12
|
|
|
use Swift_MimePart; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @copyright Future500 B.V. |
16
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class PayloadBuilder implements PayloadBuilderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $overridePart1; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $overridePart2; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $recipientOverride |
32
|
|
|
* |
33
|
|
|
* @throws Exception |
34
|
|
|
*/ |
35
|
39 |
|
public function __construct($recipientOverride = '') |
36
|
|
|
{ |
37
|
39 |
|
$this->overridePart1 = ''; |
38
|
39 |
|
$this->overridePart2 = ''; |
39
|
|
|
|
40
|
39 |
|
if ($recipientOverride) { |
41
|
9 |
|
if (!filter_var($recipientOverride, FILTER_VALIDATE_EMAIL)) { |
42
|
3 |
|
throw new Exception('Recipient override must be a valid email address'); |
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
list($this->overridePart1, $this->overridePart2) = explode('@', $recipientOverride); |
46
|
2 |
|
} |
47
|
39 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Swift_Mime_Message $message |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
33 |
|
public function buildPayload(Swift_Mime_Message $message) |
55
|
|
|
{ |
56
|
|
|
$payload = [ |
57
|
33 |
|
'recipients' => $this->buildRecipients($message), |
58
|
30 |
|
'content' => $this->buildContent($message), |
59
|
10 |
|
]; |
60
|
|
|
|
61
|
30 |
|
if ($campaignId = $this->buildCampaignId($message)) { |
62
|
3 |
|
$payload['campaign_id'] = $campaignId; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
30 |
|
if ($metadata = $this->buildMetadata($message)) { |
66
|
3 |
|
$payload['metadata'] = $metadata; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
30 |
|
if ($substitutionData = $this->buildSubstitutionData($message)) { |
70
|
3 |
|
$payload['substitution_data'] = $substitutionData; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
30 |
|
if ($options = $this->buildOptions($message)) { |
74
|
9 |
|
$payload['options'] = $options; |
75
|
3 |
|
} |
76
|
|
|
|
77
|
30 |
|
return $payload; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Swift_Mime_Message $message |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
* @throws Exception |
85
|
|
|
*/ |
86
|
33 |
|
private function buildRecipients(Swift_Mime_Message $message) |
87
|
|
|
{ |
88
|
33 |
|
$tos = array_merge((array) $message->getTo(), (array) $message->getCc()); |
89
|
33 |
|
$bcc = (array) $message->getBcc(); |
90
|
|
|
|
91
|
33 |
|
if (count($tos) === 0) { |
92
|
3 |
|
throw new Exception('Cannot send message without a recipient address'); |
93
|
|
|
} |
94
|
|
|
|
95
|
30 |
|
$tags = []; |
96
|
30 |
|
$metadata = []; |
97
|
30 |
|
$substitutionData = []; |
98
|
|
|
|
99
|
30 |
|
if ($message instanceof Message) { |
100
|
9 |
|
$tags = $message->getPerRecipientTags(); |
101
|
9 |
|
$metadata = $message->getPerRecipientMetadata(); |
102
|
9 |
|
$substitutionData = $message->getPerRecipientSubstitutionData(); |
103
|
3 |
|
} |
104
|
|
|
|
105
|
30 |
|
$recipients = []; |
106
|
|
|
|
107
|
30 |
|
foreach ($tos as $email => $name) { |
108
|
30 |
|
$recipients[] = $this->buildRecipient($email, $name, $tags, $metadata, $substitutionData); |
109
|
10 |
|
} |
110
|
|
|
|
111
|
30 |
|
$originalEmail = current($recipients)['address']['email']; |
112
|
|
|
|
113
|
30 |
|
foreach ($bcc as $email => $name) { |
114
|
21 |
|
$recipients[] = $this->buildRecipient($email, $name, $tags, $metadata, $substitutionData, $originalEmail); |
115
|
10 |
|
} |
116
|
|
|
|
117
|
30 |
|
return $recipients; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $email |
122
|
|
|
* @param string $name |
123
|
|
|
* @param array $tags |
124
|
|
|
* @param array $metadata |
125
|
|
|
* @param array $substitutionData |
126
|
|
|
* @param string $originalEmail |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
30 |
|
private function buildRecipient( |
131
|
|
|
$email, |
132
|
|
|
$name, |
133
|
|
|
array $tags, |
134
|
|
|
array $metadata, |
135
|
|
|
array $substitutionData, |
136
|
|
|
$originalEmail = '' |
137
|
|
|
) { |
138
|
30 |
|
$recipient = ['address' => ['email' => $this->overrideRecipient($email)]]; |
139
|
|
|
|
140
|
30 |
|
if ($name) { |
141
|
15 |
|
$recipient['address']['name'] = $name; |
142
|
5 |
|
} |
143
|
|
|
|
144
|
30 |
|
if ($originalEmail) { |
145
|
21 |
|
$recipient['address']['header_to'] = $originalEmail; |
146
|
7 |
|
} |
147
|
|
|
|
148
|
30 |
|
if (isset($tags[$email])) { |
149
|
6 |
|
$recipient['tags'] = $tags[$email]; |
150
|
2 |
|
} |
151
|
|
|
|
152
|
30 |
|
if (isset($metadata[$email])) { |
153
|
6 |
|
$recipient['metadata'] = $metadata[$email]; |
154
|
2 |
|
} |
155
|
|
|
|
156
|
30 |
|
if (isset($substitutionData[$email])) { |
157
|
6 |
|
$recipient['substitution_data'] = $substitutionData[$email]; |
158
|
2 |
|
} |
159
|
|
|
|
160
|
30 |
|
return $recipient; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param Swift_Mime_Message $message |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
30 |
|
private function buildContent(Swift_Mime_Message $message) |
169
|
|
|
{ |
170
|
|
|
$content = [ |
171
|
30 |
|
'subject' => $this->convertAsteriskPipeToCurlyBraces($message->getSubject()), |
172
|
30 |
|
'from' => $this->buildFrom($message), |
173
|
10 |
|
]; |
174
|
|
|
|
175
|
30 |
|
if ($message->getReplyTo()) { |
176
|
21 |
|
$content['reply_to'] = key($message->getReplyTo()); |
177
|
7 |
|
} |
178
|
|
|
|
179
|
30 |
|
$contentMap = ['text/html' => 'html', 'text/plain' => 'text']; |
180
|
|
|
|
181
|
30 |
|
$contentType = $this->readUserContentType($message); |
182
|
30 |
|
if (isset($contentMap[$contentType])) { |
183
|
30 |
|
$content[$contentMap[$contentType]] = $this->convertAsteriskPipeToCurlyBraces($message->getBody()); |
184
|
10 |
|
} |
185
|
|
|
|
186
|
30 |
|
foreach ($message->getChildren() as $part) { |
187
|
6 |
|
if (!($part instanceof Swift_MimePart)) { |
188
|
3 |
|
continue; |
189
|
|
|
} |
190
|
|
|
|
191
|
6 |
|
$contentType = $part->getContentType(); |
192
|
6 |
|
if (isset($contentMap[$contentType])) { |
193
|
6 |
|
$content[$contentMap[$contentType]] = $this->convertAsteriskPipeToCurlyBraces($part->getBody()); |
194
|
2 |
|
} |
195
|
10 |
|
} |
196
|
|
|
|
197
|
30 |
|
if ($headers = $this->buildHeaders($message)) { |
198
|
3 |
|
$content['headers'] = $headers; |
199
|
1 |
|
} |
200
|
|
|
|
201
|
30 |
|
if ($attachments = $this->buildAttachments($message)) { |
202
|
3 |
|
$content['attachments'] = $attachments; |
203
|
1 |
|
} |
204
|
|
|
|
205
|
30 |
|
return $content; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param Swift_Mime_Message $message |
210
|
|
|
* |
211
|
|
|
* @return array|string |
212
|
|
|
*/ |
213
|
30 |
|
private function buildFrom(Swift_Mime_Message $message) |
214
|
|
|
{ |
215
|
30 |
|
$from = $message->getFrom(); |
216
|
|
|
|
217
|
30 |
|
if (current($from)) { |
218
|
15 |
|
return ['email' => key($from), 'name' => current($from)]; |
219
|
|
|
} |
220
|
|
|
|
221
|
15 |
|
return key($from); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param Swift_Mime_Message $message |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
30 |
|
private function buildHeaders(Swift_Mime_Message $message) |
230
|
|
|
{ |
231
|
30 |
|
$headers = []; |
232
|
|
|
$filter = [ |
233
|
30 |
|
'Bcc', |
234
|
10 |
|
'Cc', |
235
|
10 |
|
'Content-Transfer-Encoding', |
236
|
10 |
|
'Content-Type', |
237
|
10 |
|
'Date', |
238
|
10 |
|
'DKIM-Signature', |
239
|
10 |
|
'DomainKey-Signature', |
240
|
10 |
|
'From', |
241
|
10 |
|
'Message-ID', |
242
|
10 |
|
'MIME-Version', |
243
|
10 |
|
'Received', |
244
|
10 |
|
'Reply-To', |
245
|
10 |
|
'Return-Path', |
246
|
10 |
|
'Sender', |
247
|
10 |
|
'Subject', |
248
|
10 |
|
'To', |
249
|
10 |
|
]; |
250
|
|
|
|
251
|
|
|
/** @var Swift_Mime_Header $header */ |
252
|
30 |
|
foreach ($message->getHeaders()->getAll() as $header) { |
253
|
30 |
|
if (in_array($header->getFieldName(), $filter, true)) { |
254
|
30 |
|
continue; |
255
|
|
|
} |
256
|
|
|
|
257
|
3 |
|
$headers[] = trim($header->toString()); |
258
|
10 |
|
} |
259
|
|
|
|
260
|
30 |
|
return $headers; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param Swift_Mime_Message $message |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
30 |
|
private function buildAttachments(Swift_Mime_Message $message) |
269
|
|
|
{ |
270
|
30 |
|
$attachments = []; |
271
|
|
|
|
272
|
30 |
|
foreach ($message->getChildren() as $part) { |
273
|
6 |
|
if ($part instanceof Swift_Attachment) { |
274
|
3 |
|
$attachments[] = [ |
275
|
3 |
|
'type' => $part->getContentType(), |
276
|
3 |
|
'name' => $part->getFilename(), |
277
|
5 |
|
'data' => base64_encode($part->getBody()), |
278
|
|
|
]; |
279
|
1 |
|
} |
280
|
10 |
|
} |
281
|
|
|
|
282
|
30 |
|
return $attachments; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param Swift_Mime_Message $message |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
30 |
|
private function buildCampaignId(Swift_Mime_Message $message) |
291
|
|
|
{ |
292
|
30 |
|
if (!($message instanceof Message)) { |
293
|
21 |
|
return ''; |
294
|
|
|
} |
295
|
|
|
|
296
|
9 |
|
return $message->getCampaignId(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param Swift_Mime_Message $message |
301
|
|
|
* |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
30 |
|
private function buildMetadata(Swift_Mime_Message $message) |
305
|
|
|
{ |
306
|
30 |
|
if (!($message instanceof Message)) { |
307
|
21 |
|
return []; |
308
|
|
|
} |
309
|
|
|
|
310
|
9 |
|
return $message->getMetadata(); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param Swift_Mime_Message $message |
315
|
|
|
* |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
30 |
|
private function buildSubstitutionData(Swift_Mime_Message $message) |
319
|
|
|
{ |
320
|
30 |
|
if (!($message instanceof Message)) { |
321
|
21 |
|
return []; |
322
|
|
|
} |
323
|
|
|
|
324
|
9 |
|
return $message->getSubstitutionData(); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @param Swift_Mime_Message $message |
329
|
|
|
* |
330
|
|
|
* @return array |
331
|
|
|
*/ |
332
|
30 |
|
private function buildOptions(Swift_Mime_Message $message) |
333
|
|
|
{ |
334
|
30 |
|
if (!($message instanceof Message)) { |
335
|
21 |
|
return []; |
336
|
|
|
} |
337
|
|
|
|
338
|
9 |
|
return $message->getOptions(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Convert *|foo|* to {{foo}} |
343
|
|
|
* |
344
|
|
|
* @param string |
345
|
|
|
* |
346
|
|
|
* @return string |
347
|
|
|
*/ |
348
|
30 |
|
private function convertAsteriskPipeToCurlyBraces($content) |
349
|
|
|
{ |
350
|
30 |
|
return preg_replace('/\*\|(.+?)\|\*/', '{{\1}}', $content); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param Swift_Mime_Message $message |
355
|
|
|
* |
356
|
|
|
* @return string |
357
|
|
|
*/ |
358
|
30 |
|
private function readUserContentType(Swift_Mime_Message $message) |
359
|
|
|
{ |
360
|
30 |
|
$ro = new \ReflectionObject($message); |
361
|
30 |
|
$rp = $ro->getProperty('_userContentType'); |
362
|
30 |
|
$rp->setAccessible(true); |
363
|
30 |
|
return (string) $rp->getValue($message); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param string $email |
368
|
|
|
* |
369
|
|
|
* @return string |
370
|
|
|
*/ |
371
|
30 |
|
private function overrideRecipient($email) |
372
|
|
|
{ |
373
|
30 |
|
if (!$this->overridePart1 || !$this->overridePart2) { |
374
|
24 |
|
return $email; |
375
|
|
|
} |
376
|
|
|
|
377
|
6 |
|
return sprintf( |
378
|
6 |
|
'%s+%s@%s', |
379
|
6 |
|
$this->overridePart1, |
380
|
6 |
|
trim(preg_replace('/([^a-z0-9]+)/i', '-', $email), '-'), |
381
|
6 |
|
$this->overridePart2 |
382
|
2 |
|
); |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|