BatchMessage.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?PHP
  2. namespace Mailgun\Messages;
  3. use Mailgun\Messages\MessageBuilder;
  4. use Mailgun\Messages\Exceptions\TooManyParameters;
  5. use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
  6. /*
  7. This class is used for batch sending. See the official documentation
  8. for usage instructions.
  9. */
  10. class BatchMessage extends MessageBuilder{
  11. private $batchRecipientAttributes;
  12. private $autoSend;
  13. private $restClient;
  14. private $workingDomain;
  15. private $messageIds = array();
  16. public function __construct($restClient, $workingDomain, $autoSend){
  17. $this->batchRecipientAttributes = array();
  18. $this->autoSend = $autoSend;
  19. $this->restClient = $restClient;
  20. $this->workingDomain = $workingDomain;
  21. $this->endpointUrl = $workingDomain . "/messages";
  22. }
  23. protected function addRecipient($headerName, $address, $variables){
  24. if(array_key_exists($headerName, $this->counters['recipients'])){
  25. if($this->counters['recipients'][$headerName] == RECIPIENT_COUNT_LIMIT){
  26. if($this->autoSend == false){
  27. throw new TooManyParameters(TOO_MANY_RECIPIENTS);
  28. }
  29. $this->sendMessage();
  30. }
  31. }
  32. $compiledAddress = $this->parseAddress($address, $variables);
  33. if(isset($this->message[$headerName])){
  34. array_push($this->message[$headerName], $compiledAddress);
  35. }
  36. elseif($headerName == "h:reply-to"){
  37. $this->message[$headerName] = $compiledAddress;
  38. }
  39. else{
  40. $this->message[$headerName] = array($compiledAddress);
  41. }
  42. if(array_key_exists($headerName, $this->counters['recipients'])){
  43. $this->counters['recipients'][$headerName] += 1;
  44. if(!array_key_exists("id", $variables)){
  45. $variables['id'] = $this->counters['recipients'][$headerName];
  46. }
  47. }
  48. $this->batchRecipientAttributes["$address"] = $variables;
  49. }
  50. public function sendMessage($message = array(), $files = array()){
  51. if(count($message) < 1){
  52. $message = $this->message;
  53. $files = $this->files;
  54. }
  55. if(!array_key_exists("from", $message)){
  56. throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
  57. }
  58. elseif(!array_key_exists("to", $message)){
  59. throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
  60. }
  61. elseif(!array_key_exists("subject", $message)){
  62. throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
  63. }
  64. elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){
  65. throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
  66. }
  67. else{
  68. $message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
  69. $response = $this->restClient->post($this->endpointUrl, $message, $files);
  70. $this->batchRecipientAttributes = array();
  71. $this->counters['recipients']['to'] = 0;
  72. $this->counters['recipients']['cc'] = 0;
  73. $this->counters['recipients']['bcc'] = 0;
  74. unset($this->message["to"]);
  75. array_push($this->messageIds, $response->http_response_body->id);
  76. }
  77. }
  78. public function finalize(){
  79. return $this->sendMessage();
  80. }
  81. public function getMessageIds(){
  82. return $this->messageIds;
  83. }
  84. }