I am using Xampp and I wanted to integrate sending Gmail over PHP. I see that everybody recommend PHP Mailer so I download Composer and install phpmailer composer require phpmailer/phpmailer
. After that i install google oauth2: composer require league/oauth2-google
.
I look at this link to use : Authorization Code Flow.
Google API is all set up (i got APP-id and Secret)
when i run get_oauth_token.php
i get error:
Uncaught Error: Class 'League\OAuth2\Client\Provider\Google' not found in C:\xampp\htdocs\telesales\gentelella-master\production\PHPMailer\get_oauth_token.php
my composer.json is this:
{
"require": {
"phpmailer/phpmailer": "^6.0",
"league/oauth2-client": "^2.3",
"stevenmaguire/oauth2-microsoft": "^2.2"
}
}
Sorry, but I am new at this and cannot find why .php page wont find Class.
Here is get_oauth_token.php
<?php
$provider = new League\OAuth2\Client\Provider\Google([
'clientId' => '{google-app-id}',
'clientSecret' => '{google-app-secret}',
'redirectUri' => 'https://example.com/callback-url',
'hostedDomain' => 'example.com', // optional; used to restrict access to users on your G Suite/Google Apps for Business accounts
]);
if (!empty($_GET['error'])) {
// Got an error, probably user denied access
exit('Got error: ' . htmlspecialchars($_GET['error'], ENT_QUOTES, 'UTF-8'));
} elseif (empty($_GET['code'])) {
// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
exit;
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
// State is invalid, possible CSRF attack in progress
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// Optional: Now you have a token you can look up a users profile data
try {
// We got an access token, let's now get the owner details
$ownerDetails = $provider->getResourceOwner($token);
// Use these details to create a new profile
printf('Hello %s!', $ownerDetails->getFirstName());
} catch (Exception $e) {
// Failed to get user details
exit('Something went wrong: ' . $e->getMessage());
}
// Use this to interact with an API on the users behalf
echo $token->getToken();
// Use this to get a new access token if the old one expires
echo $token->getRefreshToken();
// Number of seconds until the access token will expire, and need refreshing
echo $token->getExpires();
}
?>
I have just making the same but without composer. First it's not working with some errors. While I am searching the solution for troubleshooting, and try to edit some lines, finally it's working now. with this result :
2018-05-27 02:41:44 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:44 CLIENT -> SERVER: EHLO mybasic.local
2018-05-27 02:41:44 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.213.130.29]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-05-27 02:41:44 CLIENT -> SERVER: STARTTLS
2018-05-27 02:41:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2018-05-27 02:41:45 CLIENT -> SERVER: EHLO myweb.local
2018-05-27 02:41:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.213.130.29]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-05-27 02:41:45 CLIENT -> SERVER: AUTH LOGIN
2018-05-27 02:41:45 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-05-27 02:41:45 CLIENT -> SERVER: <credentials hidden>
2018-05-27 02:41:45 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-05-27 02:41:45 CLIENT -> SERVER: <credentials hidden>
2018-05-27 02:41:46 SERVER -> CLIENT: 235 2.7.0 Accepted
2018-05-27 02:41:46 CLIENT -> SERVER: MAIL FROM:<[email protected]>
2018-05-27 02:41:46 SERVER -> CLIENT: 250 2.1.0 OK q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: RCPT TO:<[email protected]>
2018-05-27 02:41:46 SERVER -> CLIENT: 250 2.1.5 OK q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: DATA
2018-05-27 02:41:46 SERVER -> CLIENT: 354 Go ahead q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: Date: Sun, 27 May 2018 04:41:44 +0200
2018-05-27 02:41:46 CLIENT -> SERVER: To: Test System <[email protected]>
2018-05-27 02:41:46 CLIENT -> SERVER: From: Test System <[email protected]>
2018-05-27 02:41:46 CLIENT -> SERVER: Subject: Here is the subject
2018-05-27 02:41:46 CLIENT -> SERVER: Message-ID: <[email protected]>
2018-05-27 02:41:46 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
2018-05-27 02:41:46 CLIENT -> SERVER: MIME-Version: 1.0
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: multipart/alternative;
2018-05-27 02:41:46 CLIENT -> SERVER: boundary="b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk"
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is a multi-part message in MIME format.
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is the body in plain text for non-HTML mail clients
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is the HTML message body <b>in bold!</b>
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk--
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: .
2018-05-27 02:41:48 SERVER -> CLIENT: 250 2.0.0 OK 1527388939 q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:48 CLIENT -> SERVER: QUIT
2018-05-27 02:41:48 SERVER -> CLIENT: 221 2.0.0 closing connection q126-v6sm37491953pga.79 - gsmtp
Message has been sent
My code :
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php'; // make sure the folder is right
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
//Load Composer's autoloader
//require 'vendor/autoload.php'; // if using composer
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'tsl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('[email protected]', 'Test System');
$mail->addAddress('[email protected]', 'Test System');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I also disabled/comment all lines related to email on php.ini in the folder xampp/php and reload the xampp.
Get the latest code from : https://github.com/PHPMailer/PHPMailer
Hope it will help.