PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 10.3.0 10.4.0 All 60 releases
acymailing / back / dynamics / mailgun / plugin.php

plugin.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress trunk, at back/dynamics/mailgun/plugin.php

253 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use AcyMailing\Core\AcymPlugin;
4
5 class plgAcymMailgun extends AcymPlugin
6 {
7 const SENDING_METHOD_ID = 'mailgun';
8 const SENDING_METHOD_NAME = 'Mailgun';
9 const SENDING_METHOD_API_URL_US = 'https://api.mailgun.net/v3/';
10 const SENDING_METHOD_API_URL_EU = 'https://api.eu.mailgun.net/v3/';
11
12 public $sendingMethodApiUrl;
13
14 public function __construct()
15 {
16 parent::__construct();
17 $this->pluginDescription->name = self::SENDING_METHOD_NAME;
18 }
19
20 public function onAcymGetSendingMethods(&$data, $isMailer = false)
21 {
22 $data['sendingMethods'][self::SENDING_METHOD_ID] = [
23 'name' => $this->pluginDescription->name,
24 'image' => ACYM_IMAGES.'mailers/mailgun.svg',
25 'image_class' => 'acym__selection__card__image__mailgun',
26 ];
27 }
28
29 public function onAcymGetSendingMethodsHtmlSetting(&$data)
30 {
31 $regions = [
32 'us' => acym_translation('ACYM_US'),
33 'eu' => acym_translation('ACYM_EU'),
34 ];
35 $config = empty($data['tab']) ? $this->config : $data['tab']->config;
36 $defaultDomain = $config->get(self::SENDING_METHOD_ID.'_api_domain');
37 $defaultApiKey = $config->get(self::SENDING_METHOD_ID.'_api_key');
38 ob_start();
39 ?>
40 <div class="send_settings cell grid-x acym_vcenter" id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings">
41 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
42 <label class="cell large-3 medium-4 margin-right-1">
43 <?php
44 echo esc_html(acym_translationSprintf('ACYM_SENDING_METHOD_API_REGION', self::SENDING_METHOD_NAME));
45 acym_info(
46 [
47 'textShownInTooltip' => acym_translationSprintf('ACYM_SENDING_METHOD_API_REGION_DESC', self::SENDING_METHOD_NAME),
48 ]
49 );
50 ?>
51 </label>
52 <?php
53 acym_radio(
54 $regions,
55 'config['.self::SENDING_METHOD_ID.'_api_region]',
56 $this->config->get(self::SENDING_METHOD_ID.'_api_region', 'us')
57 );
58 ?>
59 </div>
60 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
61 <label class="cell" for="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-domain">
62 <?php echo esc_html(
63 acym_translationSprintf(
64 'ACYM_SENDING_METHOD_API_DOMAIN',
65 self::SENDING_METHOD_NAME
66 )
67 ); ?>
68 </label>
69 <input type="text"
70 id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-domain"
71 value="<?php echo esc_attr(empty($defaultDomain) ? $this->config->get(self::SENDING_METHOD_ID.'_api_domain') : $defaultDomain); ?>"
72 name="config[<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_api_domain]"
73 class="cell acym__configuration__mail__settings__text">
74 </div>
75 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
76 <label class="cell shrink margin-right-1" for="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-key">
77 <?php echo esc_html(
78 acym_translationSprintf(
79 'ACYM_SENDING_METHOD_API_KEY',
80 self::SENDING_METHOD_NAME
81 )
82 ); ?>
83 </label>
84 <?php $this->getLinks('https://signup.mailgun.com/new/signup', 'https://www.mailgun.com/pricing/'); ?>
85 <input type="text"
86 id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-key"
87 value="<?php echo esc_attr(empty($defaultApiKey) ? $this->config->get(self::SENDING_METHOD_ID.'_api_key') : $defaultApiKey); ?>"
88 name="config[<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_api_key]"
89 class="cell acym__configuration__mail__settings__text">
90 <?php $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?>
91 <?php $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp'); ?>
92 </div>
93 </div>
94 <?php
95 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
96 }
97
98 public function onAcymTestCredentialSendingMethod($sendingMethod, $credentials)
99 {
100 if ($sendingMethod !== self::SENDING_METHOD_ID) return;
101
102 $this->setSendingMethodApiUrl($credentials);
103 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
104 $authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, $credentials);
105 $data = [
106 'from' => $this->config->get('from_email'),
107 'to' => acym_currentUserEmail(),
108 'subject' => 'Test email',
109 'html' => 'Test email body',
110 'o:testmode' => true,
111 ];
112
113 $response = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true);
114
115 // If call didn't work, the response is always null
116 if (empty($response)) {
117 acym_sendAjaxResponse(acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY'), [], false);
118 } else {
119 acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
120 }
121 }
122
123 public function onAcymSendEmail(&$response, $mailerHelper, $to, $from, $replyTo, $bcc = [], $attachments = [], $sendingMethodListParams = [])
124 {
125 //https://documentation.mailgun.com/en/latest/user_manual.html#sending-via-api
126 if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return;
127
128 $this->setSendingMethodApiUrl();
129 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
130 $authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams);
131 $fromData = $from['email'];
132 $replyToData = $replyTo['email'] ?? '';
133 $toData = $to['email'];
134 if ($this->config->get('add_names', 1) == 1) {
135 if (!empty($from['name'])) $fromData = $from['name'].' <'.$fromData.'>';
136 if (!empty($replyTo['name']) && !empty($replyToData)) $replyToData = $replyTo['name'].' <'.$replyToData.'>';
137 if (!empty($to['name'])) $toData = $to['name'].' <'.$toData.'>';
138 }
139 $data = [
140 'from' => $fromData,
141 'to' => $toData,
142 'subject' => $mailerHelper->Subject,
143 'html' => $mailerHelper->Body,
144 ];
145
146 if (!empty($replyToData)) {
147 $data['h:Reply-To'] = $replyToData;
148 }
149
150 if (!empty($bcc)) {
151 foreach ($bcc as $key => $bccEmail) {
152 $data['bcc['.$key.']'] = $bccEmail[0];
153 }
154 }
155
156 $files = [];
157 if (!empty($attachments)) {
158 foreach ($attachments as $key => $attachment) {
159 $files[] = [
160 'name' => 'attachment['.$key.']',
161 'path' => $attachment[0],
162 'filename' => basename($attachment[0]),
163 ];
164 }
165 }
166
167 $responseMailer = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true, $files);
168
169 if (empty($responseMailer['message']) || empty($responseMailer['id']) || $responseMailer['message'] !== 'Queued. Thank you.') {
170 $response['error'] = true;
171 $response['message'] = $responseMailer['message'] ?? ($responseMailer['error_curl'] ?? acym_translation('ACYM_ERROR'));
172 } else {
173 $response['error'] = false;
174 }
175 }
176
177 private function setSendingMethodApiUrl($credentials = [])
178 {
179 if (empty($credentials)) {
180 $this->onAcymGetCredentialsSendingMethod($credentials, self::SENDING_METHOD_ID);
181 }
182
183 $this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_US;
184 if (!empty($credentials[self::SENDING_METHOD_ID.'_api_region']) && $credentials[self::SENDING_METHOD_ID.'_api_region'] === 'eu') {
185 $this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_EU;
186 }
187
188 if (!empty($credentials[self::SENDING_METHOD_ID.'_api_domain'])) {
189 $this->sendingMethodApiUrl .= $credentials[self::SENDING_METHOD_ID.'_api_domain'].'/';
190 }
191 }
192
193 /**
194 * @param array $credentials
195 * @param string $sendingMethod
196 * @param array $sendingMethodListParams this parameter is only used for the plugin sending method list
197 *
198 * @return void
199 */
200 public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = [])
201 {
202 if ($sendingMethod != self::SENDING_METHOD_ID) return;
203
204 $apiKey = self::SENDING_METHOD_ID.'_api_key';
205 $domain = self::SENDING_METHOD_ID.'_api_domain';
206 $region = self::SENDING_METHOD_ID.'_api_region';
207
208 $credentials = [
209 $apiKey => $sendingMethodListParams[$apiKey] ?? $this->config->get($apiKey, ''),
210 $domain => $sendingMethodListParams[$domain] ?? $this->config->get($domain, ''),
211 $region => $sendingMethodListParams[$region] ?? $this->config->get($region, 'us'),
212 ];
213 }
214
215 public function getHeadersSendingMethod($sendingMethod, $credentials = [])
216 {
217 return ['content-type: multipart/form-data'];
218 }
219
220 public function getAuthenticationSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = [])
221 {
222 if (empty($credentials)) $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams);
223
224 return [
225 'name' => 'api',
226 'pwd' => $credentials[self::SENDING_METHOD_ID.'_api_key'],
227 ];
228 }
229
230 public function onAcymSendingMethodOptions(&$data)
231 {
232 $data['embedImage'][self::SENDING_METHOD_ID] = false;
233 }
234
235 public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
236 {
237 if ($method != self::SENDING_METHOD_ID) return;
238
239 if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
240 $wpMailSmtpSetting = get_option('wp_mail_smtp', '');
241 if (empty($wpMailSmtpSetting) || empty($wpMailSmtpSetting['mailgun'])) return;
242
243 $settings = $wpMailSmtpSetting['mailgun'];
244
245 if (empty($settings['api_key']) || empty($settings['domain']) || empty($settings['region'])) return;
246
247 $data['mailgun_api_domain'] = $settings['domain'];
248 $data['mailgun_api_key'] = $settings['api_key'];
249 $data['mailgun_api_region'] = $settings['region'];
250 }
251 }
252 }
253