Mailgun.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?PHP
  2. namespace Mailgun;
  3. require_once 'Constants/Constants.php';
  4. use Mailgun\Messages\Messages;
  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 for usage instructions.
  13. */
  14. class Mailgun{
  15. protected $workingDomain;
  16. protected $restClient;
  17. protected $apiKey;
  18. public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v2", $ssl = true){
  19. $this->apiKey = $apiKey;
  20. $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl);
  21. }
  22. public function sendMessage($workingDomain, $postData, $postFiles = array()){
  23. /*
  24. * This function allows the sending of a fully formed message OR a custom
  25. * MIME string. If sending MIME, the string must be passed in to the 3rd
  26. * position of the function call.
  27. */
  28. if(is_array($postFiles)){
  29. return $this->post("$workingDomain/messages", $postData, $postFiles);
  30. }
  31. else if(is_string($postFiles)){
  32. $tempFile = tempnam(sys_get_temp_dir(), "MG_TMP_MIME");
  33. $fileHandle = fopen($tempFile, "w");
  34. fwrite($fileHandle, $postFiles);
  35. $result = $this->post("$workingDomain/messages.mime", $postData, array("message" => $tempFile));
  36. fclose($fileHandle);
  37. unlink($tempFile);
  38. return $result;
  39. }
  40. else{
  41. throw new Exceptions\MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
  42. }
  43. }
  44. public function verifyWebhookSignature($postData = NULL) {
  45. /*
  46. * This function checks the signature in a POST request to see if it is
  47. * authentic.
  48. *
  49. * Pass an array of parameters. If you pass nothing, $_POST will be
  50. * used instead.
  51. *
  52. * If this function returns FALSE, you must not process the request.
  53. * You should reject the request with status code 403 Forbidden.
  54. */
  55. if(is_null($postData)) {
  56. $postData = $_POST;
  57. }
  58. $hmac = hash_hmac('sha256', "{$postData["timestamp"]}{$postData["token"]}", $this->apiKey);
  59. $sig = $postData['signature'];
  60. if(function_exists('hash_equals')) {
  61. // hash_equals is constant time, but will not be introduced until PHP 5.6
  62. return hash_equals($hmac, $sig);
  63. }
  64. else {
  65. return ($hmac == $sig);
  66. }
  67. }
  68. public function post($endpointUrl, $postData = array(), $files = array()){
  69. return $this->restClient->post($endpointUrl, $postData, $files);
  70. }
  71. public function get($endpointUrl, $queryString = array()){
  72. return $this->restClient->get($endpointUrl, $queryString);
  73. }
  74. public function delete($endpointUrl){
  75. return $this->restClient->delete($endpointUrl);
  76. }
  77. public function put($endpointUrl, $putData){
  78. return $this->restClient->put($endpointUrl, $putData);
  79. }
  80. public function MessageBuilder(){
  81. return new MessageBuilder();
  82. }
  83. public function OptInHandler(){
  84. return new OptInHandler();
  85. }
  86. public function BatchMessage($workingDomain, $autoSend = true){
  87. return new BatchMessage($this->restClient, $workingDomain, $autoSend);
  88. }
  89. }