Files
piratepoet/lib/Mailer/Methods/SendGrid.php
Rostislav Wolny f5c9d0f7db Remove unnecessary test_email extra parameter for mailer->send
It was used only to prevent appending unprocessed subscribers into error message.
Since the message is now composed by on demand by MailerError the parameter is not needed any more.

[MAILPOET-1154]
2018-09-13 11:12:38 +02:00

85 lines
2.5 KiB
PHP

<?php
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
use MailPoet\WP\Functions as WPFunctions;
if(!defined('ABSPATH')) exit;
class SendGrid {
public $url = 'https://api.sendgrid.com/api/mail.send.json';
public $api_key;
public $sender;
public $reply_to;
/** @var SendGridMapper */
private $error_mapper;
function __construct($api_key, $sender, $reply_to, SendGridMapper $error_mapper) {
$this->api_key = $api_key;
$this->sender = $sender;
$this->reply_to = $reply_to;
$this->error_mapper = $error_mapper;
}
function send($newsletter, $subscriber, $extra_params = array()) {
$result = WPFunctions::wpRemotePost(
$this->url,
$this->request($newsletter, $subscriber, $extra_params)
);
if(is_wp_error($result)) {
$error = $this->error_mapper->getConnectionError($result->get_error_message());
return Mailer::formatMailerErrorResult($error);
}
if(WPFunctions::wpRemoteRetrieveResponseCode($result) !== 200) {
$response = json_decode($result['body'], true);
$error = $this->error_mapper->getErrorFromResponse($response, $subscriber);
return Mailer::formatMailerErrorResult($error);
}
return Mailer::formatMailerSendSuccessResult();
}
function getBody($newsletter, $subscriber, $extra_params = array()) {
$body = array(
'to' => $subscriber,
'from' => $this->sender['from_email'],
'fromname' => $this->sender['from_name'],
'replyto' => $this->reply_to['reply_to_email'],
'subject' => $newsletter['subject']
);
$headers = array();
if(!empty($extra_params['unsubscribe_url'])) {
$headers['List-Unsubscribe'] = '<' . $extra_params['unsubscribe_url'] . '>';
}
if($headers) {
$body['headers'] = json_encode($headers);
}
if(!empty($newsletter['body']['html'])) {
$body['html'] = $newsletter['body']['html'];
}
if(!empty($newsletter['body']['text'])) {
$body['text'] = $newsletter['body']['text'];
}
return $body;
}
function auth() {
return 'Bearer ' . $this->api_key;
}
function request($newsletter, $subscriber, $extra_params = array()) {
$body = $this->getBody($newsletter, $subscriber, $extra_params);
return array(
'timeout' => 10,
'httpversion' => '1.1',
'method' => 'POST',
'headers' => array(
'Authorization' => $this->auth()
),
'body' => http_build_query($body, null, '&')
);
}
}