/**
  * GetOauth64
  * 
  * encode the user email related to this request along with the token in base64
  * this is used for authentication, in the phpmailer smtp class
  * 
  * @return string
  */
 public function getOauth64()
 {
     $client = GmailXOAuth2::getClient();
     $options = gmail_smtp_get_option();
     if (!empty($options['oauth_access_token'])) {
         $accessToken = $options['oauth_access_token'];
     } else {
         return false;
     }
     $client->setAccessToken($accessToken);
     // Refresh the token if it's expired.
     if ($client->isAccessTokenExpired()) {
         $client->refreshToken($client->getRefreshToken());
         $accessToken = $client->getAccessToken();
         $options['oauth_access_token'] = $accessToken;
         gmail_smtp_update_option($options);
     }
     $offlineToken = GmailXOAuth2::request_offline_token();
     return base64_encode("user="******"auth=Bearer " . $offlineToken . "");
 }
Exemple #2
0
    function general_settings()
    {
        if (isset($_POST['gmail_smtp_update_settings'])) {
            $nonce = $_REQUEST['_wpnonce'];
            if (!wp_verify_nonce($nonce, 'gmail_smtp_general_settings')) {
                wp_die('Error! Nonce Security Check Failed! please save the settings again.');
            }
            $client_id = '';
            if (isset($_POST['oauth_client_id']) && !empty($_POST['oauth_client_id'])) {
                $client_id = sanitize_text_field($_POST['oauth_client_id']);
            }
            $client_secret = '';
            if (isset($_POST['oauth_client_secret']) && !empty($_POST['oauth_client_secret'])) {
                $client_secret = sanitize_text_field($_POST['oauth_client_secret']);
            }
            $oauth_user_email = '';
            if (isset($_POST['oauth_user_email']) && !empty($_POST['oauth_user_email'])) {
                $oauth_user_email = sanitize_email($_POST['oauth_user_email']);
            }
            $from_email = '';
            if (isset($_POST['from_email']) && !empty($_POST['from_email'])) {
                $from_email = sanitize_email($_POST['from_email']);
            }
            $from_name = '';
            if (isset($_POST['from_name']) && !empty($_POST['from_name'])) {
                $from_name = sanitize_text_field(stripslashes($_POST['from_name']));
            }
            $type_of_encryption = '';
            if (isset($_POST['type_of_encryption']) && !empty($_POST['type_of_encryption'])) {
                $type_of_encryption = sanitize_text_field($_POST['type_of_encryption']);
            }
            $smtp_port = '';
            if (isset($_POST['smtp_port']) && !empty($_POST['smtp_port'])) {
                $smtp_port = sanitize_text_field($_POST['smtp_port']);
            }
            $options = array();
            $options['oauth_client_id'] = $client_id;
            $options['oauth_client_secret'] = $client_secret;
            $options['oauth_user_email'] = $oauth_user_email;
            $options['from_email'] = $from_email;
            $options['from_name'] = $from_name;
            $options['type_of_encryption'] = $type_of_encryption;
            $options['smtp_port'] = $smtp_port;
            gmail_smtp_update_option($options);
            echo '<div id="message" class="updated fade"><p><strong>';
            echo __('Settings Saved!', 'gmail-smtp');
            echo '</strong></p></div>';
        }
        if (isset($_GET['gmail_smtp_access_granted'])) {
            if ($_GET['gmail_smtp_access_granted'] == "yes") {
                echo '<div id="message" class="updated fade"><p><strong>';
                echo __('Access Granted Successfully!', 'gmail-smtp');
                echo '</strong></p></div>';
            } else {
                echo '<div id="message" class="error"><p><strong>';
                echo __('Access could not be granted', 'gmail-smtp');
                echo '</strong></p></div>';
            }
        }
        $options = gmail_smtp_get_option();
        if (!is_array($options)) {
            $options = array();
        }
        $url = "https://www.google.com/accounts/Logout?continue=https://console.developers.google.com/start/api?id=gmail";
        $link_text = sprintf(wp_kses(__('Create a new <a target="_blank" href="%s">web application</a> and set its Authorized Redirect URL to the one shown on this page.', 'gmail-smtp'), array('a' => array('href' => array(), 'target' => array()))), esc_url($url));
        ?>

        <div class="update-nag">
            <h3><?php 
        _e('Basic Setup Instructions', 'gmail-smtp');
        ?>
</li></h3>
            <ol>
                <li><?php 
        echo $link_text;
        ?>
</li>
                <li><?php 
        _e('Once the application is created it will generate a client ID and client secret. Copy them into the fields here.', 'gmail-smtp');
        ?>
</li>
                <li><?php 
        _e('Enter your OAuth Email, From Email and From name.', 'gmail-smtp');
        ?>
</li>
                <li><?php 
        _e('Select an encryption.', 'gmail-smtp');
        ?>
</li>
                <li><?php 
        _e('Enter a port number.', 'gmail-smtp');
        ?>
</li>
                <li><?php 
        _e('Save the settings.', 'gmail-smtp');
        ?>
</li>
                <li><?php 
        _e('Now you can authorize your app to access the Gmail API by clicking on the Grant Permission button.', 'gmail-smtp');
        ?>
</li>
            </ol>    
        </div>

        <form method="post" action="<?php 
        echo $_SERVER["REQUEST_URI"];
        ?>
">
            <?php 
        wp_nonce_field('gmail_smtp_general_settings');
        ?>

            <table class="form-table">

                <tbody>
                    
                    <tr valign="top">
                        <th scope="row"><label><?php 
        _e('SMTP Status', 'gmail-smtp');
        ?>
</label></th>
                        <?php 
        if (isset($options['oauth_access_token']) && !empty($options['oauth_access_token'])) {
            ?>
                        <td><img src="<?php 
            echo GMAIL_SMTP_URL . '/images/connected.png';
            ?>
" style="height: 30px;">
                            <p class="description"><?php 
            _e('Connected', 'gmail-smtp');
            ?>
</p></td>
                        <?php 
        } else {
            ?>
                        <td><img src="<?php 
            echo GMAIL_SMTP_URL . '/images/not-connected.png';
            ?>
" style="height: 30px;">
                            <p class="description"><?php 
            _e('Not Connected', 'gmail-smtp');
            ?>
</p></td>    
                        <?php 
        }
        ?>
                    </tr>

                    <tr valign="top">
                        <th scope="row"><label for="oauth_redirect_uri"><?php 
        _e('Authorized Redirect URI', 'gmail-smtp');
        ?>
</label></th>
                        <td><input name="oauth_redirect_uri" type="text" id="oauth_redirect_uri" value="<?php 
        echo admin_url("options-general.php?page=gmail-smtp-settings&action=oauth_grant");
        ?>
" readonly class="regular-text code">
                            <p class="description"><?php 
        _e('Copy this URL into your web application', 'gmail-smtp');
        ?>
</p></td>
                    </tr>

                    <tr valign="top">
                        <th scope="row"><label for="oauth_client_id"><?php 
        _e('Client ID', 'gmail-smtp');
        ?>
</label></th>
                        <td><input name="oauth_client_id" type="text" id="oauth_client_id" value="<?php 
        echo $options['oauth_client_id'];
        ?>
" class="regular-text code">
                            <p class="description"><?php 
        _e('The client ID of your web application', 'gmail-smtp');
        ?>
</p></td>
                    </tr>

                    <tr valign="top">
                        <th scope="row"><label for="oauth_client_secret"><?php 
        _e('Client Secret', 'gmail-smtp');
        ?>
</label></th>
                        <td><input name="oauth_client_secret" type="text" id="oauth_client_secret" value="<?php 
        echo $options['oauth_client_secret'];
        ?>
" class="regular-text code">
                            <p class="description"><?php 
        _e('The client secret of your web application', 'gmail-smtp');
        ?>
</p></td>
                    </tr>                   
                    
                    <tr valign="top">
                        <th scope="row"><label for="oauth_user_email"><?php 
        _e('OAuth Email Address', 'gmail-smtp');
        ?>
</label></th>
                        <td><input name="oauth_user_email" type="text" id="oauth_user_email" value="<?php 
        echo $options['oauth_user_email'];
        ?>
" class="regular-text code">
                            <p class="description"><?php 
        _e('The email address that you will use for SMTP authentication. This should be the same email used in the Google Developers Console.', 'gmail-smtp');
        ?>
</p></td>
                    </tr>
                    
                    <tr valign="top">
                        <th scope="row"><label for="from_email"><?php 
        _e('From Email Address', 'gmail-smtp');
        ?>
</label></th>
                        <td><input name="from_email" type="text" id="from_email" value="<?php 
        echo $options['from_email'];
        ?>
" class="regular-text code">
                            <p class="description"><?php 
        _e('The email address which will be used as the From Address when sending an email.', 'gmail-smtp');
        ?>
</p></td>
                    </tr>
                    
                    <tr valign="top">
                        <th scope="row"><label for="from_name"><?php 
        _e('From Name', 'gmail-smtp');
        ?>
</label></th>
                        <td><input name="from_name" type="text" id="from_name" value="<?php 
        echo $options['from_name'];
        ?>
" class="regular-text code">
                            <p class="description"><?php 
        _e('The name which will be used as the From Name when sending an email.', 'gmail-smtp');
        ?>
</p></td>
                    </tr>
                    
                    <tr>
                    <th scope="row"><label for="type_of_encryption"><?php 
        _e('Type of Encryption', 'gmail-smtp');
        ?>
</label></th>
                    <td>
                        <select name="type_of_encryption" id="type_of_encryption">
                            <option value="tls" <?php 
        echo selected($options['type_of_encryption'], 'tls', false);
        ?>
><?php 
        _e('TLS', 'gmail-smtp');
        ?>
</option>
                            <option value="ssl" <?php 
        echo selected($options['type_of_encryption'], 'ssl', false);
        ?>
><?php 
        _e('SSL', 'gmail-smtp');
        ?>
</option>
                        </select>
                        <p class="description"><?php 
        _e('The encryption which will be used when sending an email (TLS is recommended).', 'gmail-smtp');
        ?>
</p>
                    </td>
                    </tr>
                    
                    <tr valign="top">
                        <th scope="row"><label for="smtp_port"><?php 
        _e('SMTP Port', 'gmail-smtp');
        ?>
</label></th>
                        <td><input name="smtp_port" type="text" id="smtp_port" value="<?php 
        echo $options['smtp_port'];
        ?>
" class="regular-text code">
                            <p class="description"><?php 
        _e('The port which will be used when sending an email. If you choose TLS it should be set to 587. For SSL use port 465 instead.', 'gmail-smtp');
        ?>
</p></td>
                    </tr>

                </tbody>

            </table>

            <p class="submit"><input type="submit" name="gmail_smtp_update_settings" id="gmail_smtp_update_settings" class="button button-primary" value="Save Changes"></p>
        </form>
        
        <?php 
        if ($this->can_grant_permission()) {
            ?>
        <a class="button button-primary" href="<?php 
            echo $_SERVER["REQUEST_URI"] . '&action=oauth_grant';
            ?>
"><?php 
            _e('Grant Permission', 'gmail-smtp');
            ?>
</a>                             
        <?php 
        }
    }