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