api = new API($api_key); $this->sender = $sender; $this->reply_to = $reply_to; $this->services_checker = new ServicesChecker(false); } function send($newsletter, $subscriber, $extra_params = array()) { if($this->services_checker->checkMailPoetAPIKeyValid() === false) { $response = __('MailPoet API key is invalid!', 'mailpoet'); return Mailer::formatMailerSendErrorResult($response); } $message_body = $this->getBody($newsletter, $subscriber); $result = $this->api->sendMessages($message_body); switch($result['status']) { case API::SENDING_STATUS_CONNECTION_ERROR: return Mailer::formatMailerConnectionErrorResult($result['message']); case API::SENDING_STATUS_SEND_ERROR: if(!empty($result['code']) && $result['code'] === API::RESPONSE_CODE_KEY_INVALID) { Bridge::invalidateKey(); } $result['message'] .= sprintf(' %s: %s', __('Unprocessed subscriber', 'mailpoet'), htmlspecialchars($subscriber)); return Mailer::formatMailerSendErrorResult($result['message']); case API::SENDING_STATUS_OK: default: return Mailer::formatMailerSendSuccessResult(); } } function processSubscriber($subscriber) { preg_match('!(?P.*?)\s<(?P.*?)>!', $subscriber, $subscriber_data); if(!isset($subscriber_data['email'])) { $subscriber_data = array( 'email' => $subscriber, ); } return array( 'email' => $subscriber_data['email'], 'name' => (isset($subscriber_data['name'])) ? $subscriber_data['name'] : '' ); } function getBody($newsletter, $subscriber) { $composeBody = function($newsletter, $subscriber) { $body = array( 'to' => (array( 'address' => $subscriber['email'], 'name' => $subscriber['name'] )), 'from' => (array( 'address' => $this->sender['from_email'], 'name' => $this->sender['from_name'] )), 'reply_to' => (array( 'address' => $this->reply_to['reply_to_email'], 'name' => $this->reply_to['reply_to_name'] )), 'subject' => $newsletter['subject'] ); if(!empty($newsletter['body']['html'])) { $body['html'] = $newsletter['body']['html']; } if(!empty($newsletter['body']['text'])) { $body['text'] = $newsletter['body']['text']; } return $body; }; if(is_array($newsletter) && is_array($subscriber)) { $body = array(); for($record = 0; $record < count($newsletter); $record++) { $body[] = $composeBody( $newsletter[$record], $this->processSubscriber($subscriber[$record]) ); } } else { $body[] = $composeBody($newsletter, $this->processSubscriber($subscriber)); } return $body; } }