Nav apraksta

SendGrid.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace MailPoet\Mailer\Methods;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Mailer\Mailer;
  5. use MailPoet\Mailer\Methods\Common\BlacklistCheck;
  6. use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
  7. use MailPoet\WP\Functions as WPFunctions;
  8. class SendGrid {
  9. public $url = 'https://api.sendgrid.com/api/mail.send.json';
  10. public $apiKey;
  11. public $sender;
  12. public $replyTo;
  13. /** @var SendGridMapper */
  14. private $errorMapper;
  15. /** @var BlacklistCheck */
  16. private $blacklist;
  17. private $wp;
  18. public function __construct(
  19. $apiKey,
  20. $sender,
  21. $replyTo,
  22. SendGridMapper $errorMapper
  23. ) {
  24. $this->apiKey = $apiKey;
  25. $this->sender = $sender;
  26. $this->replyTo = $replyTo;
  27. $this->errorMapper = $errorMapper;
  28. $this->wp = new WPFunctions();
  29. $this->blacklist = new BlacklistCheck();
  30. }
  31. public function send($newsletter, $subscriber, $extraParams = []) {
  32. if ($this->blacklist->isBlacklisted($subscriber)) {
  33. $error = $this->errorMapper->getBlacklistError($subscriber);
  34. return Mailer::formatMailerErrorResult($error);
  35. }
  36. $result = $this->wp->wpRemotePost(
  37. $this->url,
  38. $this->request($newsletter, $subscriber, $extraParams)
  39. );
  40. if (is_wp_error($result)) {
  41. $error = $this->errorMapper->getConnectionError($result->get_error_message());
  42. return Mailer::formatMailerErrorResult($error);
  43. }
  44. if ($this->wp->wpRemoteRetrieveResponseCode($result) !== 200) {
  45. $response = json_decode($result['body'], true);
  46. $error = $this->errorMapper->getErrorFromResponse($response, $subscriber);
  47. return Mailer::formatMailerErrorResult($error);
  48. }
  49. return Mailer::formatMailerSendSuccessResult();
  50. }
  51. public function getBody($newsletter, $subscriber, $extraParams = []) {
  52. $body = [
  53. 'to' => $subscriber,
  54. 'from' => $this->sender['from_email'],
  55. 'fromname' => $this->sender['from_name'],
  56. 'replyto' => $this->replyTo['reply_to_email'],
  57. 'subject' => $newsletter['subject'],
  58. ];
  59. $headers = [];
  60. if (!empty($extraParams['unsubscribe_url'])) {
  61. $headers['List-Unsubscribe'] = '<' . $extraParams['unsubscribe_url'] . '>';
  62. }
  63. if ($headers) {
  64. $body['headers'] = json_encode($headers);
  65. }
  66. if (!empty($newsletter['body']['html'])) {
  67. $body['html'] = $newsletter['body']['html'];
  68. }
  69. if (!empty($newsletter['body']['text'])) {
  70. $body['text'] = $newsletter['body']['text'];
  71. }
  72. return $body;
  73. }
  74. public function auth() {
  75. return 'Bearer ' . $this->apiKey;
  76. }
  77. public function request($newsletter, $subscriber, $extraParams = []) {
  78. $body = $this->getBody($newsletter, $subscriber, $extraParams);
  79. return [
  80. 'timeout' => 10,
  81. 'httpversion' => '1.1',
  82. 'method' => 'POST',
  83. 'headers' => [
  84. 'Authorization' => $this->auth(),
  85. ],
  86. 'body' => http_build_query($body, '', '&'),
  87. ];
  88. }
  89. }