| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Core\AcymPlugin; |
| 4 |
|
| 5 |
class plgAcymSmtp extends AcymPlugin |
| 6 |
{ |
| 7 |
const SENDING_METHOD_ID = 'smtp'; |
| 8 |
const SENDING_METHOD_NAME = 'SMTP'; |
| 9 |
const HOST_AUTH_2 = ['smtp.gmail.com', 'smtp-mail.outlook.com', 'smtp.office365.com']; |
| 10 |
|
| 11 |
public function __construct() |
| 12 |
{ |
| 13 |
parent::__construct(); |
| 14 |
$this->pluginDescription->name = acym_translation('ACYM_SMTP_SERVICE'); |
| 15 |
} |
| 16 |
|
| 17 |
public function onAcymGetSendingMethods(&$data, $isMailer = false) |
| 18 |
{ |
| 19 |
if ($isMailer) return; |
| 20 |
$data['sendingMethods'][self::SENDING_METHOD_ID] = [ |
| 21 |
'name' => $this->pluginDescription->name, |
| 22 |
'icon' => 'acymicon-email', |
| 23 |
]; |
| 24 |
} |
| 25 |
|
| 26 |
public function onAcymGetSendingMethodsHtmlSetting(&$data) |
| 27 |
{ |
| 28 |
$smtpRedirectUrl = trim($this->config->get('smtp_redirectUrl')); |
| 29 |
$redirectUrl = empty($smtpRedirectUrl) ? acym_baseURI() : $smtpRedirectUrl; |
| 30 |
|
| 31 |
$isWalkTrough = $this->config->get('walk_through', 0); |
| 32 |
$loginAttribute = !$isWalkTrough ? 'acym-data-before="jQuery.acymConfigSave();"' : ''; |
| 33 |
|
| 34 |
$link = ACYM_DOCUMENTATION.'setup/configuration/mail-configuration/set-up-oauth-2.0'; |
| 35 |
|
| 36 |
ob_start(); |
| 37 |
?> |
| 38 |
<div class="send_settings grid-x cell large-6 xlarge-5 xxlarge-4 margin-auto" id="<?php echo self::SENDING_METHOD_ID; ?>_settings"> |
| 39 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 40 |
<label for="smtp_host" class="cell"><?php echo acym_translation('ACYM_SMTP_SERVER'); ?></label> |
| 41 |
<input id="smtp_host" class="cell" type="text" name="config[smtp_host]" value="<?php echo acym_escape($this->config->get('smtp_host')); ?>"> |
| 42 |
</div> |
| 43 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 44 |
<label for="smtp_port" class="cell"><?php echo acym_translation('ACYM_SMTP_PORT').acym_info('ACYM_SMTP_PORT_DESC'); ?></label> |
| 45 |
<input |
| 46 |
id="smtp_port" |
| 47 |
class="cell medium-6" |
| 48 |
type="number" |
| 49 |
name="config[smtp_port]" value="<?php echo acym_escape($this->config->get('smtp_port')); ?>" |
| 50 |
placeholder="465, 587, 2525, 25"> |
| 51 |
</div> |
| 52 |
<div id="available_ports" class="cell acym__sending__methods__one__settings"> |
| 53 |
<a href="#" id="available_ports_check"><?php echo acym_translation('ACYM_SMTP_AVAILABLE_PORTS'); ?></a> |
| 54 |
<?php echo $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp'); ?> |
| 55 |
</div> |
| 56 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 57 |
<label for="smtp_secured" class="cell"><?php echo acym_translation('ACYM_SMTP_SECURE').acym_info('ACYM_SMTP_SECURE_DESC'); ?></label> |
| 58 |
<div class="cell medium-6"> |
| 59 |
<?php |
| 60 |
echo acym_select( |
| 61 |
[ |
| 62 |
'' => '- - -', |
| 63 |
'ssl' => 'SSL', |
| 64 |
'tls' => 'TLS', |
| 65 |
], |
| 66 |
'config[smtp_secured]', |
| 67 |
$this->config->get('smtp_secured', 'ssl'), |
| 68 |
[ |
| 69 |
'class' => 'acym__select', |
| 70 |
'acym-data-infinite' => '', |
| 71 |
], |
| 72 |
'', |
| 73 |
'', |
| 74 |
'smtp_secured' |
| 75 |
); |
| 76 |
?> |
| 77 |
</div> |
| 78 |
</div> |
| 79 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 80 |
<?php |
| 81 |
echo acym_switch( |
| 82 |
'config[smtp_keepalive]', |
| 83 |
$this->config->get('smtp_keepalive'), |
| 84 |
acym_translation('ACYM_SMTP_ALIVE').acym_info('ACYM_SMTP_ALIVE_DESC'), |
| 85 |
[], |
| 86 |
'medium-5 small-9' |
| 87 |
); |
| 88 |
?> |
| 89 |
</div> |
| 90 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 91 |
<?php |
| 92 |
echo acym_switch( |
| 93 |
'config[smtp_auth]', |
| 94 |
$this->config->get('smtp_auth', 1), |
| 95 |
acym_translation('ACYM_SMTP_AUTHENTICATION').acym_info('ACYM_SMTP_AUTHENTICATION_DESC'), |
| 96 |
[], |
| 97 |
'medium-5 small-9' |
| 98 |
); |
| 99 |
?> |
| 100 |
</div> |
| 101 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 102 |
<label for="smtp_method" class="cell"><?php echo acym_translation('ACYM_AUTHENTICATION_METHOD').acym_info('ACYM_AUTHENTICATION_METHOD_DESC'); ?></label> |
| 103 |
<div class="cell medium-6"> |
| 104 |
<?php |
| 105 |
echo acym_select( |
| 106 |
[ |
| 107 |
'' => acym_translation('ACYM_AUTOMATIC'), |
| 108 |
'CRAM-MD5' => 'CRAM-MD5', |
| 109 |
'LOGIN' => 'LOGIN', |
| 110 |
'PLAIN' => 'PLAIN', |
| 111 |
'XOAUTH2' => 'XOAUTH2', |
| 112 |
], |
| 113 |
'config[smtp_method]', |
| 114 |
$this->config->get('smtp_method', ''), |
| 115 |
[ |
| 116 |
'class' => 'acym__select', |
| 117 |
'acym-data-infinite' => '', |
| 118 |
], |
| 119 |
'', |
| 120 |
'', |
| 121 |
'smtp_method' |
| 122 |
); |
| 123 |
?> |
| 124 |
</div> |
| 125 |
</div> |
| 126 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings" id="acym__sending__methods__one__settings__type"> |
| 127 |
<label for="smtp_type" class="cell"><?php echo acym_translation('ACYM_CONNECTION_TYPE').acym_info('ACYM_CONNECTION_TYPE_DESC'); ?></label> |
| 128 |
<div class="cell medium-6"> |
| 129 |
<?php |
| 130 |
echo acym_select( |
| 131 |
[ |
| 132 |
'oauth' => 'ACYM_WITH_OAUTH', |
| 133 |
'password' => 'ACYM_WITH_PASSWORD', |
| 134 |
], |
| 135 |
'config[smtp_type]', |
| 136 |
$this->config->get('smtp_type', 'oauth'), |
| 137 |
[ |
| 138 |
'class' => 'acym__select', |
| 139 |
'acym-data-infinite' => '', |
| 140 |
], |
| 141 |
'', |
| 142 |
'', |
| 143 |
'smtp_type', |
| 144 |
true |
| 145 |
); |
| 146 |
?> |
| 147 |
</div> |
| 148 |
</div> |
| 149 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 150 |
<label for="smtp_username" class="cell"><?php echo acym_translation('ACYM_SMTP_USERNAME').acym_info('ACYM_SMTP_USERNAME_DESC'); ?></label> |
| 151 |
<input id="smtp_username" |
| 152 |
class="cell" |
| 153 |
type="text" |
| 154 |
name="config[smtp_username]" |
| 155 |
value="<?php echo acym_escape($this->config->get('smtp_username')); ?>"> |
| 156 |
</div> |
| 157 |
<div class="acym__default_auth_sending_params cell"> |
| 158 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings" id="oauthParams"> |
| 159 |
<label for="smtp_password" class="cell"><?php echo acym_translation('ACYM_SMTP_PASSWORD').acym_info('ACYM_SMTP_PASSWORD_DESC'); ?></label> |
| 160 |
<input id="smtp_password" |
| 161 |
class="cell" |
| 162 |
type="text" |
| 163 |
name="config[smtp_password]" |
| 164 |
value="<?php echo str_repeat('*', strlen($this->config->get('smtp_password'))); ?>"> |
| 165 |
</div> |
| 166 |
</div> |
| 167 |
<div class="acym__oauth2_sending_params cell"> |
| 168 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 169 |
<label for="smtp_clientId" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_ID', $link, false); ?></label> |
| 170 |
<input id="smtp_clientId" class="cell" type="text" name="config[smtp_clientId]" value="<?php echo acym_escape($this->config->get('smtp_clientId')); ?>"> |
| 171 |
</div> |
| 172 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 173 |
<label for="smtp_secret" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_SECRET', $link, false); ?></label> |
| 174 |
<input id="smtp_secret" class="cell" type="text" name="config[smtp_secret]" value="<?php echo acym_escape($this->config->get('smtp_secret')); ?>"> |
| 175 |
</div> |
| 176 |
<div id="smtp_tenant_container" class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 177 |
<label for="smtp_tenant" class="cell"><?php echo acym_externalLink('ACYM_TENANT', $link, false); ?></label> |
| 178 |
<?php |
| 179 |
$valuesArray = [ |
| 180 |
'consumers' => 'ACYM_MICROSOFT_ACCOUNTS', |
| 181 |
'common' => 'ACYM_ANY_ACCOUNT_TYPE', |
| 182 |
'organizations' => 'ACYM_ORGANIZATIONS', |
| 183 |
]; |
| 184 |
$value = $this->config->get('smtp_tenant'); |
| 185 |
echo acym_select( |
| 186 |
$valuesArray, |
| 187 |
'config[smtp_tenant]', |
| 188 |
empty($value) ? 'consumers' : $value, |
| 189 |
[ |
| 190 |
'class' => 'acym__select', |
| 191 |
], |
| 192 |
'', |
| 193 |
'', |
| 194 |
'', |
| 195 |
true |
| 196 |
); |
| 197 |
?> |
| 198 |
</div> |
| 199 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 200 |
<label for="smtp_redirectUrl" class="cell"><?php echo acym_translation('ACYM_SMTP_REDIRECT_URL'); ?></label> |
| 201 |
<input id="smtp_redirectUrl" class="cell" type="text" name="config[smtp_redirectUrl]" value="<?php echo acym_escape($redirectUrl); ?>"> |
| 202 |
</div> |
| 203 |
<div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> |
| 204 |
<button <?php echo $loginAttribute; ?> |
| 205 |
data-task="loginForOAuth2Smtp" |
| 206 |
class="button acy_button_submit button-secondary margin-bottom-1" |
| 207 |
id="smtp_account_login"> |
| 208 |
<?php echo acym_translation('ACYM_LOGIN'); ?> |
| 209 |
</button> |
| 210 |
</div> |
| 211 |
</div> |
| 212 |
</div> |
| 213 |
<?php |
| 214 |
$data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean(); |
| 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['smtp'])) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
$data['smtp_host'] = $wpMailSmtpSetting['smtp']['host']; |
| 228 |
$data['smtp_port'] = $wpMailSmtpSetting['smtp']['port']; |
| 229 |
$data['smtp_secured'] = $wpMailSmtpSetting['smtp']['encryption']; |
| 230 |
$data['smtp_keepalive'] = 1; |
| 231 |
$data['smtp_auth'] = $wpMailSmtpSetting['smtp']['auth'] ? 1 : 0; |
| 232 |
$data['smtp_username'] = $wpMailSmtpSetting['smtp']['user']; |
| 233 |
$data['smtp_password'] = WPMailSMTP\Helpers\Crypto::decrypt($wpMailSmtpSetting['smtp']['pass']); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
public function onAcymDisplayPage() |
| 238 |
{ |
| 239 |
$mailerMethod = trim($this->config->get('mailer_method')); |
| 240 |
if ($mailerMethod != self::SENDING_METHOD_ID) return; |
| 241 |
|
| 242 |
$clientId = trim($this->config->get('smtp_clientId')); |
| 243 |
$secret = trim($this->config->get('smtp_secret')); |
| 244 |
$host = trim($this->config->get('smtp_host')); |
| 245 |
$connectionType = $this->config->get('smtp_type'); |
| 246 |
|
| 247 |
$requireAuth = false; |
| 248 |
if (in_array($host, self::HOST_AUTH_2) && $connectionType !== 'password') { |
| 249 |
$requireAuth = true; |
| 250 |
} |
| 251 |
|
| 252 |
if ((empty($clientId) || empty($secret)) && $requireAuth) { |
| 253 |
$link = '<a class="acym_message_link" href="'.acym_completeLink('configuration#oauthParams').'">'.strtolower(acym_translation('ACYM_HERE')).'</a>'; |
| 254 |
acym_enqueueMessage(acym_translationSprintf('ACYM_SMTP_OAUTH_WARNING', $link), 'warning'); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|