From = isset($options['txtSmtpMail']) ? sanitize_text_field($options['txtSmtpMail']) : ''; $phpmailer->FromName = isset($options['txtSmtpName']) ? sanitize_text_field($options['txtSmtpName']) : ''; if ( !empty($phpmailer->From) && !empty($phpmailer->FromName) ) { $phpmailer->SetFrom( $phpmailer->From, $phpmailer->FromName ); } if ($mailer_type === 'smtp') { $phpmailer->isSMTP(); $phpmailer->Host = isset($options['txtSmtpHost']) ? sanitize_text_field($options['txtSmtpHost']) : ''; $phpmailer->Port = isset($options['txtSmtpPort']) ? sanitize_text_field($options['txtSmtpPort']) : 25; $username = isset($options['txtSmtpUserName']) ? sanitize_text_field($options['txtSmtpUserName']) : ''; $password = isset($options['txtSmtpPassword']) ? sanitize_text_field($options['txtSmtpPassword']) : ''; if (!empty($password)) { $password = AIO_Crypto::decrypt($password); } if ( !empty($username) ) { $phpmailer->SMTPAuth = true; $phpmailer->Username = $username; $phpmailer->Password = $password; } /* Set the SMTPSecure value */ $enc = isset($options['opt-smtp-enc']) ? $options['opt-smtp-enc'] : 'none'; if ( 'none' !== $enc ) { $phpmailer->SMTPSecure = $enc; } } elseif ($mailer_type === 'google' || $mailer_type === 'microsoft') { // OAuth 2.0 Boilerplate $phpmailer->isSMTP(); $phpmailer->SMTPAuth = true; $phpmailer->AuthType = 'XOAUTH2'; // SMTP Server for Google/Microsoft if ($mailer_type === 'google') { $phpmailer->Host = 'smtp.gmail.com'; $phpmailer->Port = 465; $phpmailer->SMTPSecure = 'ssl'; } else { $phpmailer->Host = 'smtp.office365.com'; $phpmailer->Port = 587; $phpmailer->SMTPSecure = 'tls'; } $encrypted_token = isset($options['optOAuthRefreshToken_' . $mailer_type]) ? $options['optOAuthRefreshToken_' . $mailer_type] : ''; $refresh_token = !empty($encrypted_token) ? AIO_Crypto::decrypt($encrypted_token) : ''; if (!empty($refresh_token)) { if (!interface_exists('\PHPMailer\PHPMailer\OAuthTokenProvider') && file_exists(ABSPATH . WPINC . '/PHPMailer/OAuthTokenProvider.php')) { require_once ABSPATH . WPINC . '/PHPMailer/OAuthTokenProvider.php'; } if (!class_exists('AIO_OAuthTokenProvider')) { class AIO_OAuthTokenProvider implements \PHPMailer\PHPMailer\OAuthTokenProvider { private $email; private $client_id; private $client_secret; private $refresh_token; private $provider; public function __construct($opts) { $this->email = $opts['email']; $this->client_id = $opts['client_id']; $this->client_secret = $opts['client_secret']; $this->refresh_token = $opts['refresh_token']; $this->provider = $opts['provider']; } public function getOauth64() { $url = $this->provider === 'google' ? 'https://oauth2.googleapis.com/token' : 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; $args = [ 'method' => 'POST', 'body' => [ 'client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'refresh_token' => $this->refresh_token, 'grant_type' => 'refresh_token' ] ]; $response = wp_remote_post($url, $args); if (is_wp_error($response)) { return ''; } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body['access_token'])) { return ''; } // Format: base64("user=" . email . "^Aauth=Bearer " . token . "^A^A") return base64_encode("user=" . $this->email . "\001auth=Bearer " . $body['access_token'] . "\001\001"); } } } $raw_client_secret = $options['txtOAuthClientSecret'] ?? ''; if (!empty($raw_client_secret)) { $raw_client_secret = AIO_Crypto::decrypt($raw_client_secret); } $phpmailer->setOAuth(new AIO_OAuthTokenProvider([ 'email' => $phpmailer->From, 'client_id' => $options['txtOAuthClientId'] ?? '', 'client_secret' => $raw_client_secret, 'refresh_token' => $refresh_token, 'provider' => $mailer_type ])); } } //set reasonable timeout $phpmailer->Timeout = 15; } function aio_CatchPhpMailerFail($error){ error_log( $error->get_error_message(), 3, WP_CONTENT_DIR . '/debug.log' ); } add_action( 'wp_mail_failed', 'aio_CatchPhpMailerFail' ); function aio_SetMailContentType(){ return "text/html"; } add_filter( 'wp_mail_content_type', 'aio_SetMailContentType' );