Can't Send PHPMailer with Oauth Google

I have some problem. My code

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$userEmail = "[email protected]";
$clientId = "xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
$clientSecret = "xxxxxxxx-xxxxxxxxx";
$token = "1/xxxxxxxx-xxxxxxxx-5EmWcSmuvnRbJs";

$mailer = new PHPMailerOAuth;
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->AuthType = 'XOAUTH2';
$mailer->oauthUserEmail = $userEmail;
$mailer->oauthClientId = $clientId;
$mailer->oauthClientSecret = $clientSecret;
$mailer->oauthRefreshToken = $token;
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;

$mailer->setFrom("[email protected]");
$mailer->addAddress("[email protected]");

$mailer->Subject = "Subject";
$mailer->Body = "Message";

if (! $mailer->send())
    throw new RuntimeException('Mail submission failed! ' . $mailer->ErrorInfo);

This eror

Fatal error: Uncaught Error: Class 'League\OAuth2\Client\Provider\Google' not found in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php:54 Stack trace: #0 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(67): PHPMailerOAuthGoogle->getProvider() #1 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(74): PHPMailerOAuthGoogle->getToken() #2 /opt/lampp/htdocs/mailer/PHPMailer/class.smtp.php(470): PHPMailerOAuthGoogle->getOauth64() #3 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauth.php(174): SMTP->authenticate('', '', 'XOAUTH2', '', '', Object(PHPMailerOAuthGoogle)) #4 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1540): PHPMailerOAuth->smtpConnect(Array) #5 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1335): PHPMailer->smtpSend('Date: Thu, 3 Au...', 'Message\n') #6 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1213): PHPMailer->postSend() #7 /opt/lampp/htdocs/mailer/index.php(27): PHPMailer->send() #8 {main} thrown in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php on line 54

i don't know what my mistake, i just want to create mail with PHPmailer using google smtp with Oauth2.0, I've searched on google but I have not found the solution yet

Upvotes: 2

Views: 2039

Answers (1)

Synchro
Synchro

Reputation: 37730

Look at PHPMailer's composer.json file - it includes the OAuth class as a suggestion, because it's not a requirement, and won't work on older PHP versions that PHPMailer supports, so it can't be enabled by default.

You need to add it to your own composer.json file (the same one you load PHPMailer with).

I've added comments to this effect to the Gmail OAuth2 tutorial on the PHPMailer wiki.

While I'm here, I suggest you don't implement this using PHPMailer 5.2, but go directly to PHPMailer 6.0 which has much better support for OAuth2.

Upvotes: 2

Related Questions