From = $from_email; if ( ! empty( $from_name ) ) { $phpmailer->FromName = $from_name; $phpmailer->SetFrom( $from_email, $from_name ); } else { $phpmailer->SetFrom( $from_email ); } } if ( $mailer_type === 'smtp' ) { $host = isset( $options['txtSmtpHost'] ) ? sanitize_text_field( $options['txtSmtpHost'] ) : ''; if ( empty( $host ) ) { return; } $phpmailer->isSMTP(); $phpmailer->Host = $host; $phpmailer->Port = ! empty( $options['txtSmtpPort'] ) ? (int) $options['txtSmtpPort'] : 25; $username = isset( $options['txtSmtpUserName'] ) ? sanitize_text_field( $options['txtSmtpUserName'] ) : ''; $password = isset( $options['txtSmtpPassword'] ) ? (string) $options['txtSmtpPassword'] : ''; if ( ! empty( $password ) ) { $password = AIO_Crypto::decrypt( $password ); } if ( ! empty( $username ) ) { $phpmailer->SMTPAuth = true; $phpmailer->Username = $username; $phpmailer->Password = $password; } $enc = isset( $options['opt-smtp-enc'] ) ? $options['opt-smtp-enc'] : 'none'; if ( in_array( $enc, [ 'ssl', 'tls' ], true ) ) { $phpmailer->SMTPSecure = $enc; } } elseif ( $mailer_type === 'google' || $mailer_type === 'microsoft' ) { $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 ) ) { return; } $phpmailer->isSMTP(); $phpmailer->SMTPAuth = true; $phpmailer->AuthType = 'XOAUTH2'; 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'; } 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() { $cache_key = 'aio_smtp_acc_' . md5( $this->provider . '|' . $this->email . '|' . $this->client_id ); $cached_token = get_transient( $cache_key ); $access_token = ! empty( $cached_token ) ? AIO_Crypto::decrypt( $cached_token ) : ''; if ( empty( $access_token ) ) { $url = $this->provider === 'google' ? 'https://oauth2.googleapis.com/token' : 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; $response = wp_safe_remote_post( $url, [ 'timeout' => 15, 'body' => [ 'client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'refresh_token' => $this->refresh_token, 'grant_type' => 'refresh_token', ], ] ); if ( is_wp_error( $response ) ) { return ''; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['access_token'] ) ) { return ''; } $access_token = $body['access_token']; $ttl = isset( $body['expires_in'] ) ? max( 60, (int) $body['expires_in'] - 120 ) : 3000; set_transient( $cache_key, AIO_Crypto::encrypt( $access_token ), $ttl ); } return base64_encode( 'user=' . $this->email . "\001auth=Bearer " . $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, ] ) ); } $phpmailer->Timeout = 15; }