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