Jake Vanderwerf
2026-05-13 226b50642af0895948fbaa623a9b7180399a63b6
inc/integrations/PostMark.php
@@ -15,6 +15,13 @@
 */
class PostMark extends Integrations
{
   protected ?string $server_token = null;
   protected string $message_stream = 'outbound';
   protected string $from_email;
   protected string $from_name;
   protected bool $track_open;
   protected bool $track_links;
   protected ?string $lastMessageId = null;
   /**
    * Constructor
    */
@@ -22,9 +29,10 @@
   {
      $this->service_name = 'postmark';
      $this->title = 'PostMark';
      $this->description = 'Professional transactional email delivery service';
      $this->icon = 'envelope-simple';
      $this->cacheName = ($userID)? 'gmb_'.$userID : 'postmark';
      // PostMark API configuration
      $this->apiBase = 'https://api.postmarkapp.com';
      $this->apiVersion = 'v1';
@@ -48,12 +56,13 @@
      ];
      // Required credentials
      $this->requiredCredentials = [
      $this->fields = [
         'server_token' => [
            'label' => 'Server API Token',
            'type' => 'password',
            'type' => 'text',
            'subtype'=> 'password',
            'placeholder' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
            'description' => 'Your PostMark Server API Token'
            'hint' => 'Your PostMark Server API Token'
         ],
         'message_stream' => [
            'label' => 'Message Stream ID',
@@ -101,7 +110,7 @@
      parent::__construct($userID);
      // Hook into WordPress mail system if integration is healthy
      if ($this->is_healthy && $this->hasValidCredentials()) {
      if ($this->is_healthy && $this->isSetUp()) {
         $this->initializeMailHooks();
      }
   }
@@ -157,10 +166,12 @@
      $result = $this->sendEmail($payload);
      if ($result === true) {
         error_log('================================ Email sent! ================================');
         // Prevent default wp_mail from sending
         add_filter('pre_wp_mail', '__return_true');
         do_action('postmark_email_sent', $args, $payload);
      } else {
         error_log('=-======================[POSTMARK]Something went wrong... ================================');
         // Log failure but allow fallback to default mail
         do_action('postmark_email_failed', $args, $result);
@@ -249,9 +260,12 @@
    */
   protected function sendEmail(array $payload): bool|WP_Error
   {
      if (!$this->isSetUp()) {
         return false;
      }
      try {
         $response = $this->postRequest('email', $payload);
         error_log('================================ POSTMARK RESPONSE: ================================'.print_r($response, true));
         if (is_wp_error($response)) {
            return $response;
         }
@@ -271,7 +285,7 @@
         return true;
      } catch (\Exception $e) {
         $this->handleException($e, 'email');
         $this->logError('[POSTMARK]', ['method' => 'sendEmail', 'error' => $e->getMessage()]);
         return new WP_Error(
            'postmark_send_failed',
            $e->getMessage()
@@ -407,9 +421,7 @@
      // Clean up whitespace
      $text = preg_replace('/\s+/', ' ', $text);
      $text = trim($text);
      return $text;
      return trim($text);
   }
   /**
@@ -459,22 +471,15 @@
   /**
    * Override makeRequest to add PostMark headers
    */
   protected function makeRequest(
      string $method,
      string $endpoint,
      array $params = [],
      array $headers = []
   ): mixed {
   protected function getRequestHeaders(): array {
      $credentials = $this->getCredentials();
      // Add PostMark specific headers
      $headers = array_merge([
      return [
         'Accept' => 'application/json',
         'Content-Type' => 'application/json',
         'X-Postmark-Server-Token' => $credentials['server_token'] ?? ''
      ], $headers);
      return parent::makeRequest($method, $endpoint, $params, $headers);
      ];
   }
   /**
@@ -500,7 +505,7 @@
            return $stats;
         }
      } catch (\Exception $e) {
         $this->handleException($e, 'stats/outbound');
         $this->logError('stats/outbound', ['error' => $e->getMessage()]);
      }
      return [];
@@ -517,7 +522,7 @@
            'offset' => $offset
         ]);
      } catch (\Exception $e) {
         $this->handleException($e, 'bounces');
         $this->logError('[POSTMARK]', ['method' => 'getBounces', 'error' => $e->getMessage()]);
         return [];
      }
   }
@@ -634,11 +639,14 @@
   protected function initialize(): void
   {
      // TODO: Implement initialize() method.
   }
   protected function getRequestHeaders(): array
   {
      // TODO: Implement getRequestHeaders() method.
      if (empty($this->credentials)) {
         $this->loadCredentials();
      }
      $this->server_token = (array_key_exists('server_token', $this->credentials)) ? $this->credentials['server_token'] : null;
      $this->from_email = (array_key_exists('from_email', $this->credentials)) ? $this->credentials['from_email'] : get_bloginfo('admin_email');
      $this->from_name = (array_key_exists('from_name', $this->credentials)) ? $this->credentials['from_name'] : get_bloginfo('name');
      $this->message_stream = (array_key_exists('message_stream', $this->credentials)) ? $this->credentials['message_stream'] : 'outbound';
      $this->track_open = (array_key_exists('track_open', $this->credentials)) ? $this->credentials['track_open'] : false;
      $this->track_links = (array_key_exists('track_links', $this->credentials)) ? $this->credentials['track_links'] : false;
   }
}