Mailgun.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?PHP
  2. namespace Mailgun;
  3. use Http\Client\HttpClient;
  4. use Mailgun\Constants\ExceptionMessages;
  5. use Mailgun\Messages\Exceptions;
  6. use Mailgun\Connection\RestClient;
  7. use Mailgun\Messages\BatchMessage;
  8. use Mailgun\Lists\OptInHandler;
  9. use Mailgun\Messages\MessageBuilder;
  10. /**
  11. * This class is the base class for the Mailgun SDK.
  12. * See the official documentation (link below) for usage instructions.
  13. *
  14. * @link https://github.com/mailgun/mailgun-php/blob/master/README.md
  15. */
  16. class Mailgun{
  17. /**
  18. * @var RestClient
  19. */
  20. protected $restClient;
  21. /**
  22. * @var null|string
  23. */
  24. protected $apiKey;
  25. /**
  26. * @param string|null $apiKey
  27. * @param HttpClient $httpClient
  28. * @param string $apiEndpoint
  29. */
  30. public function __construct(
  31. $apiKey = null,
  32. HttpClient $httpClient = null,
  33. $apiEndpoint = 'api.mailgun.net'
  34. ) {
  35. $this->apiKey = $apiKey;
  36. $this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient);
  37. }
  38. /**
  39. * This function allows the sending of a fully formed message OR a custom
  40. * MIME string. If sending MIME, the string must be passed in to the 3rd
  41. * position of the function call.
  42. *
  43. * @param string $workingDomain
  44. * @param array $postData
  45. * @param array $postFiles
  46. * @throws Exceptions\MissingRequiredMIMEParameters
  47. */
  48. public function sendMessage($workingDomain, $postData, $postFiles = array()){
  49. if(is_array($postFiles)){
  50. return $this->post("$workingDomain/messages", $postData, $postFiles);
  51. }
  52. else if(is_string($postFiles)){
  53. $tempFile = tempnam(sys_get_temp_dir(), "MG_TMP_MIME");
  54. $fileHandle = fopen($tempFile, "w");
  55. fwrite($fileHandle, $postFiles);
  56. $result = $this->post("$workingDomain/messages.mime", $postData, array("message" => $tempFile));
  57. fclose($fileHandle);
  58. unlink($tempFile);
  59. return $result;
  60. }
  61. else{
  62. throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
  63. }
  64. }
  65. /**
  66. * This function checks the signature in a POST request to see if it is
  67. * authentic.
  68. *
  69. * Pass an array of parameters. If you pass nothing, $_POST will be
  70. * used instead.
  71. *
  72. * If this function returns FALSE, you must not process the request.
  73. * You should reject the request with status code 403 Forbidden.
  74. *
  75. * @param array|null $postData
  76. * @return bool
  77. */
  78. public function verifyWebhookSignature($postData = NULL) {
  79. if(is_null($postData)) {
  80. $postData = $_POST;
  81. }
  82. $hmac = hash_hmac('sha256', "{$postData["timestamp"]}{$postData["token"]}", $this->apiKey);
  83. $sig = $postData['signature'];
  84. if(function_exists('hash_equals')) {
  85. // hash_equals is constant time, but will not be introduced until PHP 5.6
  86. return hash_equals($hmac, $sig);
  87. }
  88. else {
  89. return ($hmac == $sig);
  90. }
  91. }
  92. /**
  93. * @param string $endpointUrl
  94. * @param array $postData
  95. * @param array $files
  96. * @return \stdClass
  97. */
  98. public function post($endpointUrl, $postData = array(), $files = array()){
  99. return $this->restClient->post($endpointUrl, $postData, $files);
  100. }
  101. /**
  102. * @param string $endpointUrl
  103. * @param array $queryString
  104. * @return \stdClass
  105. */
  106. public function get($endpointUrl, $queryString = array()){
  107. return $this->restClient->get($endpointUrl, $queryString);
  108. }
  109. /**
  110. * @param string $endpointUrl
  111. * @return \stdClass
  112. */
  113. public function delete($endpointUrl){
  114. return $this->restClient->delete($endpointUrl);
  115. }
  116. /**
  117. * @param string $endpointUrl
  118. * @param array $putData
  119. * @return \stdClass
  120. */
  121. public function put($endpointUrl, $putData){
  122. return $this->restClient->put($endpointUrl, $putData);
  123. }
  124. /**
  125. * @param string $apiVersion
  126. *
  127. * @return Mailgun
  128. */
  129. public function setApiVersion($apiVersion)
  130. {
  131. $this->restClient->setApiVersion($apiVersion);
  132. return $this;
  133. }
  134. /**
  135. * @param boolean $sslEnabled
  136. *
  137. * @return Mailgun
  138. */
  139. public function setSslEnabled($sslEnabled)
  140. {
  141. $this->restClient->setSslEnabled($sslEnabled);
  142. return $this;
  143. }
  144. /**
  145. * @return MessageBuilder
  146. */
  147. public function MessageBuilder(){
  148. return new MessageBuilder();
  149. }
  150. /**
  151. * @return OptInHandler
  152. */
  153. public function OptInHandler(){
  154. return new OptInHandler();
  155. }
  156. /**
  157. * @param string $workingDomain
  158. * @param bool $autoSend
  159. * @return BatchMessage
  160. */
  161. public function BatchMessage($workingDomain, $autoSend = true){
  162. return new BatchMessage($this->restClient, $workingDomain, $autoSend);
  163. }
  164. }