pluginDescription->name = acym_translation('ACYM_OUTLOOK');
}
public function onAcymGetSendingMethods(&$data, $isMailer = false)
{
if ($isMailer) {
return;
}
$data['sendingMethods'][self::SENDING_METHOD_ID] = [
'name' => $this->pluginDescription->name,
'image' => ACYM_IMAGES.'mailers/outlook.svg',
];
}
public function onAcymGetSendingMethodsHtmlSetting(&$data)
{
$username = $this->config->get('outlook_username', $this->config->get('from_email'));
$setupDocumentation = ACYM_DOCUMENTATION.'setup/configuration/mail-configuration/set-up-oauth-2.0';
$redirectUrl = trim($this->config->get('outlook_redirect_url', acym_baseURI()));
$refreshToken = $this->config->get('outlook_refresh_token');
$refreshTokenExpiration = $this->config->get('outlook_refresh_token_expiration');
$mustAuthenticate = empty($refreshToken) || (!empty($refreshTokenExpiration) && $refreshTokenExpiration < time());
$mailerHelper = new MailerHelper();
ob_start();
?>
config->get('outlook_tenant');
acym_select(
[
'common' => 'ACYM_ANY_ACCOUNT_TYPE',
'consumers' => 'ACYM_MICROSOFT_ACCOUNTS',
'organizations' => 'ACYM_ORGANIZATIONS',
],
'config[outlook_tenant]',
empty($value) ? 'consumers' : $value,
[
'class' => 'acym__select',
],
'',
'',
'',
true,
true
);
?>
getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp', false); ?>
config->get('mailer_method') !== self::SENDING_METHOD_ID) {
return;
}
$clientId = $this->config->get('outlook_client_id');
$clientSecret = $this->config->get('outlook_client_secret');
$tenant = $this->config->get('outlook_tenant');
$redirectUrl = $this->config->get('outlook_redirect_url');
$scope = self::SCOPE_SMTP;
} else {
if ($this->config->get('bounce_server') !== 'outlook.office365.com') {
return;
}
$clientId = $this->config->get('bounce_client_id');
$clientSecret = $this->config->get('bounce_client_secret');
$tenant = $this->config->get('bounce_tenant');
$redirectUrl = acym_baseURI();
$scope = self::SCOPE_IMAP;
}
if (empty($clientId) || empty($clientSecret) || empty($redirectUrl)) {
acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
return;
}
$consentUrl = 'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/authorize';
$consentUrl .= '?response_mode=query';
$consentUrl .= '&client_id='.urlencode($clientId);
$consentUrl .= '&prompt=consent';
$consentUrl .= '&response_type=code';
$consentUrl .= '&redirect_uri='.urlencode($redirectUrl);
$consentUrl .= '&scope='.urlencode($scope);
$consentUrl .= '&state=acymailing'.($isSmtp ? 'smtp' : 'bounce');
}
/**
* Doc: https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow#redeem-a-code-for-an-access-token
*/
public function onAcymOauthCredentialsCreation(bool $isSmtp, string $code): void
{
if ($isSmtp) {
if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
return;
}
$clientId = trim($this->config->get('outlook_client_id'));
$clientSecret = trim($this->config->get('outlook_client_secret'));
$tenant = trim($this->config->get('outlook_tenant', 'consumers'));
$redirectUrl = trim($this->config->get('outlook_redirect_url', acym_baseURI()));
$scope = self::SCOPE_SMTP;
} else {
if ($this->config->get('bounce_server') !== 'outlook.office365.com') {
return;
}
$clientId = trim($this->config->get('bounce_client_id'));
$clientSecret = trim($this->config->get('bounce_client_secret'));
$tenant = trim($this->config->get('bounce_tenant', 'consumers'));
$redirectUrl = acym_baseURI();
$scope = self::SCOPE_IMAP;
}
if (empty($clientId) || empty($clientSecret)) {
acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
return;
}
$response = acym_makeCurlCall(
'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/token',
[
'method' => 'POST',
'data' => [
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => $clientId,
'client_secret' => $clientSecret,
'redirect_uri' => $redirectUrl,
'scope' => $scope,
],
]
);
acym_logError('Response from OAuth call: '.json_encode($response), self::SENDING_METHOD_ID);
if (!empty($response['error'])) {
acym_enqueueMessage(acym_translationSprintf('ACYM_SMTP_OAUTH_ERROR', $response['error']), 'error', false);
return;
}
$expiringTime = time() + (int)$response['expires_in'];
if (!empty($response['refresh_token_expires_in'])) {
$refreshExpiringTime = time() + (int)$response['refresh_token_expires_in'];
acym_enqueueMessage(
acym_translationSprintf(
'ACYM_OAUTH_TEMPORARY',
acym_date($refreshExpiringTime, 'ACYM_DATE_FORMAT_LC2')
),
'info',
false
);
} else {
$refreshExpiringTime = 0;
acym_enqueueMessage(acym_translation('ACYM_SMTP_OAUTH_OK'), 'info');
}
if ($isSmtp) {
$newConfig = [
'outlook_access_token' => $response['access_token'],
'outlook_access_token_expiration' => $expiringTime,
'outlook_refresh_token' => $response['refresh_token'],
'outlook_refresh_token_expiration' => $refreshExpiringTime,
];
} else {
$newConfig = [
'bounce_access_token' => $response['access_token'],
'bounce_access_token_expiration' => $expiringTime,
'bounce_refresh_token' => $response['refresh_token'],
'bounce_refresh_token_expiration' => $refreshExpiringTime,
];
}
$this->config->saveConfig($newConfig);
acym_config(true);
}
public function onAcymOauthRevoke(): void
{
if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
return;
}
$this->config->saveConfig(
[
'outlook_refresh_token' => '',
'outlook_refresh_token_expiration' => '',
]
);
}
}