| | |
| | | */ |
| | | 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 |
| | | */ |
| | |
| | | { |
| | | $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'; |
| | |
| | | ]; |
| | | |
| | | // 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', |
| | |
| | | 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(); |
| | | } |
| | | } |
| | |
| | | $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); |
| | | |
| | |
| | | */ |
| | | 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; |
| | | } |
| | |
| | | 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() |
| | |
| | | |
| | | // Clean up whitespace |
| | | $text = preg_replace('/\s+/', ' ', $text); |
| | | $text = trim($text); |
| | | |
| | | return $text; |
| | | return trim($text); |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 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); |
| | | ]; |
| | | } |
| | | |
| | | /** |
| | |
| | | return $stats; |
| | | } |
| | | } catch (\Exception $e) { |
| | | $this->handleException($e, 'stats/outbound'); |
| | | $this->logError('stats/outbound', ['error' => $e->getMessage()]); |
| | | } |
| | | |
| | | return []; |
| | |
| | | 'offset' => $offset |
| | | ]); |
| | | } catch (\Exception $e) { |
| | | $this->handleException($e, 'bounces'); |
| | | $this->logError('[POSTMARK]', ['method' => 'getBounces', 'error' => $e->getMessage()]); |
| | | return []; |
| | | } |
| | | } |
| | |
| | | |
| | | 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; |
| | | } |
| | | } |