GmailApi:GWM
This commit is contained in:
parent
6a619ad267
commit
a634f78b53
@ -64,7 +64,8 @@ $routes->get('payment_form', 'PaymentController::payment_form');
|
||||
$routes->post('processPayment', 'PaymentController::processPayment');
|
||||
|
||||
|
||||
$routes->match(['get','post'],'/test/(:any)','Test_controller::index/$1');
|
||||
$routes->match(['get','post'],'/test','Test_controller::sendEmail');
|
||||
$routes->match(['get','post'],'/test2','Test_controller::test2');
|
||||
|
||||
|
||||
|
||||
|
||||
@ -28,6 +28,14 @@ use App\Models\Cart_model;
|
||||
|
||||
use App\Models\Transaction_model;
|
||||
|
||||
use Google_Client;
|
||||
use Google_Service_Gmail;
|
||||
use Google_Service_Gmail_Message;
|
||||
use Google_Service_Gmail_ModifyMessageRequest;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use Google_Service_Gmail_MessagePart;
|
||||
use Google_Service_Gmail_MessagePartHeader;
|
||||
use Google_Service_Gmail_MessagePartBody;
|
||||
class Test_controller extends BaseController
|
||||
|
||||
{
|
||||
@ -72,16 +80,225 @@ class Test_controller extends BaseController
|
||||
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
public function test2()
|
||||
{
|
||||
if($this->request->getVar('code')) {
|
||||
try {
|
||||
$capi = new Google_Client();
|
||||
|
||||
// Get the access token
|
||||
$data = $capi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $this->request->getVar('code'));
|
||||
|
||||
// Save the access token as a session variable
|
||||
$_SESSION['access_token'] = $data['access_token'];
|
||||
|
||||
// Redirect to the page where user can create event
|
||||
header('Location: home.php');
|
||||
exit();
|
||||
}
|
||||
catch(Exception $e) {
|
||||
echo $e->getMessage();
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function sendEmail()
|
||||
{
|
||||
$credentialsPath = 'credentials.json';
|
||||
$tokensPath = 'token.json';
|
||||
$accessToken = $this->getAccessToken($tokensPath); // Retrieve access token from storage
|
||||
$refreshToken = $this->getRefreshToken($tokensPath); // Retrieve refresh token from storage
|
||||
$recipientEmail = 'gowtham.manavalan.la@gmail.com';
|
||||
$subject = 'Test email';
|
||||
$messageBody = 'This is a test email sent using the Gmail API';
|
||||
|
||||
$client = new Google_Client();
|
||||
$client->setAuthConfig($credentialsPath);
|
||||
|
||||
// Set the access token and refresh token
|
||||
// $client->setAccessToken([
|
||||
// 'access_token' => $accessToken,
|
||||
// 'refresh_token' => $refreshToken,
|
||||
// 'expires_in' => 3600, // Expiration time in seconds (adjust if necessary)
|
||||
// 'created' => time()
|
||||
// ]);
|
||||
|
||||
// if ($client->isAccessTokenExpired()) {
|
||||
if (false) {
|
||||
// Refresh the access token if expired
|
||||
$newAccessToken = $this->refreshAccessToken($client, $refreshToken); // Implement your own method to refresh the token
|
||||
$client->setAccessToken([
|
||||
'access_token' => $newAccessToken,
|
||||
'refresh_token' => $refreshToken,
|
||||
'expires_in' => 3600,
|
||||
'created' => time()
|
||||
]);
|
||||
|
||||
// Update the access token in storage if necessary
|
||||
$this->updateAccessToken($newAccessToken, $tokensPath); // Implement your own method to update the token
|
||||
} else {
|
||||
// Request authorization from the user.
|
||||
$authUrl = $client->createAuthUrl();
|
||||
echo "Open the following link in your browser:\n%s\n", $authUrl;
|
||||
print 'Enter verification code: ';
|
||||
$authCode = trim(fgets(STDIN));
|
||||
|
||||
// Exchange authorization code for an access token.
|
||||
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
|
||||
$client->setAccessToken($accessToken);
|
||||
|
||||
// Check to see if there was an error.
|
||||
if (array_key_exists('error', $accessToken)) {
|
||||
throw new Exception(join(', ', $accessToken));
|
||||
}
|
||||
}
|
||||
// Save the token to a file.
|
||||
if (!file_exists(dirname($tokenPath))) {
|
||||
mkdir(dirname($tokensPath), 0700, true);
|
||||
}
|
||||
file_put_contents($tokensPath, json_encode($this->client->getAccessToken()));
|
||||
$service = new Google_Service_Gmail($client);
|
||||
|
||||
$message = $this->createMessage('me', $recipientEmail, $subject, $messageBody);
|
||||
|
||||
$service->users_messages->send('me', $message);
|
||||
|
||||
echo 'Email sent successfully';
|
||||
}
|
||||
|
||||
private function getAccessToken($tokensPath)
|
||||
{
|
||||
$tokens = $this->loadTokens($tokensPath);
|
||||
return $tokens['access_token'] ?? null;
|
||||
}
|
||||
|
||||
private function getRefreshToken($tokensPath)
|
||||
{
|
||||
$tokens = $this->loadTokens($tokensPath);
|
||||
return $tokens['refresh_token'] ?? null;
|
||||
}
|
||||
|
||||
private function refreshAccessToken($client, $refreshToken)
|
||||
{
|
||||
$client->fetchAccessTokenWithRefreshToken($refreshToken);
|
||||
return $client->getAccessToken()['access_token'];
|
||||
}
|
||||
|
||||
private function updateAccessToken($accessToken, $tokensPath)
|
||||
{
|
||||
$tokens = $this->loadTokens($tokensPath);
|
||||
$tokens['access_token'] = $accessToken;
|
||||
$this->saveTokens($tokens, $tokensPath);
|
||||
}
|
||||
|
||||
private function loadTokens($tokensPath)
|
||||
{
|
||||
if (file_exists($tokensPath)) {
|
||||
$tokensData = file_get_contents($tokensPath);
|
||||
return json_decode($tokensData, true);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
private function saveTokens($tokens, $tokensPath)
|
||||
{
|
||||
$tokensData = json_encode($tokens);
|
||||
file_put_contents($tokensPath, $tokensData);
|
||||
}
|
||||
|
||||
private function createMessage($sender, $recipient, $subject, $messageBody)
|
||||
{
|
||||
$message = new Google_Service_Gmail_Message();
|
||||
$email = new Google_Service_Gmail_MessagePartHeader();
|
||||
$email->setName('To');
|
||||
$email->setValue($recipient);
|
||||
$message->setPayload(new Google_Service_Gmail_MessagePart());
|
||||
$message->getPayload()->setHeaders([$email]);
|
||||
|
||||
$body = new Google_Service_Gmail_MessagePartBody();
|
||||
$body->setData(base64_encode($messageBody));
|
||||
$message->getPayload()->setBody($body);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// public function sendEmail()
|
||||
// {
|
||||
// $credentialsPath = 'credentials.json';
|
||||
// $accessToken = 'your-access-token';
|
||||
// $refreshToken = 'your-refresh-token';
|
||||
// $recipientEmail = 'gowtham.manavalan.la@gmail.com';
|
||||
// $subject = 'Test email';
|
||||
// $messageBody = 'This is a test email sent using the Gmail API';
|
||||
|
||||
// $client = new Google_Client();
|
||||
// $client->setAuthConfig($credentialsPath);
|
||||
|
||||
// // Set the access token and refresh token
|
||||
// $client->setAccessToken([
|
||||
// 'access_token' => $accessToken,
|
||||
// 'refresh_token' => $refreshToken,
|
||||
// 'expires_in' => 3600, // Expiration time in seconds (adjust if necessary)
|
||||
// 'created' => time()
|
||||
// ]);
|
||||
|
||||
// if ($client->isAccessTokenExpired()) {
|
||||
// // Refresh the access token if expired
|
||||
// $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
|
||||
// }
|
||||
|
||||
// $service = new Google_Service_Gmail($client);
|
||||
|
||||
// $message = $this->createMessage('me', $recipientEmail, $subject, $messageBody);
|
||||
|
||||
// $service->users_messages->send('me', $message);
|
||||
|
||||
// echo 'Email sent successfully';
|
||||
// }
|
||||
|
||||
// private function createMessage($sender, $recipient, $subject, $messageBody)
|
||||
// {
|
||||
// $message = new Google_Service_Gmail_Message();
|
||||
// $email = new Google_Service_Gmail_MessagePartHeader();
|
||||
// $email->setName('To');
|
||||
// $email->setValue($recipient);
|
||||
// $message->setPayload(new Google_Service_Gmail_MessagePart());
|
||||
// $message->getPayload()->setHeaders([$email]);
|
||||
|
||||
// $body = new Google_Service_Gmail_MessagePartBody();
|
||||
// $body->setData(base64_encode($messageBody));
|
||||
// $message->getPayload()->setBody($body);
|
||||
|
||||
// return $message;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
1
app/Controllers/credentials.json
Normal file
1
app/Controllers/credentials.json
Normal file
@ -0,0 +1 @@
|
||||
{"web":{"client_id":"1091575547896-uksojot6eohd8i2mg7dcga9t0rn430ie.apps.googleusercontent.com","project_id":"titanium-genre-389806","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost/golf_backend/test2"],"javascript_origins":["http://localhost"]}}
|
||||
6
app/Controllers/token.json
Normal file
6
app/Controllers/token.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"access_token": "your-access-token",
|
||||
"refresh_token": "your-refresh-token",
|
||||
"expires_in": 3600,
|
||||
"created": 1678934567
|
||||
}
|
||||
@ -9,6 +9,7 @@
|
||||
"codeigniter4/framework": "^4.0",
|
||||
"daycry/cronjob": "^2.2",
|
||||
"google/apiclient": "^2.15",
|
||||
"guzzlehttp/guzzle": "^7.7",
|
||||
"ramsey/uuid": "^4.2",
|
||||
"square/square": "18.0.0.20220420"
|
||||
},
|
||||
|
||||
2
composer.lock
generated
2
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "5a8d46ecf78556919bd1e0e57fdfba48",
|
||||
"content-hash": "6e8b2f75e98ca3380942cf3f26410005",
|
||||
"packages": [
|
||||
{
|
||||
"name": "apimatic/jsonmapper",
|
||||
|
||||
1
credentials.json
Normal file
1
credentials.json
Normal file
@ -0,0 +1 @@
|
||||
{"web":{"client_id":"649722930931-nrphm6ifgufi609vhlf3sva9m1etllbe.apps.googleusercontent.com","project_id":"golf-evo-smtp","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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"}}
|
||||
BIN
writable.zip
BIN
writable.zip
Binary file not shown.
Loading…
Reference in New Issue
Block a user