MessageBuilder.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?PHP
  2. namespace Mailgun\Messages;
  3. use Mailgun\Messages\Exceptions\InvalidParameter;
  4. use Mailgun\Messages\Exceptions\TooManyParameters;
  5. use Mailgun\Messages\Exceptions\InvalidParameterType;
  6. /*
  7. This class is used for composing a properly formed
  8. message object. Dealing with arrays can be cumbersome,
  9. this class makes the process easier. See the official
  10. documentation for usage instructions.
  11. */
  12. class MessageBuilder
  13. {
  14. protected $message = array();
  15. protected $variables = array();
  16. protected $files = array();
  17. protected $counters = array(
  18. 'recipients' => array(
  19. 'to' => 0,
  20. 'cc' => 0,
  21. 'bcc' => 0
  22. ),
  23. 'attributes' => array(
  24. 'attachment' => 0,
  25. 'campaign_id' => 0,
  26. 'custom_option' => 0,
  27. 'tag' => 0
  28. )
  29. );
  30. protected function safeGet($params, $key, $default)
  31. {
  32. if (array_key_exists($key, $params)) {
  33. return $params[$key];
  34. }
  35. return $default;
  36. }
  37. protected function getFullName($params)
  38. {
  39. if (array_key_exists("first", $params)) {
  40. $first = $this->safeGet($params, "first", "");
  41. $last = $this->safeGet($params, "last", "");
  42. return trim("$first $last");
  43. }
  44. return $this->safeGet($params, "full_name", "");
  45. }
  46. protected function parseAddress($address, $variables)
  47. {
  48. if (!is_array($variables)) {
  49. return $address;
  50. }
  51. $fullName = $this->getFullName($variables);
  52. if ($fullName != null) {
  53. return "'$fullName' <$address>";
  54. }
  55. return $address;
  56. }
  57. protected function addRecipient($headerName, $address, $variables)
  58. {
  59. $compiledAddress = $this->parseAddress($address, $variables);
  60. if (isset($this->message[$headerName])) {
  61. array_push($this->message[$headerName], $compiledAddress);
  62. } elseif ($headerName == "h:reply-to") {
  63. $this->message[$headerName] = $compiledAddress;
  64. } else {
  65. $this->message[$headerName] = array($compiledAddress);
  66. }
  67. if (array_key_exists($headerName, $this->counters['recipients'])) {
  68. $this->counters['recipients'][$headerName] += 1;
  69. }
  70. }
  71. public function addToRecipient($address, $variables = null)
  72. {
  73. if ($this->counters['recipients']['to'] > RECIPIENT_COUNT_LIMIT) {
  74. throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
  75. }
  76. $this->addRecipient("to", $address, $variables);
  77. return end($this->message['to']);
  78. }
  79. public function addCcRecipient($address, $variables = null)
  80. {
  81. if ($this->counters['recipients']['cc'] > RECIPIENT_COUNT_LIMIT) {
  82. throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
  83. }
  84. $this->addRecipient("cc", $address, $variables);
  85. return end($this->message['cc']);
  86. }
  87. public function addBccRecipient($address, $variables = null)
  88. {
  89. if ($this->counters['recipients']['bcc'] > RECIPIENT_COUNT_LIMIT) {
  90. throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
  91. }
  92. $this->addRecipient("bcc", $address, $variables);
  93. return end($this->message['bcc']);
  94. }
  95. public function setFromAddress($address, $variables = null)
  96. {
  97. $this->addRecipient("from", $address, $variables);
  98. return $this->message['from'];
  99. }
  100. public function setReplyToAddress($address, $variables = null)
  101. {
  102. $this->addRecipient("h:reply-to", $address, $variables);
  103. return $this->message['h:reply-to'];
  104. }
  105. public function setSubject($subject = null)
  106. {
  107. if ($subject == null || $subject == "") {
  108. $subject = " ";
  109. }
  110. $this->message['subject'] = $subject;
  111. return $this->message['subject'];
  112. }
  113. public function addCustomHeader($headerName, $headerData)
  114. {
  115. if (!preg_match("/^h:/i", $headerName)) {
  116. $headerName = "h:" . $headerName;
  117. }
  118. $this->message[$headerName] = array($headerData);
  119. return $this->message[$headerName];
  120. }
  121. public function setTextBody($textBody)
  122. {
  123. if ($textBody == null || $textBody == "") {
  124. $textBody = " ";
  125. }
  126. $this->message['text'] = $textBody;
  127. return $this->message['text'];
  128. }
  129. public function setHtmlBody($htmlBody)
  130. {
  131. if ($htmlBody == null || $htmlBody == "") {
  132. $htmlBody = " ";
  133. }
  134. $this->message['html'] = $htmlBody;
  135. return $this->message['html'];
  136. }
  137. public function addAttachment($attachmentPath, $attachmentName = null)
  138. {
  139. if (isset($this->files["attachment"])) {
  140. $attachment = array(
  141. 'filePath' => $attachmentPath,
  142. 'remoteName' => $attachmentName
  143. );
  144. array_push($this->files["attachment"], $attachment);
  145. } else {
  146. $this->files["attachment"] = array(
  147. array(
  148. 'filePath' => $attachmentPath,
  149. 'remoteName' => $attachmentName
  150. )
  151. );
  152. }
  153. return true;
  154. }
  155. public function addInlineImage($inlineImagePath, $inlineImageName = null)
  156. {
  157. if (preg_match("/^@/", $inlineImagePath)) {
  158. if (isset($this->files['inline'])) {
  159. $inlineAttachment = array(
  160. 'filePath' => $inlineImagePath,
  161. 'remoteName' => $inlineImageName
  162. );
  163. array_push($this->files['inline'], $inlineAttachment);
  164. } else {
  165. $this->files['inline'] = array(
  166. array(
  167. 'filePath' => $inlineImagePath,
  168. 'remoteName' => $inlineImageName
  169. )
  170. );
  171. }
  172. return true;
  173. } else {
  174. throw new InvalidParameter(INVALID_PARAMETER_INLINE);
  175. }
  176. }
  177. public function setTestMode($testMode)
  178. {
  179. if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
  180. $testMode = "yes";
  181. } else {
  182. $testMode = "no";
  183. }
  184. $this->message['o:testmode'] = $testMode;
  185. return $this->message['o:testmode'];
  186. }
  187. public function addCampaignId($campaignId)
  188. {
  189. if ($this->counters['attributes']['campaign_id'] < CAMPAIGN_ID_LIMIT) {
  190. if (isset($this->message['o:campaign'])) {
  191. array_push($this->message['o:campaign'], $campaignId);
  192. } else {
  193. $this->message['o:campaign'] = array($campaignId);
  194. }
  195. $this->counters['attributes']['campaign_id'] += 1;
  196. return $this->message['o:campaign'];
  197. } else {
  198. throw new TooManyParameters(TOO_MANY_PARAMETERS_CAMPAIGNS);
  199. }
  200. }
  201. public function addTag($tag)
  202. {
  203. if ($this->counters['attributes']['tag'] < TAG_LIMIT) {
  204. if (isset($this->message['o:tag'])) {
  205. array_push($this->message['o:tag'], $tag);
  206. } else {
  207. $this->message['o:tag'] = array($tag);
  208. }
  209. $this->counters['attributes']['tag'] += 1;
  210. return $this->message['o:tag'];
  211. } else {
  212. throw new TooManyParameters(TOO_MANY_PARAMETERS_TAGS);
  213. }
  214. }
  215. public function setDkim($enabled)
  216. {
  217. if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
  218. $enabled = "yes";
  219. } else {
  220. $enabled = "no";
  221. }
  222. $this->message["o:dkim"] = $enabled;
  223. return $this->message["o:dkim"];
  224. }
  225. public function setOpenTracking($enabled)
  226. {
  227. if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
  228. $enabled = "yes";
  229. } else {
  230. $enabled = "no";
  231. }
  232. $this->message['o:tracking-opens'] = $enabled;
  233. return $this->message['o:tracking-opens'];
  234. }
  235. public function setClickTracking($enabled)
  236. {
  237. if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
  238. $enabled = "yes";
  239. } elseif ($enabled == "html") {
  240. $enabled = "html";
  241. } else {
  242. $enabled = "no";
  243. }
  244. $this->message['o:tracking-clicks'] = $enabled;
  245. return $this->message['o:tracking-clicks'];
  246. }
  247. public function setDeliveryTime($timeDate, $timeZone = null)
  248. {
  249. if (isset($timeZone)) {
  250. $timeZoneObj = new \DateTimeZone("$timeZone");
  251. } else {
  252. $timeZoneObj = new \DateTimeZone(\DEFAULT_TIME_ZONE);
  253. }
  254. $dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
  255. $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822);
  256. $this->message['o:deliverytime'] = $formattedTimeDate;
  257. return $this->message['o:deliverytime'];
  258. }
  259. public function addCustomData($customName, $data)
  260. {
  261. $this->message['v:' . $customName] = json_encode($data);
  262. }
  263. public function addCustomParameter($parameterName, $data)
  264. {
  265. if (isset($this->message[$parameterName])) {
  266. array_push($this->message[$parameterName], $data);
  267. return $this->message[$parameterName];
  268. } else {
  269. $this->message[$parameterName] = array($data);
  270. return $this->message[$parameterName];
  271. }
  272. }
  273. public function setMessage($message)
  274. {
  275. $this->message = $message;
  276. }
  277. public function getMessage()
  278. {
  279. return $this->message;
  280. }
  281. public function getFiles()
  282. {
  283. return $this->files;
  284. }
  285. }