$mail->SMTPAuth = true; //Set AuthType $mail->AuthType = 'XOAUTH2'; //User Email to use for SMTP authentication - Use the same Email used in Google Developer Console $mail->oauthUserEmail = "*****@*****.**"; //Obtained From Google Developer Console $mail->oauthClientId = "RANDOMCHARS-----duv1n2.apps.googleusercontent.com"; //Obtained From Google Developer Console $mail->oauthClientSecret = "RANDOMCHARS-----lGyjPcRtvP"; //Obtained By running get_oauth_token.php after setting up APP in Google Developer Console. //Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php // eg: http://localhost/phpmail/get_oauth_token.php $mail->oauthRefreshToken = "RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0"; //Set who the message is to be sent from //For gmail, this generally needs to be the same as the user you logged in as $mail->setFrom('*****@*****.**', 'First Last'); //Set who the message is to be sent to $mail->addAddress('*****@*****.**', 'John Doe'); //Set the subject line $mail->Subject = 'PHPMailer GMail SMTP test'; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; //Attach an image file $mail->addAttachment('images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else {
function test_email_settings() { if (isset($_POST['gmail_smtp_send_test_email'])) { $to = ''; if (isset($_POST['gmail_smtp_to_email']) && !empty($_POST['gmail_smtp_to_email'])) { $to = sanitize_text_field($_POST['gmail_smtp_to_email']); } $subject = ''; if (isset($_POST['gmail_smtp_email_subject']) && !empty($_POST['gmail_smtp_email_subject'])) { $subject = sanitize_text_field($_POST['gmail_smtp_email_subject']); } $message = ''; if (isset($_POST['gmail_smtp_email_body']) && !empty($_POST['gmail_smtp_email_body'])) { $message = sanitize_text_field($_POST['gmail_smtp_email_body']); } $options = gmail_smtp_get_option(); $mail = new PHPMailerOAuth(); /* this must be the custom class we created */ // Tell PHPMailer to use SMTP $mail->isSMTP(); // Enable SMTP debugging $mail->SMTPDebug = 4; // Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; // Set AuthType $mail->AuthType = 'XOAUTH2'; // Whether to use SMTP authentication $mail->SMTPAuth = true; // Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = $options['type_of_encryption']; // Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; // Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = $options['smtp_port']; // User Email to use for SMTP authentication - Use the same Email used in Google Developer Console $mail->oauthUserEmail = $options['oauth_user_email']; //Obtained From Google Developer Console $mail->oauthClientId = $options['oauth_client_id']; //Obtained From Google Developer Console $mail->oauthClientSecret = $options['oauth_client_secret']; $gmail_token = json_decode($options['oauth_access_token'], true); //Obtained By running get_oauth_token.php after setting up APP in Google Developer Console. //Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php // eg: http://localhost/phpmail/get_oauth_token.php $mail->oauthRefreshToken = $gmail_token['refresh_token']; //Set who the message is to be sent from $mail->setFrom($options['from_email'], $options['from_name']); //Set an alternative reply-to address //$mail->addReplyTo('*****@*****.**', 'James Scott'); //Set who the message is to be sent to $mail->addAddress($to); //Set the subject line $mail->Subject = $subject; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body //$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); $mail->Body = $message; $mail->SMTPAutoTLS = false; //send the message, check for errors if (!$mail->send()) { echo __('Mailer Error: ', 'gmail-smtp') . $mail->ErrorInfo; } else { echo __('Message sent!', 'gmail-smtp'); } } ?> <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?> "> <?php wp_nonce_field('gmail_smtp_test_email'); ?> <table class="form-table"> <tbody> <tr valign="top"> <th scope="row"><label for="gmail_smtp_to_email"><?php _e('To', 'gmail-smtp'); ?> </label></th> <td><input name="gmail_smtp_to_email" type="text" id="gmail_smtp_to_email" value="" class="regular-text"> <p class="description"><?php _e('Email address of the recipient', 'gmail-smtp'); ?> </p></td> </tr> <tr valign="top"> <th scope="row"><label for="gmail_smtp_email_subject"><?php _e('Subject', 'gmail-smtp'); ?> </label></th> <td><input name="gmail_smtp_email_subject" type="text" id="gmail_smtp_email_subject" value="" class="regular-text"> <p class="description"><?php _e('Subject of the email', 'gmail-smtp'); ?> </p></td> </tr> <tr valign="top"> <th scope="row"><label for="gmail_smtp_email_body"><?php _e('Message', 'gmail-smtp'); ?> </label></th> <td><textarea name="gmail_smtp_email_body" id="gmail_smtp_email_body" rows="6"></textarea> <p class="description"><?php _e('Email body', 'gmail-smtp'); ?> </p></td> </tr> </tbody> </table> <p class="submit"><input type="submit" name="gmail_smtp_send_test_email" id="gmail_smtp_send_test_email" class="button button-primary" value="<?php _e('Send Email', 'gmail-smtp'); ?> "></p> </form> <?php }