Exceptions
7 years ago
BatchMessage.php
7 years ago
MessageBuilder.php
7 years ago
README.md
7 years ago
MessageBuilder.php
543 lines
| 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 | private function get($params, $key, $default) |
| 66 | { |
| 67 | if (array_key_exists($key, $params)) { |
| 68 | return $params[$key]; |
| 69 | } |
| 70 | |
| 71 | 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 | * @return string |
| 83 | */ |
| 84 | private function getFullName(array $params) |
| 85 | { |
| 86 | if (isset($params['full_name'])) { |
| 87 | return $this->get($params, 'full_name', ''); |
| 88 | } |
| 89 | |
| 90 | return trim(sprintf('%s %s', $this->get($params, 'first', ''), $this->get($params, 'last', ''))); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param string $address |
| 95 | * @param array $params { |
| 96 | * |
| 97 | * @var string $full_name |
| 98 | * @var string $first |
| 99 | * @var string $last |
| 100 | * } |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | protected function parseAddress($address, array $variables) |
| 105 | { |
| 106 | $fullName = $this->getFullName($variables); |
| 107 | if (!empty($fullName)) { |
| 108 | return sprintf('"%s" <%s>', $fullName, $address); |
| 109 | } |
| 110 | |
| 111 | return $address; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param string $headerName |
| 116 | * @param string $address |
| 117 | * @param array $variables { |
| 118 | * |
| 119 | * @var string $full_name |
| 120 | * @var string $first |
| 121 | * @var string $last |
| 122 | * } |
| 123 | * |
| 124 | * @return MessageBuilder |
| 125 | */ |
| 126 | protected function addRecipient($headerName, $address, array $variables) |
| 127 | { |
| 128 | $compiledAddress = $this->parseAddress($address, $variables); |
| 129 | |
| 130 | if ('h:reply-to' === $headerName) { |
| 131 | $this->message[$headerName] = $compiledAddress; |
| 132 | } elseif (isset($this->message[$headerName])) { |
| 133 | $this->message[$headerName][] = $compiledAddress; |
| 134 | } else { |
| 135 | $this->message[$headerName] = [$compiledAddress]; |
| 136 | } |
| 137 | if (array_key_exists($headerName, $this->counters['recipients'])) { |
| 138 | $this->counters['recipients'][$headerName] += 1; |
| 139 | } |
| 140 | |
| 141 | return $this; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @param string $address |
| 146 | * @param array $variables { |
| 147 | * |
| 148 | * @var string $id If used with BatchMessage |
| 149 | * @var string $full_name |
| 150 | * @var string $first |
| 151 | * @var string $last |
| 152 | * } |
| 153 | * |
| 154 | * @throws TooManyRecipients |
| 155 | * |
| 156 | * @return MessageBuilder |
| 157 | */ |
| 158 | public function addToRecipient($address, array $variables = []) |
| 159 | { |
| 160 | if ($this->counters['recipients']['to'] > self::RECIPIENT_COUNT_LIMIT) { |
| 161 | throw TooManyRecipients::create('to'); |
| 162 | } |
| 163 | $this->addRecipient('to', $address, $variables); |
| 164 | |
| 165 | return $this; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param string $address |
| 170 | * @param array $variables { |
| 171 | * |
| 172 | * @var string $id If used with BatchMessage |
| 173 | * @var string $full_name |
| 174 | * @var string $first |
| 175 | * @var string $last |
| 176 | * } |
| 177 | * |
| 178 | * @throws TooManyRecipients |
| 179 | * |
| 180 | * @return MessageBuilder |
| 181 | */ |
| 182 | public function addCcRecipient($address, array $variables = []) |
| 183 | { |
| 184 | if ($this->counters['recipients']['cc'] > self::RECIPIENT_COUNT_LIMIT) { |
| 185 | throw TooManyRecipients::create('cc'); |
| 186 | } |
| 187 | |
| 188 | $this->addRecipient('cc', $address, $variables); |
| 189 | |
| 190 | return $this; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param string $address |
| 195 | * @param array $variables { |
| 196 | * |
| 197 | * @var string $id If used with BatchMessage |
| 198 | * @var string $full_name |
| 199 | * @var string $first |
| 200 | * @var string $last |
| 201 | * } |
| 202 | * |
| 203 | * @throws TooManyRecipients |
| 204 | * |
| 205 | * @return MessageBuilder |
| 206 | */ |
| 207 | public function addBccRecipient($address, array $variables = []) |
| 208 | { |
| 209 | if ($this->counters['recipients']['bcc'] > self::RECIPIENT_COUNT_LIMIT) { |
| 210 | throw TooManyRecipients::create('bcc'); |
| 211 | } |
| 212 | |
| 213 | $this->addRecipient('bcc', $address, $variables); |
| 214 | |
| 215 | return $this; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @param string $address |
| 220 | * @param array $variables { |
| 221 | * |
| 222 | * @var string $id If used with BatchMessage |
| 223 | * @var string $full_name |
| 224 | * @var string $first |
| 225 | * @var string $last |
| 226 | * } |
| 227 | * |
| 228 | * @return MessageBuilder |
| 229 | */ |
| 230 | public function setFromAddress($address, array $variables = []) |
| 231 | { |
| 232 | $this->addRecipient('from', $address, $variables); |
| 233 | |
| 234 | return $this; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @param string $address |
| 239 | * @param array $variables { |
| 240 | * |
| 241 | * @var string $id If used with BatchMessage |
| 242 | * @var string $full_name |
| 243 | * @var string $first |
| 244 | * @var string $last |
| 245 | * } |
| 246 | * |
| 247 | * @return MessageBuilder |
| 248 | */ |
| 249 | public function setReplyToAddress($address, array $variables = []) |
| 250 | { |
| 251 | $this->addRecipient('h:reply-to', $address, $variables); |
| 252 | |
| 253 | return $this; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @param string $subject |
| 258 | * |
| 259 | * @return MessageBuilder |
| 260 | */ |
| 261 | public function setSubject($subject) |
| 262 | { |
| 263 | $this->message['subject'] = $subject; |
| 264 | |
| 265 | return $this; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @param string $headerName |
| 270 | * @param mixed $headerData |
| 271 | * |
| 272 | * @return MessageBuilder |
| 273 | */ |
| 274 | public function addCustomHeader($headerName, $headerData) |
| 275 | { |
| 276 | if (!preg_match('/^h:/i', $headerName)) { |
| 277 | $headerName = 'h:'.$headerName; |
| 278 | } |
| 279 | |
| 280 | if (!array_key_exists($headerName, $this->message)) { |
| 281 | $this->message[$headerName] = $headerData; |
| 282 | } else { |
| 283 | if (is_array($this->message[$headerName])) { |
| 284 | $this->message[$headerName][] = $headerData; |
| 285 | } else { |
| 286 | $this->message[$headerName] = [$this->message[$headerName], $headerData]; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return $this; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @param string $textBody |
| 295 | * |
| 296 | * @return MessageBuilder |
| 297 | */ |
| 298 | public function setTextBody($textBody) |
| 299 | { |
| 300 | $this->message['text'] = $textBody; |
| 301 | |
| 302 | return $this; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @param string $htmlBody |
| 307 | * |
| 308 | * @return MessageBuilder |
| 309 | */ |
| 310 | public function setHtmlBody($htmlBody) |
| 311 | { |
| 312 | $this->message['html'] = $htmlBody; |
| 313 | |
| 314 | return $this; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @param string $attachmentPath |
| 319 | * @param string|null $attachmentName |
| 320 | * |
| 321 | * @return MessageBuilder |
| 322 | */ |
| 323 | public function addAttachment($attachmentPath, $attachmentName = null) |
| 324 | { |
| 325 | if (!isset($this->message['attachment'])) { |
| 326 | $this->message['attachment'] = []; |
| 327 | } |
| 328 | |
| 329 | $this->message['attachment'][] = [ |
| 330 | 'filePath' => $attachmentPath, |
| 331 | 'remoteName' => $attachmentName, |
| 332 | ]; |
| 333 | |
| 334 | return $this; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * @param string $inlineImagePath |
| 339 | * @param string|null $inlineImageName |
| 340 | * |
| 341 | * @return MessageBuilder |
| 342 | */ |
| 343 | public function addInlineImage($inlineImagePath, $inlineImageName = null) |
| 344 | { |
| 345 | if (!isset($this->message['inline'])) { |
| 346 | $this->message['inline'] = []; |
| 347 | } |
| 348 | |
| 349 | $this->message['inline'][] = [ |
| 350 | 'filePath' => $inlineImagePath, |
| 351 | 'remoteName' => $inlineImageName, |
| 352 | ]; |
| 353 | |
| 354 | return $this; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * @param bool $enabled |
| 359 | * |
| 360 | * @return MessageBuilder |
| 361 | */ |
| 362 | public function setTestMode($enabled) |
| 363 | { |
| 364 | $this->message['o:testmode'] = $this->boolToString($enabled); |
| 365 | |
| 366 | return $this; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @param string $campaignId |
| 371 | * |
| 372 | * @throws LimitExceeded |
| 373 | * |
| 374 | * @return MessageBuilder |
| 375 | */ |
| 376 | public function addCampaignId($campaignId) |
| 377 | { |
| 378 | if ($this->counters['attributes']['campaign_id'] >= self::CAMPAIGN_ID_LIMIT) { |
| 379 | throw LimitExceeded::create('campaigns', self::CAMPAIGN_ID_LIMIT); |
| 380 | } |
| 381 | if (isset($this->message['o:campaign'])) { |
| 382 | array_push($this->message['o:campaign'], (string) $campaignId); |
| 383 | } else { |
| 384 | $this->message['o:campaign'] = [(string) $campaignId]; |
| 385 | } |
| 386 | $this->counters['attributes']['campaign_id'] += 1; |
| 387 | |
| 388 | return $this; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @param string $tag |
| 393 | * |
| 394 | * @throws LimitExceeded |
| 395 | * |
| 396 | * @return MessageBuilder |
| 397 | */ |
| 398 | public function addTag($tag) |
| 399 | { |
| 400 | if ($this->counters['attributes']['tag'] >= self::TAG_LIMIT) { |
| 401 | throw LimitExceeded::create('tags', self::TAG_LIMIT); |
| 402 | } |
| 403 | |
| 404 | if (isset($this->message['o:tag'])) { |
| 405 | array_push($this->message['o:tag'], $tag); |
| 406 | } else { |
| 407 | $this->message['o:tag'] = [$tag]; |
| 408 | } |
| 409 | $this->counters['attributes']['tag'] += 1; |
| 410 | |
| 411 | return $this; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @param bool $enabled |
| 416 | * |
| 417 | * @return MessageBuilder |
| 418 | */ |
| 419 | public function setDkim($enabled) |
| 420 | { |
| 421 | $this->message['o:dkim'] = $this->boolToString($enabled); |
| 422 | |
| 423 | return $this; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * @param bool $enabled |
| 428 | * |
| 429 | * @return MessageBuilder |
| 430 | */ |
| 431 | public function setOpenTracking($enabled) |
| 432 | { |
| 433 | $this->message['o:tracking-opens'] = $this->boolToString($enabled); |
| 434 | |
| 435 | return $this; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * @param bool $enabled |
| 440 | * |
| 441 | * @return MessageBuilder |
| 442 | */ |
| 443 | public function setClickTracking($enabled) |
| 444 | { |
| 445 | $this->message['o:tracking-clicks'] = $this->boolToString($enabled); |
| 446 | |
| 447 | return $this; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * @param string $timeDate |
| 452 | * @param string|null $timeZone |
| 453 | * |
| 454 | * @return string |
| 455 | * |
| 456 | * @deprecated The return value is deprecated. This method will return $this in version 3.0. |
| 457 | */ |
| 458 | public function setDeliveryTime($timeDate, $timeZone = null) |
| 459 | { |
| 460 | if (null !== $timeZone) { |
| 461 | $timeZoneObj = new \DateTimeZone($timeZone); |
| 462 | } else { |
| 463 | $timeZoneObj = new \DateTimeZone('UTC'); |
| 464 | } |
| 465 | |
| 466 | $dateTimeObj = new \DateTime($timeDate, $timeZoneObj); |
| 467 | $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); |
| 468 | $this->message['o:deliverytime'] = $formattedTimeDate; |
| 469 | |
| 470 | return $this->message['o:deliverytime']; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * @param string $customName |
| 475 | * @param mixed $data |
| 476 | * |
| 477 | * @return MessageBuilder |
| 478 | */ |
| 479 | public function addCustomData($customName, $data) |
| 480 | { |
| 481 | $this->message['v:'.$customName] = json_encode($data); |
| 482 | |
| 483 | return $this; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * @param string $parameterName |
| 488 | * @param mixed $data |
| 489 | * |
| 490 | * @return mixed |
| 491 | * |
| 492 | * @deprecated The return value is deprecated. This method will return $this in version 3.0. |
| 493 | */ |
| 494 | public function addCustomParameter($parameterName, $data) |
| 495 | { |
| 496 | if (isset($this->message[$parameterName])) { |
| 497 | $this->message[$parameterName][] = $data; |
| 498 | } else { |
| 499 | $this->message[$parameterName] = [$data]; |
| 500 | } |
| 501 | |
| 502 | return $this->message[$parameterName]; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * @param array $message |
| 507 | * |
| 508 | * @return MessageBuilder |
| 509 | */ |
| 510 | public function setMessage($message) |
| 511 | { |
| 512 | $this->message = $message; |
| 513 | |
| 514 | return $this; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * @return array |
| 519 | */ |
| 520 | public function getMessage() |
| 521 | { |
| 522 | return $this->message; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * @param $enabled |
| 527 | * |
| 528 | * @return string |
| 529 | */ |
| 530 | private function boolToString($enabled) |
| 531 | { |
| 532 | if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { |
| 533 | $enabled = 'yes'; |
| 534 | } elseif ('html' === $enabled) { |
| 535 | $enabled = 'html'; |
| 536 | } else { |
| 537 | $enabled = 'no'; |
| 538 | } |
| 539 | |
| 540 | return $enabled; |
| 541 | } |
| 542 | } |
| 543 |