/wp-content/plugins/kali-forms/Inc/Utils/EmailUtilities/class-postmark-helper.php

https://gitlab.com/chernushov881/charity-fund · PHP · 188 lines · 115 code · 15 blank · 58 comment · 10 complexity · 888489541221407ab799daab3a569093 MD5 · raw file

  1. <?php
  2. namespace KaliForms\Inc\Utils\EmailUtilities;
  3. class Postmark_Helper
  4. {
  5. /**
  6. * Protected URL for the Sendinblue_Helper
  7. *
  8. * @var string
  9. */
  10. protected $url = 'https://api.postmarkapp.com/email';
  11. /**
  12. * The send mail object
  13. *
  14. * @var array
  15. */
  16. protected $send_mail_obj = [];
  17. /**
  18. * $token (api key)
  19. *
  20. * @var string
  21. */
  22. protected $token = '';
  23. protected $forbidden_attach_ext = ['vbs', 'exe', 'bin', 'bat', 'chm', 'com', 'cpl', 'crt', 'hlp', 'hta', 'inf', 'ins', 'isp', 'jse', 'lnk', 'mdb', 'pcd', 'pif', 'reg', 'scr', 'sct', 'shs', 'vbe', 'vba', 'wsf', 'wsh', 'wsl', 'msc', 'msi', 'msp', 'mst'];
  24. /**
  25. * Class constructor
  26. */
  27. public function __construct()
  28. {
  29. }
  30. /**
  31. * Send the actual email
  32. *
  33. * @return void
  34. */
  35. public function send()
  36. {
  37. if (empty($this->token)) {
  38. throw new \Exception(__('No Api Key configured', 'kaliforms'));
  39. }
  40. return $this->make_request();
  41. }
  42. /**
  43. * Set user data
  44. *
  45. * @return void
  46. */
  47. public function set_data($PHPMAILER)
  48. {
  49. $this->token = $PHPMAILER->Postmark_Api_Key;
  50. $recipients = $this->_get_recipients($PHPMAILER);
  51. if (!empty($recipients['ReplyTo'])) {
  52. $this->send_mail_obj['ReplyTo'] = $this->_format_array_of_address($recipients['ReplyTo']);
  53. };
  54. $this->send_mail_obj['To'] = $this->_format_array_of_address($recipients['To']);
  55. $this->send_mail_obj['Cc'] = $this->_format_array_of_address($recipients['Cc']);
  56. $this->send_mail_obj['Bcc'] = $this->_format_array_of_address($recipients['Bcc']);
  57. $this->send_mail_obj['From'] = $this->_format_address($PHPMAILER->From, $PHPMAILER->FromName);
  58. $this->send_mail_obj['Subject'] = $PHPMAILER->Subject;
  59. $this->send_mail_obj['HtmlBody'] = $PHPMAILER->Body;
  60. $this->send_mail_obj['TextBody'] = $PHPMAILER->AltBody;
  61. $this->send_mail_obj['MessageStream'] = 'outbound';
  62. $this->send_mail_obj['Attachments'] = $this->get_attachments($PHPMAILER);
  63. $this->send_mail_obj = array_filter($this->send_mail_obj);
  64. }
  65. /**
  66. * Format the address as we need it
  67. *
  68. * @param string $email
  69. * @param string $name
  70. * @return string
  71. */
  72. private function _format_address($email = '', $name = '')
  73. {
  74. return empty($name) ? sprintf('<%s>', $email) : sprintf('%s<%s>', $name, $email);
  75. }
  76. /**
  77. * Formats an array of addresses
  78. *
  79. * @param [type] $arr
  80. * @return string
  81. */
  82. public function _format_array_of_address($arr)
  83. {
  84. $ret_array = [];
  85. foreach ($arr as $address) {
  86. if (empty($address)) {
  87. continue;
  88. }
  89. $ret_array[] = $this->_format_address($address[0]);
  90. }
  91. return implode(',', $ret_array);
  92. }
  93. /**
  94. * Get the recipients
  95. *
  96. * @param [type] $PHPMAILER
  97. * @return void
  98. */
  99. public function _get_recipients($PHPMAILER)
  100. {
  101. return [
  102. 'To' => array_filter($PHPMAILER->getToAddresses()),
  103. 'Cc' => array_filter($PHPMAILER->getCcAddresses()),
  104. 'Bcc' => array_filter($PHPMAILER->getBccAddresses()),
  105. 'ReplyTo' => array_filter($PHPMAILER->getReplyToAddresses()),
  106. ];
  107. }
  108. /**
  109. * Get attachments
  110. *
  111. * @param [type] $PHPMAILER
  112. * @return void
  113. */
  114. public function get_attachments($PHPMAILER)
  115. {
  116. $attachments = $PHPMAILER->getAttachments();
  117. if (empty($attachments)) {
  118. return [];
  119. }
  120. $data = [];
  121. foreach ($attachments as $attachment) {
  122. $file = false;
  123. $mime = 'text/plain';
  124. try {
  125. if (is_file($attachment[0]) && is_readable($attachment[0])) {
  126. $ext = pathinfo($attachment[0], PATHINFO_EXTENSION);
  127. $mime = mime_content_type($attachment[0]);
  128. if (!in_array($ext, $this->forbidden_attach_ext, true)) {
  129. $file = file_get_contents($attachment[0]);
  130. }
  131. }
  132. } catch (\Exception $e) {
  133. $file = false;
  134. }
  135. if ($file === false) {
  136. continue;
  137. }
  138. $data[] = [
  139. 'Name' => $attachment[2],
  140. 'Content' => base64_encode($file),
  141. 'ContentType' => $mime,
  142. ];
  143. }
  144. return $data;
  145. }
  146. /**
  147. * Send request
  148. *
  149. * @return void
  150. */
  151. public function make_request()
  152. {
  153. $response = wp_remote_post(
  154. $this->url,
  155. [
  156. 'headers' => [
  157. 'X-Postmark-Server-Token' => $this->token,
  158. 'content-type' => 'application/json',
  159. 'accept' => 'application/json',
  160. ],
  161. 'body' => json_encode($this->send_mail_obj),
  162. ]
  163. );
  164. if (is_wp_error($response)) {
  165. throw new \Exception($response->get_error_message(), $response->get_error_code());
  166. }
  167. $data = json_decode(wp_remote_retrieve_body($response));
  168. if ($data->ErrorCode > 0) {
  169. throw new \Exception($data->ErrorCode . ' ' . $data->Message);
  170. };
  171. return true;
  172. }
  173. }