|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2013 Mailgun |
|
5
|
|
|
* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Mailgun\Message; |
|
11
|
|
|
|
|
12
|
|
|
use Mailgun\Message\Exceptions\LimitExceeded; |
|
13
|
|
|
use Mailgun\Message\Exceptions\TooManyRecipients; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This class is used for composing a properly formed |
|
17
|
|
|
* message object. Dealing with arrays can be cumbersome, |
|
18
|
|
|
* this class makes the process easier. See the official |
|
19
|
|
|
* documentation (link below) for usage instructions. |
|
20
|
|
|
* |
|
21
|
|
|
* @see https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Message/README.md |
|
22
|
|
|
*/ |
|
23
|
|
|
class MessageBuilder |
|
24
|
|
|
{ |
|
25
|
|
|
const RECIPIENT_COUNT_LIMIT = 1000; |
|
26
|
|
|
|
|
27
|
|
|
const CAMPAIGN_ID_LIMIT = 3; |
|
28
|
|
|
|
|
29
|
|
|
const TAG_LIMIT = 3; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $message = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $variables = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $counters = [ |
|
45
|
|
|
'recipients' => [ |
|
46
|
|
|
'to' => 0, |
|
47
|
|
|
'cc' => 0, |
|
48
|
|
|
'bcc' => 0, |
|
49
|
|
|
], |
|
50
|
|
|
'attributes' => [ |
|
51
|
|
|
'attachment' => 0, |
|
52
|
|
|
'campaign_id' => 0, |
|
53
|
|
|
'custom_option' => 0, |
|
54
|
|
|
'tag' => 0, |
|
55
|
|
|
], |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param array $params |
|
60
|
|
|
* @param string $key |
|
61
|
|
|
* @param mixed $default |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
22 |
|
private function get(array $params, string $key, $default) |
|
66
|
|
|
{ |
|
67
|
22 |
|
if (array_key_exists($key, $params)) { |
|
68
|
20 |
|
return $params[$key]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
return $default; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $params { |
|
76
|
|
|
* |
|
77
|
|
|
* @var string $full_name |
|
78
|
|
|
* @var string $first |
|
79
|
|
|
* @var string $last |
|
80
|
|
|
* } |
|
81
|
|
|
*/ |
|
82
|
22 |
|
private function getFullName(array $params): string |
|
83
|
|
|
{ |
|
84
|
22 |
|
if (isset($params['full_name'])) { |
|
85
|
|
|
return $this->get($params, 'full_name', ''); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
22 |
|
return trim(sprintf('%s %s', $this->get($params, 'first', ''), $this->get($params, 'last', ''))); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $address |
|
93
|
|
|
* @param array $params { |
|
|
|
|
|
|
94
|
|
|
* |
|
95
|
|
|
* @var string $full_name |
|
96
|
|
|
* @var string $first |
|
97
|
|
|
* @var string $last |
|
98
|
|
|
* } |
|
99
|
|
|
*/ |
|
100
|
22 |
|
protected function parseAddress(string $address, array $variables): string |
|
101
|
|
|
{ |
|
102
|
22 |
|
$fullName = $this->getFullName($variables); |
|
103
|
22 |
|
if (!empty($fullName)) { |
|
104
|
20 |
|
return sprintf('"%s" <%s>', $fullName, $address); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
return $address; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $headerName |
|
112
|
|
|
* @param string $address |
|
113
|
|
|
* @param array $variables { |
|
114
|
|
|
* |
|
115
|
|
|
* @var string $full_name |
|
116
|
|
|
* @var string $first |
|
117
|
|
|
* @var string $last |
|
118
|
|
|
* } |
|
119
|
|
|
*/ |
|
120
|
22 |
|
protected function addRecipient(string $headerName, string $address, array $variables): self |
|
121
|
|
|
{ |
|
122
|
22 |
|
$compiledAddress = $this->parseAddress($address, $variables); |
|
123
|
|
|
|
|
124
|
22 |
|
if ('h:reply-to' === $headerName) { |
|
125
|
1 |
|
$this->message[$headerName] = $compiledAddress; |
|
126
|
21 |
|
} elseif (isset($this->message[$headerName])) { |
|
127
|
2 |
|
$this->message[$headerName][] = $compiledAddress; |
|
128
|
|
|
} else { |
|
129
|
21 |
|
$this->message[$headerName] = [$compiledAddress]; |
|
130
|
|
|
} |
|
131
|
22 |
|
if (array_key_exists($headerName, $this->counters['recipients'])) { |
|
132
|
19 |
|
++$this->counters['recipients'][$headerName]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
22 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $address |
|
140
|
|
|
* @param array $variables { |
|
141
|
|
|
* |
|
142
|
|
|
* @var string $id If used with BatchMessage |
|
143
|
|
|
* @var string $full_name |
|
144
|
|
|
* @var string $first |
|
145
|
|
|
* @var string $last |
|
146
|
|
|
* } |
|
147
|
|
|
* |
|
148
|
|
|
* @throws TooManyRecipients |
|
149
|
|
|
*/ |
|
150
|
13 |
View Code Duplication |
public function addToRecipient(string $address, array $variables = []): self |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
13 |
|
if ($this->counters['recipients']['to'] > self::RECIPIENT_COUNT_LIMIT) { |
|
153
|
|
|
throw TooManyRecipients::create('to'); |
|
154
|
|
|
} |
|
155
|
13 |
|
$this->addRecipient('to', $address, $variables); |
|
156
|
|
|
|
|
157
|
13 |
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $address |
|
162
|
|
|
* @param array $variables { |
|
163
|
|
|
* |
|
164
|
|
|
* @var string $id If used with BatchMessage |
|
165
|
|
|
* @var string $full_name |
|
166
|
|
|
* @var string $first |
|
167
|
|
|
* @var string $last |
|
168
|
|
|
* } |
|
169
|
|
|
* |
|
170
|
|
|
* @throws TooManyRecipients |
|
171
|
|
|
*/ |
|
172
|
4 |
View Code Duplication |
public function addCcRecipient(string $address, array $variables = []): self |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
4 |
|
if ($this->counters['recipients']['cc'] > self::RECIPIENT_COUNT_LIMIT) { |
|
175
|
|
|
throw TooManyRecipients::create('cc'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
4 |
|
$this->addRecipient('cc', $address, $variables); |
|
179
|
|
|
|
|
180
|
4 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param string $address |
|
185
|
|
|
* @param array $variables { |
|
186
|
|
|
* |
|
187
|
|
|
* @var string $id If used with BatchMessage |
|
188
|
|
|
* @var string $full_name |
|
189
|
|
|
* @var string $first |
|
190
|
|
|
* @var string $last |
|
191
|
|
|
* } |
|
192
|
|
|
* |
|
193
|
|
|
* @throws TooManyRecipients |
|
194
|
|
|
*/ |
|
195
|
3 |
View Code Duplication |
public function addBccRecipient(string $address, array $variables = []): self |
|
|
|
|
|
|
196
|
|
|
{ |
|
197
|
3 |
|
if ($this->counters['recipients']['bcc'] > self::RECIPIENT_COUNT_LIMIT) { |
|
198
|
|
|
throw TooManyRecipients::create('bcc'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
3 |
|
$this->addRecipient('bcc', $address, $variables); |
|
202
|
|
|
|
|
203
|
3 |
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param string $address |
|
208
|
|
|
* @param array $variables { |
|
209
|
|
|
* |
|
210
|
|
|
* @var string $id If used with BatchMessage |
|
211
|
|
|
* @var string $full_name |
|
212
|
|
|
* @var string $first |
|
213
|
|
|
* @var string $last |
|
214
|
|
|
* } |
|
215
|
|
|
*/ |
|
216
|
7 |
|
public function setFromAddress(string $address, array $variables = []): self |
|
217
|
|
|
{ |
|
218
|
7 |
|
$this->addRecipient('from', $address, $variables); |
|
219
|
|
|
|
|
220
|
7 |
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $address |
|
225
|
|
|
* @param array $variables { |
|
226
|
|
|
* |
|
227
|
|
|
* @var string $id If used with BatchMessage |
|
228
|
|
|
* @var string $full_name |
|
229
|
|
|
* @var string $first |
|
230
|
|
|
* @var string $last |
|
231
|
|
|
* } |
|
232
|
|
|
*/ |
|
233
|
1 |
|
public function setReplyToAddress(string $address, array $variables = []): self |
|
234
|
|
|
{ |
|
235
|
1 |
|
$this->addRecipient('h:reply-to', $address, $variables); |
|
236
|
|
|
|
|
237
|
1 |
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
7 |
|
public function setSubject(string $subject): self |
|
241
|
|
|
{ |
|
242
|
7 |
|
$this->message['subject'] = $subject; |
|
243
|
|
|
|
|
244
|
7 |
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param string $headerName |
|
249
|
|
|
* @param mixed $headerData |
|
250
|
|
|
*/ |
|
251
|
2 |
|
public function addCustomHeader(string $headerName, $headerData): self |
|
252
|
|
|
{ |
|
253
|
2 |
|
if (!preg_match('/^h:/i', $headerName)) { |
|
254
|
2 |
|
$headerName = 'h:'.$headerName; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
2 |
|
if (!array_key_exists($headerName, $this->message)) { |
|
258
|
2 |
|
$this->message[$headerName] = $headerData; |
|
259
|
|
|
} else { |
|
260
|
1 |
|
if (is_array($this->message[$headerName])) { |
|
261
|
|
|
$this->message[$headerName][] = $headerData; |
|
262
|
|
|
} else { |
|
263
|
1 |
|
$this->message[$headerName] = [$this->message[$headerName], $headerData]; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
2 |
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
7 |
|
public function setTextBody(string $textBody): self |
|
271
|
|
|
{ |
|
272
|
7 |
|
$this->message['text'] = $textBody; |
|
273
|
|
|
|
|
274
|
7 |
|
return $this; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
1 |
|
public function setHtmlBody(string $htmlBody): self |
|
278
|
|
|
{ |
|
279
|
1 |
|
$this->message['html'] = $htmlBody; |
|
280
|
|
|
|
|
281
|
1 |
|
return $this; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
2 |
View Code Duplication |
public function addAttachment(string $attachmentPath, string $attachmentName = null): self |
|
|
|
|
|
|
285
|
|
|
{ |
|
286
|
2 |
|
if (!isset($this->message['attachment'])) { |
|
287
|
2 |
|
$this->message['attachment'] = []; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
2 |
|
$this->message['attachment'][] = [ |
|
291
|
2 |
|
'filePath' => $attachmentPath, |
|
292
|
2 |
|
'remoteName' => $attachmentName, |
|
293
|
|
|
]; |
|
294
|
|
|
|
|
295
|
2 |
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
2 |
View Code Duplication |
public function addInlineImage(string $inlineImagePath, string $inlineImageName = null): self |
|
|
|
|
|
|
299
|
|
|
{ |
|
300
|
2 |
|
if (!isset($this->message['inline'])) { |
|
301
|
2 |
|
$this->message['inline'] = []; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
2 |
|
$this->message['inline'][] = [ |
|
305
|
2 |
|
'filePath' => $inlineImagePath, |
|
306
|
2 |
|
'remoteName' => $inlineImageName, |
|
307
|
|
|
]; |
|
308
|
|
|
|
|
309
|
2 |
|
return $this; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
1 |
|
public function setTestMode(bool $enabled): self |
|
313
|
|
|
{ |
|
314
|
1 |
|
$this->message['o:testmode'] = $enabled ? 'yes' : 'no'; |
|
315
|
|
|
|
|
316
|
1 |
|
return $this; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @throws LimitExceeded |
|
321
|
|
|
*/ |
|
322
|
2 |
View Code Duplication |
public function addCampaignId(string $campaignId): self |
|
|
|
|
|
|
323
|
|
|
{ |
|
324
|
2 |
|
if ($this->counters['attributes']['campaign_id'] >= self::CAMPAIGN_ID_LIMIT) { |
|
325
|
|
|
throw LimitExceeded::create('campaigns', self::CAMPAIGN_ID_LIMIT); |
|
326
|
|
|
} |
|
327
|
2 |
|
if (isset($this->message['o:campaign'])) { |
|
328
|
2 |
|
array_push($this->message['o:campaign'], (string) $campaignId); |
|
329
|
|
|
} else { |
|
330
|
2 |
|
$this->message['o:campaign'] = [(string) $campaignId]; |
|
331
|
|
|
} |
|
332
|
2 |
|
++$this->counters['attributes']['campaign_id']; |
|
333
|
|
|
|
|
334
|
2 |
|
return $this; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @throws LimitExceeded |
|
339
|
|
|
*/ |
|
340
|
|
View Code Duplication |
public function addTag(string $tag): self |
|
|
|
|
|
|
341
|
|
|
{ |
|
342
|
|
|
if ($this->counters['attributes']['tag'] >= self::TAG_LIMIT) { |
|
343
|
|
|
throw LimitExceeded::create('tags', self::TAG_LIMIT); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
if (isset($this->message['o:tag'])) { |
|
347
|
|
|
array_push($this->message['o:tag'], $tag); |
|
348
|
|
|
} else { |
|
349
|
|
|
$this->message['o:tag'] = [$tag]; |
|
350
|
|
|
} |
|
351
|
|
|
++$this->counters['attributes']['tag']; |
|
352
|
|
|
|
|
353
|
|
|
return $this; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
1 |
|
public function setDkim(bool $enabled): self |
|
357
|
|
|
{ |
|
358
|
1 |
|
$this->message['o:dkim'] = $enabled ? 'yes' : 'no'; |
|
359
|
|
|
|
|
360
|
1 |
|
return $this; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
1 |
|
public function setOpenTracking(bool $enabled): self |
|
364
|
|
|
{ |
|
365
|
1 |
|
$this->message['o:tracking-opens'] = $enabled ? 'yes' : 'no'; |
|
366
|
|
|
|
|
367
|
1 |
|
return $this; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
1 |
|
public function setClickTracking(bool $enabled, bool $htmlOnly = false): self |
|
371
|
|
|
{ |
|
372
|
1 |
|
$value = 'no'; |
|
373
|
1 |
|
if ($enabled) { |
|
374
|
1 |
|
$value = 'yes'; |
|
375
|
1 |
|
if ($htmlOnly) { |
|
376
|
1 |
|
$value = 'htmlonly'; |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
1 |
|
$this->message['o:tracking-clicks'] = $value; |
|
381
|
|
|
|
|
382
|
1 |
|
return $this; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
1 |
|
public function setDeliveryTime(string $timeDate, string $timeZone = null): self |
|
386
|
|
|
{ |
|
387
|
1 |
|
if (null !== $timeZone) { |
|
388
|
1 |
|
$timeZoneObj = new \DateTimeZone($timeZone); |
|
389
|
|
|
} else { |
|
390
|
1 |
|
$timeZoneObj = new \DateTimeZone('UTC'); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
1 |
|
$dateTimeObj = new \DateTime($timeDate, $timeZoneObj); |
|
394
|
1 |
|
$formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); |
|
395
|
1 |
|
$this->message['o:deliverytime'] = $formattedTimeDate; |
|
396
|
|
|
|
|
397
|
1 |
|
return $this; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* @param string $customName |
|
402
|
|
|
* @param mixed $data |
|
403
|
|
|
*/ |
|
404
|
1 |
|
public function addCustomData(string $customName, $data): self |
|
405
|
|
|
{ |
|
406
|
1 |
|
$this->message['v:'.$customName] = json_encode($data); |
|
407
|
|
|
|
|
408
|
1 |
|
return $this; |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* @param string $parameterName |
|
413
|
|
|
* @param mixed $data |
|
414
|
|
|
*/ |
|
415
|
1 |
|
public function addCustomParameter(string $parameterName, $data): self |
|
416
|
|
|
{ |
|
417
|
1 |
|
if (isset($this->message[$parameterName])) { |
|
418
|
|
|
$this->message[$parameterName][] = $data; |
|
419
|
|
|
} else { |
|
420
|
1 |
|
$this->message[$parameterName] = [$data]; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
1 |
|
return $this; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
1 |
|
public function setMessage(array $message): self |
|
427
|
|
|
{ |
|
428
|
1 |
|
$this->message = $message; |
|
429
|
|
|
|
|
430
|
1 |
|
return $this; |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
25 |
|
public function getMessage(): array |
|
434
|
|
|
{ |
|
435
|
25 |
|
return $this->message; |
|
436
|
|
|
} |
|
437
|
|
|
} |
|
438
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.