| @@ -1,169 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -use AcyMailing\Core\AcymPlugin; | |
| 4 | - | |
| 5 | -class plgAcymPostmark extends AcymPlugin | |
| 6 | -{ | |
| 7 | - const SENDING_METHOD_ID = 'postmark'; | |
| 8 | - const SENDING_METHOD_NAME = 'Postmark'; | |
| 9 | - const SENDING_METHOD_API_URL = 'https://api.postmarkapp.com/'; | |
| 10 | - | |
| 11 | - public function __construct() | |
| 12 | - { | |
| 13 | - parent::__construct(); | |
| 14 | - $this->pluginDescription->name = self::SENDING_METHOD_NAME; | |
| 15 | - } | |
| 16 | - | |
| 17 | - public function onAcymGetSendingMethods(&$data, $isMailer = false) | |
| 18 | - { | |
| 19 | - $data['sendingMethods'][self::SENDING_METHOD_ID] = [ | |
| 20 | - 'name' => $this->pluginDescription->name, | |
| 21 | - 'image' => ACYM_IMAGES.'mailers/postmark.svg', | |
| 22 | - 'image_class' => '', | |
| 23 | - ]; | |
| 24 | - } | |
| 25 | - | |
| 26 | - public function onAcymGetSendingMethodsHtmlSetting(&$data) | |
| 27 | - { | |
| 28 | - $config = empty($data['tab']) ? $this->config : $data['tab']->config; | |
| 29 | - $defaultApiKey = $config->get(self::SENDING_METHOD_ID.'_api_key'); | |
| 30 | - ob_start(); | |
| 31 | - ?> | |
| 32 | - <div class="send_settings cell grid-x acym_vcenter" id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings"> | |
| 33 | - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 34 | - <label class="cell shrink margin-right-1" for="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-key"> | |
| 35 | - <?php echo esc_html( | |
| 36 | - acym_translationSprintf( | |
| 37 | - 'ACYM_SENDING_METHOD_API_KEY', | |
| 38 | - self::SENDING_METHOD_NAME | |
| 39 | - ) | |
| 40 | - ); ?> | |
| 41 | - </label> | |
| 42 | - <?php $this->getLinks('https://account.postmarkapp.com/sign_up', 'https://postmarkapp.com/pricing'); ?> | |
| 43 | - <input type="text" | |
| 44 | - id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-key" | |
| 45 | - value="<?php echo esc_attr(empty($defaultApiKey) ? $this->config->get(self::SENDING_METHOD_ID.'_api_key') : $defaultApiKey); ?>" | |
| 46 | - name="config[<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_api_key]" | |
| 47 | - class="cell margin-next-1 acym__configuration__mail__settings__text"> | |
| 48 | - <?php $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?> | |
| 49 | - </div> | |
| 50 | - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 51 | - <label class="cell medium-6"> | |
| 52 | - <?php echo esc_html(acym_translation('ACYM_POSTMARK_STREAM_ID')); | |
| 53 | - acym_info(['textShownInTooltip' => 'ACYM_STREAM_ID_INFO']); ?> | |
| 54 | - <input type="text" | |
| 55 | - class="cell auto" | |
| 56 | - name="config[postmark_stream_id]" | |
| 57 | - value="<?php echo esc_attr(empty($this->config->get('postmark_stream_id')) ? '' : $this->config->get('postmark_stream_id')); ?>"> | |
| 58 | - </label> | |
| 59 | - </div> | |
| 60 | - </div> | |
| 61 | - <?php | |
| 62 | - $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean(); | |
| 63 | - } | |
| 64 | - | |
| 65 | - public function getHeadersSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = []) | |
| 66 | - { | |
| 67 | - if (empty($credentials)) $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams); | |
| 68 | - | |
| 69 | - return [ | |
| 70 | - 'X-Postmark-Server-Token:'.$credentials[self::SENDING_METHOD_ID.'_api_key'], | |
| 71 | - 'Accept: application/json', | |
| 72 | - 'Content-type: application/json', | |
| 73 | - ]; | |
| 74 | - } | |
| 75 | - | |
| 76 | - /** | |
| 77 | - * @param array $credentials | |
| 78 | - * @param string $sendingMethod | |
| 79 | - * @param array $sendingMethodListParams this parameter is only used for the plugin sending method list | |
| 80 | - * | |
| 81 | - * @return void | |
| 82 | - */ | |
| 83 | - public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = []) | |
| 84 | - { | |
| 85 | - if ($sendingMethod != self::SENDING_METHOD_ID) return; | |
| 86 | - | |
| 87 | - $key = self::SENDING_METHOD_ID.'_api_key'; | |
| 88 | - | |
| 89 | - $credentials = [ | |
| 90 | - $key => $sendingMethodListParams[$key] ?? $this->config->get($key, ''), | |
| 91 | - ]; | |
| 92 | - } | |
| 93 | - | |
| 94 | - public function onAcymTestCredentialSendingMethod($sendingMethod, $credentials) | |
| 95 | - { | |
| 96 | - if ($sendingMethod == self::SENDING_METHOD_ID) { | |
| 97 | - $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID, $credentials); | |
| 98 | - $response = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'server', [], $headers); | |
| 99 | - if (!empty($response['error_curl'])) { | |
| 100 | - acym_sendAjaxResponse(acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']), [], false); | |
| 101 | - } elseif (!empty($response['ErrorCode']) && in_array($response['ErrorCode'], ['Unauthorized', '10'])) { | |
| 102 | - $message = acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY'); | |
| 103 | - acym_sendAjaxResponse($message, [], false); | |
| 104 | - } elseif (!empty($response['ErrorCode'])) { | |
| 105 | - $message = acym_translationSprintf('ACYM_API_RETURN_THIS_ERROR', $response['ErrorCode'].': '.$response['Message']); | |
| 106 | - acym_sendAjaxResponse($message, [], false); | |
| 107 | - } else { | |
| 108 | - acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT')); | |
| 109 | - } | |
| 110 | - } | |
| 111 | - } | |
| 112 | - | |
| 113 | - public function onAcymSendEmail(&$response, $mailerHelper, $to, $from, $reply_to, $bcc = [], $attachments = [], $sendingMethodListParams = []) | |
| 114 | - { | |
| 115 | - //https://postmarkapp.com/developer/api/email-api | |
| 116 | - if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return; | |
| 117 | - | |
| 118 | - $data = [ | |
| 119 | - 'From' => $from['email'], | |
| 120 | - 'ReplyTo' => $reply_to['email'], | |
| 121 | - 'To' => $to['email'], | |
| 122 | - 'Subject' => $mailerHelper->Subject, | |
| 123 | - 'HtmlBody' => $mailerHelper->Body, | |
| 124 | - 'MessageStream' => 'outbound', | |
| 125 | - ]; | |
| 126 | - if (!empty($bcc)) $data['Bcc'] = $bcc[0][0]; | |
| 127 | - | |
| 128 | - if (!empty($attachments)) { | |
| 129 | - $attachFormated = []; | |
| 130 | - foreach ($attachments as $oneAttach) { | |
| 131 | - $attachFormated[] = [ | |
| 132 | - 'Name' => $oneAttach[1], | |
| 133 | - 'Content' => $oneAttach['contentEncoded'], | |
| 134 | - 'ContentType' => $oneAttach[4], | |
| 135 | - ]; | |
| 136 | - } | |
| 137 | - $data['Attachments'] = $attachFormated; | |
| 138 | - } | |
| 139 | - | |
| 140 | - // Handle the Stream ID | |
| 141 | - if (!empty($mailerHelper->mailId)) { | |
| 142 | - $mailId = $mailerHelper->mailId; | |
| 143 | - if (acym_isMultilingual()) { | |
| 144 | - $parentId = acym_loadResult('SELECT parent_id FROM `#__acym_mail` WHERE id = '.intval($mailId)); | |
| 145 | - if (!empty($parentId)) $mailId = $parentId; | |
| 146 | - } | |
| 147 | - | |
| 148 | - $sendParams = acym_loadResult('SELECT sending_params FROM `#__acym_campaign` WHERE mail_id = '.intval($mailId)); | |
| 149 | - if (!empty($sendParams)) { | |
| 150 | - $sendParams = json_decode($sendParams, true); | |
| 151 | - if (!empty($sendParams['message_stream_id'])) { | |
| 152 | - $data['MessageStream'] = $sendParams['message_stream_id']; | |
| 153 | - } else { | |
| 154 | - $data['MessageStream'] = $this->config->get('postmark_stream_id'); | |
| 155 | - } | |
| 156 | - } | |
| 157 | - } | |
| 158 | - | |
| 159 | - $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams); | |
| 160 | - $responseMailer = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'email', $data, $headers, 'POST'); | |
| 161 | - | |
| 162 | - if (!empty($responseMailer['ErrorCode'])) { | |
| 163 | - $response['error'] = true; | |
| 164 | - $response['message'] = $responseMailer['Message']; | |
| 165 | - } else { | |
| 166 | - $response['error'] = false; | |
| 167 | - } | |
| 168 | - } | |
| 169 | -} | |