2019-11-21 15:07:44 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace ProcessMaker\GmailOAuth;
|
|
|
|
|
|
|
|
|
|
use Google_Client;
|
|
|
|
|
use Google_Service_Gmail;
|
|
|
|
|
use Google_Service_Gmail_Message;
|
2021-04-29 12:32:54 -04:00
|
|
|
use ProcessMaker\EmailOAuth\EmailBase;
|
2019-11-21 15:07:44 -04:00
|
|
|
|
|
|
|
|
class GmailOAuth
|
|
|
|
|
{
|
|
|
|
|
|
2021-04-29 12:32:54 -04:00
|
|
|
use EmailBase;
|
2019-11-21 15:07:44 -04:00
|
|
|
|
|
|
|
|
/**
|
2021-04-29 12:32:54 -04:00
|
|
|
* Constructor of the class.
|
2019-11-21 15:07:44 -04:00
|
|
|
*/
|
2021-04-29 12:32:54 -04:00
|
|
|
public function __construct()
|
2019-11-21 15:07:44 -04:00
|
|
|
{
|
2021-04-29 12:32:54 -04:00
|
|
|
$this->setServer("smtp.gmail.com");
|
|
|
|
|
$this->setPort(587);
|
2019-11-21 15:07:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a Google_Client object, this may vary depending on the service provider.
|
|
|
|
|
* @return Google_Client
|
|
|
|
|
*/
|
|
|
|
|
public function getGoogleClient(): Google_Client
|
|
|
|
|
{
|
|
|
|
|
$googleClient = new Google_Client();
|
|
|
|
|
$googleClient->setClientId($this->clientID);
|
|
|
|
|
$googleClient->setClientSecret($this->clientSecret);
|
|
|
|
|
$googleClient->setRedirectUri($this->redirectURI);
|
|
|
|
|
$googleClient->setAccessType('offline');
|
|
|
|
|
$googleClient->setApprovalPrompt('force');
|
|
|
|
|
$googleClient->addScope(Google_Service_Gmail::MAIL_GOOGLE_COM);
|
|
|
|
|
return $googleClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This sends a test email with Google_Service_Gmail_Message object, as long
|
|
|
|
|
* as the test flag is activated.
|
|
|
|
|
* @return Google_Service_Gmail_Message
|
|
|
|
|
*/
|
|
|
|
|
public function sendTestEmailWithGoogleServiceGmail(): Google_Service_Gmail_Message
|
|
|
|
|
{
|
|
|
|
|
$googleServiceGmailMessage = new Google_Service_Gmail_Message();
|
|
|
|
|
if (!filter_var($this->fromAccount, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
|
return $googleServiceGmailMessage;
|
|
|
|
|
}
|
|
|
|
|
if (!filter_var($this->mailTo, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
|
return $googleServiceGmailMessage;
|
|
|
|
|
}
|
|
|
|
|
if ($this->sendTestMail === 0) {
|
|
|
|
|
return $googleServiceGmailMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$googleClient = $this->getGoogleClient();
|
|
|
|
|
$googleClient->refreshToken($this->getRefreshToken());
|
|
|
|
|
if ($googleClient->isAccessTokenExpired()) {
|
|
|
|
|
$newAccessToken = $googleClient->getAccessToken();
|
|
|
|
|
$googleClient->setAccessToken($newAccessToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$raw = $this->getRawMessage();
|
|
|
|
|
$googleServiceGmailMessage->setRaw($raw);
|
|
|
|
|
|
|
|
|
|
$service = new Google_Service_Gmail($googleClient);
|
|
|
|
|
$result = $service->users_messages->send("me", $googleServiceGmailMessage);
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|