pluginDescription->name = self::SENDING_METHOD_NAME;
}
public function onAcymGetSendingMethods(&$data, $isMailer = false)
{
$data['sendingMethods'][self::SENDING_METHOD_ID] = [
'name' => $this->pluginDescription->name,
'image' => ACYM_IMAGES.'mailers/mailgun.svg',
'image_class' => 'acym__selection__card__image__mailgun',
];
}
public function onAcymGetSendingMethodsHtmlSetting(&$data)
{
$regions = [
'us' => acym_translation('ACYM_US'),
'eu' => acym_translation('ACYM_EU'),
];
$config = empty($data['tab']) ? $this->config : $data['tab']->config;
$defaultDomain = $config->get(self::SENDING_METHOD_ID.'_api_domain');
$defaultApiKey = $config->get(self::SENDING_METHOD_ID.'_api_key');
ob_start();
?>
setSendingMethodApiUrl($credentials);
$headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
$authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, $credentials);
$data = [
'from' => $this->config->get('from_email'),
'to' => acym_currentUserEmail(),
'subject' => 'Test email',
'html' => 'Test email body',
'o:testmode' => true,
];
$response = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true);
if (empty($response)) {
acym_sendAjaxResponse(acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY'), [], false);
} else {
acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
}
}
public function onAcymSendEmail(&$response, $mailerHelper, $to, $from, $replyTo, $bcc = [], $attachments = [], $sendingMethodListParams = [])
{
if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return;
$this->setSendingMethodApiUrl();
$headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
$authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams);
$fromData = $from['email'];
$replyToData = $replyTo['email'] ?? '';
$toData = $to['email'];
if ($this->config->get('add_names', 1) == 1) {
if (!empty($from['name'])) $fromData = $from['name'].' <'.$fromData.'>';
if (!empty($replyTo['name']) && !empty($replyToData)) $replyToData = $replyTo['name'].' <'.$replyToData.'>';
if (!empty($to['name'])) $toData = $to['name'].' <'.$toData.'>';
}
$data = [
'from' => $fromData,
'to' => $toData,
'subject' => $mailerHelper->Subject,
'html' => $mailerHelper->Body,
];
if (!empty($replyToData)) {
$data['h:Reply-To'] = $replyToData;
}
if (!empty($bcc)) {
foreach ($bcc as $key => $bccEmail) {
$data['bcc['.$key.']'] = $bccEmail[0];
}
}
if (!empty($attachments)) {
foreach ($attachments as $key => $attachment) {
$data['attachment['.$key.']'] = curl_file_create($attachment[0]);
}
}
$responseMailer = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true);
if (empty($responseMailer['message']) || empty($responseMailer['id']) || $responseMailer['message'] !== 'Queued. Thank you.') {
$response['error'] = true;
$response['message'] = $responseMailer['message'] ?? ($responseMailer['error_curl'] ?? acym_translation('ACYM_ERROR'));
} else {
$response['error'] = false;
}
}
private function setSendingMethodApiUrl($credentials = [])
{
if (empty($credentials)) {
$this->onAcymGetCredentialsSendingMethod($credentials, self::SENDING_METHOD_ID);
}
$this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_US;
if (!empty($credentials[self::SENDING_METHOD_ID.'_api_region']) && $credentials[self::SENDING_METHOD_ID.'_api_region'] === 'eu') {
$this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_EU;
}
if (!empty($credentials[self::SENDING_METHOD_ID.'_api_domain'])) {
$this->sendingMethodApiUrl .= $credentials[self::SENDING_METHOD_ID.'_api_domain'].'/';
}
}
public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = [])
{
if ($sendingMethod != self::SENDING_METHOD_ID) return;
$apiKey = self::SENDING_METHOD_ID.'_api_key';
$domain = self::SENDING_METHOD_ID.'_api_domain';
$region = self::SENDING_METHOD_ID.'_api_region';
$credentials = [
$apiKey => $sendingMethodListParams[$apiKey] ?? $this->config->get($apiKey, ''),
$domain => $sendingMethodListParams[$domain] ?? $this->config->get($domain, ''),
$region => $sendingMethodListParams[$region] ?? $this->config->get($region, 'us'),
];
}
public function getHeadersSendingMethod($sendingMethod, $credentials = [])
{
return ['content-type: multipart/form-data'];
}
public function getAuthenticationSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = [])
{
if (empty($credentials)) $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams);
return [
'name' => 'api',
'pwd' => $credentials[self::SENDING_METHOD_ID.'_api_key'],
];
}
public function onAcymSendingMethodOptions(&$data)
{
$data['embedImage'][self::SENDING_METHOD_ID] = false;
}
public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
{
if ($method != self::SENDING_METHOD_ID) return;
if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
$wpMailSmtpSetting = get_option('wp_mail_smtp', '');
if (empty($wpMailSmtpSetting) || empty($wpMailSmtpSetting['mailgun'])) return;
$settings = $wpMailSmtpSetting['mailgun'];
if (empty($settings['api_key']) || empty($settings['domain']) || empty($settings['region'])) return;
$data['mailgun_api_domain'] = $settings['domain'];
$data['mailgun_api_key'] = $settings['api_key'];
$data['mailgun_api_region'] = $settings['region'];
}
}
}