PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.11.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.11.1
11.1.1 11.1.0 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 All 62 releases
acymailing / back / dynamics / mailgun / plugin.php

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

235 lines 9.9 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 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 acym_translationSprintf('ACYM_SENDING_METHOD_API_REGION', self::SENDING_METHOD_NAME);
45 echo 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 echo 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 self::SENDING_METHOD_ID; ?>_settings_api-domain">
62 <?php echo acym_translationSprintf(
63 'ACYM_SENDING_METHOD_API_DOMAIN',
64 self::SENDING_METHOD_NAME
65 ); ?>
66 </label>
67 <input type="text"
68 id="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-domain"
69 value="<?php echo empty($defaultDomain) ? $this->config->get(self::SENDING_METHOD_ID.'_api_domain') : $defaultDomain; ?>"
70 name="config[<?php echo self::SENDING_METHOD_ID; ?>_api_domain]"
71 class="cell acym__configuration__mail__settings__text">
72 </div>
73 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
74 <label class="cell shrink margin-right-1" for="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key">
75 <?php echo acym_translationSprintf(
76 'ACYM_SENDING_METHOD_API_KEY',
77 self::SENDING_METHOD_NAME
78 ); ?>
79 </label>
80 <?php echo $this->getLinks('https://signup.mailgun.com/new/signup', 'https://www.mailgun.com/pricing/'); ?>
81 <input type="text"
82 id="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key"
83 value="<?php echo empty($defaultApiKey) ? $this->config->get(self::SENDING_METHOD_ID.'_api_key') : $defaultApiKey; ?>"
84 name="config[<?php echo self::SENDING_METHOD_ID; ?>_api_key]"
85 class="cell acym__configuration__mail__settings__text">
86 <?php echo $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?>
87 <?php echo $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp'); ?>
88 </div>
89 </div>
90 <?php
91 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
92 }
93
94 public function onAcymTestCredentialSendingMethod($sendingMethod, $credentials)
95 {
96 if ($sendingMethod !== self::SENDING_METHOD_ID) return;
97
98 $this->setSendingMethodApiUrl($credentials);
99 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
100 $authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, $credentials);
101 $data = [
102 'from' => $this->config->get('from_email'),
103 'to' => acym_currentUserEmail(),
104 'subject' => 'Test email',
105 'html' => 'Test email body',
106 'o:testmode' => true,
107 ];
108
109 $response = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true);
110
111 if (empty($response)) {
112 acym_sendAjaxResponse(acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY'), [], false);
113 } else {
114 acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
115 }
116 }
117
118 public function onAcymSendEmail(&$response, $mailerHelper, $to, $from, $replyTo, $bcc = [], $attachments = [], $sendingMethodListParams = [])
119 {
120 if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return;
121
122 $this->setSendingMethodApiUrl();
123 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
124 $authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams);
125 $fromData = $from['email'];
126 $replyToData = $replyTo['email'] ?? '';
127 $toData = $to['email'];
128 if ($this->config->get('add_names', 1) == 1) {
129 if (!empty($from['name'])) $fromData = $from['name'].' <'.$fromData.'>';
130 if (!empty($replyTo['name']) && !empty($replyToData)) $replyToData = $replyTo['name'].' <'.$replyToData.'>';
131 if (!empty($to['name'])) $toData = $to['name'].' <'.$toData.'>';
132 }
133 $data = [
134 'from' => $fromData,
135 'to' => $toData,
136 'subject' => $mailerHelper->Subject,
137 'html' => $mailerHelper->Body,
138 ];
139
140 if (!empty($replyToData)) {
141 $data['h:Reply-To'] = $replyToData;
142 }
143
144 if (!empty($bcc)) {
145 foreach ($bcc as $key => $bccEmail) {
146 $data['bcc['.$key.']'] = $bccEmail[0];
147 }
148 }
149
150 if (!empty($attachments)) {
151 foreach ($attachments as $key => $attachment) {
152 $data['attachment['.$key.']'] = curl_file_create($attachment[0]);
153 }
154 }
155
156 $responseMailer = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true);
157
158 if (empty($responseMailer['message']) || empty($responseMailer['id']) || $responseMailer['message'] !== 'Queued. Thank you.') {
159 $response['error'] = true;
160 $response['message'] = $responseMailer['message'] ?? ($responseMailer['error_curl'] ?? acym_translation('ACYM_ERROR'));
161 } else {
162 $response['error'] = false;
163 }
164 }
165
166 private function setSendingMethodApiUrl($credentials = [])
167 {
168 if (empty($credentials)) {
169 $this->onAcymGetCredentialsSendingMethod($credentials, self::SENDING_METHOD_ID);
170 }
171
172 $this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_US;
173 if (!empty($credentials[self::SENDING_METHOD_ID.'_api_region']) && $credentials[self::SENDING_METHOD_ID.'_api_region'] === 'eu') {
174 $this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_EU;
175 }
176
177 if (!empty($credentials[self::SENDING_METHOD_ID.'_api_domain'])) {
178 $this->sendingMethodApiUrl .= $credentials[self::SENDING_METHOD_ID.'_api_domain'].'/';
179 }
180 }
181
182 public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = [])
183 {
184 if ($sendingMethod != self::SENDING_METHOD_ID) return;
185
186 $apiKey = self::SENDING_METHOD_ID.'_api_key';
187 $domain = self::SENDING_METHOD_ID.'_api_domain';
188 $region = self::SENDING_METHOD_ID.'_api_region';
189
190 $credentials = [
191 $apiKey => $sendingMethodListParams[$apiKey] ?? $this->config->get($apiKey, ''),
192 $domain => $sendingMethodListParams[$domain] ?? $this->config->get($domain, ''),
193 $region => $sendingMethodListParams[$region] ?? $this->config->get($region, 'us'),
194 ];
195 }
196
197 public function getHeadersSendingMethod($sendingMethod, $credentials = [])
198 {
199 return ['content-type: multipart/form-data'];
200 }
201
202 public function getAuthenticationSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = [])
203 {
204 if (empty($credentials)) $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams);
205
206 return [
207 'name' => 'api',
208 'pwd' => $credentials[self::SENDING_METHOD_ID.'_api_key'],
209 ];
210 }
211
212 public function onAcymSendingMethodOptions(&$data)
213 {
214 $data['embedImage'][self::SENDING_METHOD_ID] = false;
215 }
216
217 public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
218 {
219 if ($method != self::SENDING_METHOD_ID) return;
220
221 if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
222 $wpMailSmtpSetting = get_option('wp_mail_smtp', '');
223 if (empty($wpMailSmtpSetting) || empty($wpMailSmtpSetting['mailgun'])) return;
224
225 $settings = $wpMailSmtpSetting['mailgun'];
226
227 if (empty($settings['api_key']) || empty($settings['domain']) || empty($settings['region'])) return;
228
229 $data['mailgun_api_domain'] = $settings['domain'];
230 $data['mailgun_api_key'] = $settings['api_key'];
231 $data['mailgun_api_region'] = $settings['region'];
232 }
233 }
234 }
235