SendGrid.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. class SendGrid
  3. {
  4. const VERSION = '3.2.0';
  5. protected
  6. $namespace = 'SendGrid',
  7. $headers = array('Content-Type' => 'application/json'),
  8. $client,
  9. $options;
  10. public
  11. $apiUser,
  12. $apiKey,
  13. $url,
  14. $endpoint,
  15. $version = self::VERSION;
  16. public function __construct($apiUserOrKey, $apiKeyOrOptions = null, $options = array())
  17. {
  18. // Check if given a username + password or api key
  19. if (is_string($apiKeyOrOptions)) {
  20. // Username and password
  21. $this->apiUser = $apiUserOrKey;
  22. $this->apiKey = $apiKeyOrOptions;
  23. $this->options = $options;
  24. } elseif (is_array($apiKeyOrOptions) || $apiKeyOrOptions === null) {
  25. // API key
  26. $this->apiKey = $apiUserOrKey;
  27. $this->apiUser = null;
  28. // With options
  29. if (is_array($apiKeyOrOptions)) {
  30. $this->options = $apiKeyOrOptions;
  31. }
  32. } else {
  33. // Won't be thrown?
  34. throw new InvalidArgumentException('Need a username + password or api key!');
  35. }
  36. $this->options['turn_off_ssl_verification'] = (isset($this->options['turn_off_ssl_verification']) && $this->options['turn_off_ssl_verification'] == true);
  37. if (!isset($this->options['raise_exceptions'])) {
  38. $this->options['raise_exceptions'] = true;
  39. }
  40. $protocol = isset($this->options['protocol']) ? $this->options['protocol'] : 'https';
  41. $host = isset($this->options['host']) ? $this->options['host'] : 'api.sendgrid.com';
  42. $port = isset($this->options['port']) ? $this->options['port'] : '';
  43. $this->url = isset($this->options['url']) ? $this->options['url'] : $protocol . '://' . $host . ($port ? ':' . $port : '');
  44. $this->endpoint = isset($this->options['endpoint']) ? $this->options['endpoint'] : '/api/mail.send.json';
  45. $this->client = $this->prepareHttpClient();
  46. }
  47. /**
  48. * Prepares the HTTP client
  49. *
  50. * @return \Guzzle\Http\Client
  51. */
  52. private function prepareHttpClient()
  53. {
  54. $guzzleOption = array(
  55. 'request.options' => array(
  56. 'verify' => !$this->options['turn_off_ssl_verification'],
  57. 'exceptions' => (isset($this->options['enable_guzzle_exceptions']) && $this->options['enable_guzzle_exceptions'] == true)
  58. )
  59. );
  60. // Using api key
  61. if ($this->apiUser === null) {
  62. $guzzleOption['request.options']['headers'] = array('Authorization' => 'Bearer ' . $this->apiKey);
  63. }
  64. // Using http proxy
  65. if (isset($this->options['proxy'])) {
  66. $guzzleOption['request.options']['proxy'] = $this->options['proxy'];
  67. }
  68. $client = new \Guzzle\Http\Client($this->url, $guzzleOption);
  69. $client->setUserAgent('sendgrid/' . $this->version . ';php');
  70. return $client;
  71. }
  72. /**
  73. * @return array The protected options array
  74. */
  75. public function getOptions()
  76. {
  77. return $this->options;
  78. }
  79. /**
  80. * Makes a post request to SendGrid to send an email
  81. *
  82. * @param SendGrid\Email $email Email object built
  83. *
  84. * @throws SendGrid\Exception if the response code is not 200
  85. * @return stdClass SendGrid response object
  86. */
  87. public function send(SendGrid\Email $email)
  88. {
  89. $form = $email->toWebFormat();
  90. // Using username password
  91. if ($this->apiUser !== null) {
  92. $form['api_user'] = $this->apiUser;
  93. $form['api_key'] = $this->apiKey;
  94. }
  95. $response = $this->postRequest($this->endpoint, $form);
  96. if ($response->code != 200 && $this->options['raise_exceptions']) {
  97. throw new SendGrid\Exception($response->raw_body, $response->code);
  98. }
  99. return $response;
  100. }
  101. /**
  102. * Makes the actual HTTP request to SendGrid
  103. *
  104. * @param $endpoint string endpoint to post to
  105. * @param $form array web ready version of SendGrid\Email
  106. *
  107. * @return SendGrid\Response
  108. */
  109. public function postRequest($endpoint, $form)
  110. {
  111. $req = $this->client->post($endpoint, null, $form);
  112. $res = $req->send();
  113. $response = new SendGrid\Response($res->getStatusCode(), $res->getHeaders(), $res->getBody(true), $res->json());
  114. return $response;
  115. }
  116. public static function register_autoloader()
  117. {
  118. spl_autoload_register(array('SendGrid', 'autoloader'));
  119. }
  120. public static function autoloader($class)
  121. {
  122. // Check that the class starts with 'SendGrid'
  123. if ($class == 'SendGrid' || stripos($class, 'SendGrid\\') === 0) {
  124. $file = str_replace('\\', '/', $class);
  125. if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
  126. require_once(dirname(__FILE__) . '/' . $file . '.php');
  127. }
  128. }
  129. }
  130. }