Common
1 year ago
ErrorMappers
1 year ago
AmazonSES.php
1 year ago
MailPoet.php
2 years ago
MailerMethod.php
3 years ago
PHPMail.php
4 years ago
PHPMailerMethod.php
1 year ago
SMTP.php
3 weeks ago
SendGrid.php
1 year ago
index.php
3 years ago
SendGrid.php
118 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Mailer\Methods; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Mailer\Mailer; |
| 9 | use MailPoet\Mailer\Methods\Common\BlacklistCheck; |
| 10 | use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper; |
| 11 | use MailPoet\Util\Url; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class SendGrid implements MailerMethod { |
| 15 | public $url = 'https://api.sendgrid.com/api/mail.send.json'; |
| 16 | public $apiKey; |
| 17 | public $sender; |
| 18 | public $replyTo; |
| 19 | |
| 20 | /** @var SendGridMapper */ |
| 21 | private $errorMapper; |
| 22 | |
| 23 | /** @var Url */ |
| 24 | private $urlUtils; |
| 25 | |
| 26 | /** @var BlacklistCheck */ |
| 27 | private $blacklist; |
| 28 | |
| 29 | private $wp; |
| 30 | |
| 31 | public function __construct( |
| 32 | $apiKey, |
| 33 | $sender, |
| 34 | $replyTo, |
| 35 | SendGridMapper $errorMapper, |
| 36 | Url $urlUtils |
| 37 | ) { |
| 38 | $this->apiKey = $apiKey; |
| 39 | $this->sender = $sender; |
| 40 | $this->replyTo = $replyTo; |
| 41 | $this->errorMapper = $errorMapper; |
| 42 | $this->urlUtils = $urlUtils; |
| 43 | $this->wp = new WPFunctions(); |
| 44 | $this->blacklist = new BlacklistCheck(); |
| 45 | } |
| 46 | |
| 47 | public function send($newsletter, $subscriber, $extraParams = []): array { |
| 48 | if ($this->blacklist->isBlacklisted($subscriber)) { |
| 49 | $error = $this->errorMapper->getBlacklistError($subscriber); |
| 50 | return Mailer::formatMailerErrorResult($error); |
| 51 | } |
| 52 | $result = $this->wp->wpRemotePost( |
| 53 | $this->url, |
| 54 | $this->request($newsletter, $subscriber, $extraParams) |
| 55 | ); |
| 56 | if (is_wp_error($result)) { |
| 57 | $error = $this->errorMapper->getConnectionError($result->get_error_message()); |
| 58 | return Mailer::formatMailerErrorResult($error); |
| 59 | } |
| 60 | if ($this->wp->wpRemoteRetrieveResponseCode($result) !== 200) { |
| 61 | $response = json_decode($result['body'], true); |
| 62 | $error = $this->errorMapper->getErrorFromResponse($response, $subscriber); |
| 63 | return Mailer::formatMailerErrorResult($error); |
| 64 | } |
| 65 | return Mailer::formatMailerSendSuccessResult(); |
| 66 | } |
| 67 | |
| 68 | public function getBody($newsletter, $subscriber, $extraParams = []) { |
| 69 | $body = [ |
| 70 | 'to' => $subscriber, |
| 71 | 'from' => $this->sender['from_email'], |
| 72 | 'fromname' => $this->sender['from_name'], |
| 73 | 'replyto' => $this->replyTo['reply_to_email'], |
| 74 | 'subject' => $newsletter['subject'], |
| 75 | ]; |
| 76 | $headers = []; |
| 77 | |
| 78 | // unsubscribe header |
| 79 | $unsubscribeUrl = $extraParams['unsubscribe_url'] ?? null; |
| 80 | $oneClickUnsubscribeUrl = $extraParams['one_click_unsubscribe'] ?? null; |
| 81 | if ($unsubscribeUrl) { |
| 82 | $isHttps = $this->urlUtils->isUsingHttps($unsubscribeUrl); |
| 83 | $url = $isHttps && $oneClickUnsubscribeUrl ? $oneClickUnsubscribeUrl : $unsubscribeUrl; |
| 84 | if ($isHttps) { |
| 85 | $headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click'; |
| 86 | } |
| 87 | $headers['List-Unsubscribe'] = '<' . $url . '>'; |
| 88 | } |
| 89 | if ($headers) { |
| 90 | $body['headers'] = json_encode($headers); |
| 91 | } |
| 92 | if (!empty($newsletter['body']['html'])) { |
| 93 | $body['html'] = $newsletter['body']['html']; |
| 94 | } |
| 95 | if (!empty($newsletter['body']['text'])) { |
| 96 | $body['text'] = $newsletter['body']['text']; |
| 97 | } |
| 98 | return $body; |
| 99 | } |
| 100 | |
| 101 | public function auth() { |
| 102 | return 'Bearer ' . $this->apiKey; |
| 103 | } |
| 104 | |
| 105 | public function request($newsletter, $subscriber, $extraParams = []) { |
| 106 | $body = $this->getBody($newsletter, $subscriber, $extraParams); |
| 107 | return [ |
| 108 | 'timeout' => 10, |
| 109 | 'httpversion' => '1.1', |
| 110 | 'method' => 'POST', |
| 111 | 'headers' => [ |
| 112 | 'Authorization' => $this->auth(), |
| 113 | ], |
| 114 | 'body' => http_build_query($body, '', '&'), |
| 115 | ]; |
| 116 | } |
| 117 | } |
| 118 |