nhance/app/Libraries/GmailAPI.php
2024-09-24 14:06:43 +05:30

181 lines
7.0 KiB
PHP
Executable File

<?php
namespace App\Libraries;
use Google\Client;
use Google\Service\Gmail;
use Google\Service\Gmail\Message;
use App\Models\SettingsModel;
use CodeIgniter\CLI\CLI;
class GmailAPI
{
protected $client;
protected $service;
protected $tokenField = 'email_token';
protected $myLogger = '';
protected $authField = 'gmail_api_oauth_credentials';
public function __construct()
{
$this->myLogger = \Config\Services::mylogger();
$this->client = new Client();
$this->client->setApplicationName('Nhance GmailAPI');
$this->client->setScopes([
'https://www.googleapis.com/auth/gmail.addons.current.message.readonly',
'https://www.googleapis.com/auth/gmail.readonly',
'https://www.googleapis.com/auth/gmail.labels',
'https://www.googleapis.com/auth/gmail.send'
]);
$this->setAuthConfig();
//die();
$this->client->setAccessType('offline');
$this->client->setPrompt('select_account consent');
if (php_sapi_name() !== 'cli' || (uri_string() == 'cli/send_mail_cli' || uri_string() == 'cli/processjob') )
{
$this->setTokenFromDB(); // Call only if not in CLI
}
$this->service = new Gmail($this->client);
}
protected function setAuthConfig()
{
$settingsModel = new SettingsModel();
$credentials = $settingsModel->where('id', 1)->first();
if (!$credentials) {
$this->myLogger->logme('error','OAuth credentials for GmailAPI not found in database.');
throw new \Exception('OAuth credentials for GmailAPI not found in database.');
}
// print_r($credentials[$this->authField]);//die();
// $accessToken = $credentials[$this->authField];
$this->client->setAuthConfig(json_decode($credentials[$this->authField],true));
// if ($this->client->isAccessTokenExpired()) {
// if ($this->client->getRefreshToken()) {
// $this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
// $this->storeTokenToDB($this->client->getAccessToken());
// } else {
// $this->myLogger->logme('error', 'Access token expired and no refresh token available.');
// throw new \Exception('Access token expired and no refresh token available.');
// }
// }
}
protected function setTokenFromDB()
{
$settingsModel = new SettingsModel();
$token = $settingsModel->where('id', 1)->first();
// dd(is_array($token) && is_null($token[$this->tokenField]));
if (is_array($token) && is_null($token[$this->tokenField])) {
$this->myLogger->logme('error','Email Token not found in database.');
throw new \Exception('Email Token not found in database.');
}
$accessToken = json_decode($token[$this->tokenField], true);
$this->client->setAccessToken($accessToken);
if ($this->client->isAccessTokenExpired()) {
if ($this->client->getRefreshToken()) {
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
$this->storeTokenToDB($this->client->getAccessToken());
} else {
$this->myLogger->logme('error', 'Access token expired and no refresh token available.');
throw new \Exception('Access token expired and no refresh token available.');
}
}
}
protected function storeTokenToDB($token)
{
$settingsModel = new SettingsModel();
$token = json_encode($token);
// $settingsModel->update(['id' => $this->tokenField], ['value' => json_encode($token)]);
$settingsModel->where('id', 1)->set([$this->tokenField => $token])->update();
}
public function createMessage($to, $subject, $htmlContent, $from, $replyTo, $cc = [], $bcc = [])
{
$boundary = uniqid(rand(), true);
$rawMessageString = "From: $from\r\n";
$rawMessageString .= "To: $to\r\n";
$rawMessageString .= "Reply-To: $replyTo\r\n";
if (!empty($cc)) {
$rawMessageString .= "Cc: " . implode(',', $cc) . "\r\n";
}
if (!empty($bcc)) {
$rawMessageString .= "Bcc: " . implode(',', $bcc) . "\r\n";
}
$rawMessageString .= "Subject: $subject\r\n";
$rawMessageString .= "MIME-Version: 1.0\r\n";
$rawMessageString .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n\r\n";
$rawMessageString .= "--$boundary\r\n";
$rawMessageString .= "Content-Type: text/html; charset=UTF-8\r\n";
$rawMessageString .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$rawMessageString .= $htmlContent . "\r\n";
$rawMessageString .= "--$boundary--";
$rawMessage = base64_encode($rawMessageString);
$rawMessage = str_replace(['+', '/', '='], ['-', '_', ''], $rawMessage);
$message = new Message();
$message->setRaw($rawMessage);
return $message;
}
public function sendMessage($to, $subject, $htmlContent, $from, $replyTo, $cc = [], $bcc = [])
{
try {
$message = $this->createMessage($to, $subject, $htmlContent, $from, $replyTo, $cc, $bcc);
$res = $this->service->users_messages->send('me', $message);
return array('status' => true, 'data' => $res);
} catch (\Exception $e) {
$err_msg = 'An error occurred while sending the mail: ' . $e->getMessage();
$this->myLogger->logme('error', $err_msg);
// throw new \Exception('An error occurred while sending the mail: ' . $e->getMessage());
return array('status' => false, 'data' =>$e->getMessage());
}
}
public function generateNewToken()
{
// dd('called');
if (!is_cli()) {
throw new \RuntimeException('This method can only be accessed via the command line.');
}
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
$authUrl = $this->client->createAuthUrl();
CLI::write('Open the following link in your browser');
CLI::write("\n");
CLI::write($authUrl);
// printf ("Open the following link in your browser:\n%s\n", $authUrl);
CLI::write('Enter verification code: ');
// print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $this->client->fetchAccessTokenWithAuthCode($authCode);
if (array_key_exists('error', $accessToken)) {
$this->myLogger->logme('error', 'Error in getting access token from authCode' . join(', ', $accessToken));
throw new \Exception(join(', ', $accessToken));
}
$this->storeTokenToDB($accessToken);
$this->myLogger->logme('error', 'New token generated and stored in the database.');
print 'New token generated and stored in the database.';
}
}