← Back
Editing: Handler.php
<?php namespace FluentMail\App\Services\Mailer\Providers\Postmark; use FluentMail\Includes\Support\Arr; use FluentMail\App\Services\Mailer\BaseHandler; class Handler extends BaseHandler { use ValidatorTrait; protected $emailSentCode = 200; protected $url = 'https://api.postmarkapp.com/email'; public function send() { if ($this->preSend() && $this->phpMailer->preSend()) { return $this->postSend(); } return $this->handleResponse(new \WP_Error(422, __('Something went wrong.', 'fluent-smtp'), [])); } public function postSend() { $body = [ 'From' => $this->getFormattedFrom(), 'To' => $this->getTo(), 'Subject' => $this->getSubject(), 'MessageStream' => $this->getSetting('message_stream', 'outbound') ]; if ($replyTo = $this->getReplyTo()) { $body['ReplyTo'] = $replyTo; } if ($bcc = $this->getBlindCarbonCopy()) { $body['Bcc'] = $bcc; } if ($cc = $this->getCarbonCopy()) { $body['Cc'] = $cc; } $contentType = $this->getHeader('content-type'); if ($contentType == 'text/html') { $body['HtmlBody'] = $this->getParam('message'); if ($this->getSetting('track_opens') == 'yes') { $body['TrackOpens'] = true; } if ($this->getSetting('track_links') == 'yes') { $body['TrackLinks'] = 'HtmlOnly'; } } else if ($contentType == 'multipart/alternative') { $body['HtmlBody'] = $this->getParam('message'); $body['TextBody'] = $this->phpMailer->AltBody; } else { $body['TextBody'] = $this->getParam('message'); } if (!empty($this->getParam('attachments'))) { $body['Attachments'] = $this->getAttachments(); } // Add any custom headers $customHeaders = $this->phpMailer->getCustomHeaders(); if (!empty($customHeaders)) { foreach ($customHeaders as $header) { $body['Headers'][] = [ 'Name' => $header[0], 'Value' => $header[1] ]; } } // Handle apostrophes in email address From names by escaping them for the Postmark API. $from_regex = "/(\"From\": \"[a-zA-Z\\d]+)*[\\\\]{2,}'/"; $args = array( 'headers' => $this->getRequestHeaders(), 'body' => preg_replace($from_regex, "'", wp_json_encode($body), 1), ); $response = wp_remote_post($this->url, $args); if (is_wp_error($response)) { $returnResponse = new \WP_Error($response->get_error_code(), $response->get_error_message(), $response->get_error_messages()); } else { $responseBody = wp_remote_retrieve_body($response); $responseCode = wp_remote_retrieve_response_code($response); $isOKCode = $responseCode == $this->emailSentCode; $responseBody = \json_decode($responseBody, true); if ($isOKCode) { $returnResponse = [ 'id' => Arr::get($responseBody, 'MessageID'), 'message' => Arr::get($responseBody, 'Message') ]; } else { $returnResponse = new \WP_Error($responseCode, Arr::get($responseBody, 'Message', __('Unknown Error', 'fluent-smtp')), $responseBody); } } $this->response = $returnResponse; return $this->handleResponse($this->response); } public function setSettings($settings) { if ($settings['key_store'] == 'wp_config') { $settings['api_key'] = defined('FLUENTMAIL_POSTMARK_API_KEY') ? FLUENTMAIL_POSTMARK_API_KEY : ''; } $this->settings = $settings; return $this; } protected function getReplyTo() { if ($replyTo = $this->getParam('headers.reply-to')) { $replyTo = reset($replyTo); return isset($replyTo['name']) ? self::formatQuotedAddress($replyTo['email'], $replyTo['name']) : $replyTo['email']; } } protected function getTo() { return $this->getRecipients($this->getParam('to')); } protected function getCarbonCopy() { return $this->getRecipients($this->getParam('headers.cc')); } protected function getBlindCarbonCopy() { return $this->getRecipients($this->getParam('headers.bcc')); } protected function getRecipients($recipients) { $array = array_map(function ($recipient) { return isset($recipient['name']) ? self::formatAddress($recipient['email'], $recipient['name']) : $recipient['email']; }, $recipients); return implode(', ', $array); } protected function getAttachments() { $data = []; foreach ($this->getParam('attachments') as $attachment) { try { $file = self::attachmentContent($attachment); } catch (\Exception $e) { $this->logAttachmentFailure('Postmark', $e); continue; } $row = [ 'Name' => self::attachmentName($attachment), 'Content' => base64_encode($file), 'ContentType' => self::attachmentType($attachment) ]; // Postmark embeds an attachment whose ContentID matches the HTML's // cid: reference; its documented form carries the "cid:" prefix. $contentId = self::inlineContentId($attachment); if ($contentId !== '') { $row['ContentID'] = 'cid:' . $contentId; } $data[] = $row; } return $data; } protected function getRequestHeaders() { return [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'X-Postmark-Server-Token' => $this->getSetting('api_key'), ]; } }
Save File
Cancel