0

I need a cron to run in my shared host which will scan my gmail account every 30 minutes and will reply all unread email using PHP

First I downloaded the apiclient with composer -> { "require": { "google/apiclient": "1.*" } } . Then I create the service account(https://console.developers.google.com/apis/credentials/serviceaccountkey?project=xxxxxx) and get a json file which has these values :

"type": "service_account",
  "project_id": "xxxxxx",
  "private_key_id": "xyxyxyxyyxyxy",
  "private_key": "-----BEGIN PRIVATE KEY-----\ndddddddddddddddmr\n-----END PRIVATE KEY-----\n",
  "client_email": "yy-yyy@zzzz-zzzzzz.iam.gserviceaccount.com",
  "client_id": "11763887676776474",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxx%40zzz-zzzzzz.iam.gserviceaccount.com"

Also made sure gmail api is enabled(from). Now I wondering How I connect with gmail and just read the un-read email using php...

I was modifying a example code but stuck here($apiKey =)...

<?php
include_once "vendor/google/templates/base.php";
echo pageHeader("Simple API Access");

require_once realpath(dirname(__FILE__) . '/vendor/google/apiclient/src/Google/autoload.php');

$client = new Google_Client();
$client->setApplicationName("Gmail access test");
$apiKey = "<YOUR_API_KEY>"; // WHICH VALUE i SHOULD PUT HERE????.

// Warn if the API key isn't changed.
if (strpos($apiKey, "<") !== false) {
    echo missingApiKeyWarning();
    exit;
}
$client->setDeveloperKey($apiKey);

$service = new Google_Service_Books($client);
...
...
...

UPDATE : my updated code looks like this

<?php
require_once realpath(dirname(__FILE__) . '/vendor/google/apiclient/src/Google/autoload.php');

session_start();

$scopes = array(
    Google_Service_Gmail::GMAIL_READONLY,
    Google_Service_Gmail::GMAIL_COMPOSE,
    Google_Service_Gmail::GMAIL_SEND,
    Google_Service_Gmail::MAIL_GOOGLE_COM,
);

$client = new Google_Client();
$arDetails = $client->loadServiceAccountJson('99b15f1f2326.json', $scopes);

$client->setAssertionCredentials($arDetails);
$client->setScopes($scopes);

$fl = $client->getAuth()->isAccessTokenExpired() ;

if($fl) {
    $client->getAuth()->refreshTokenWithAssertion($arDetails);
}

$client->setAccessType('offline');
$gmailService = new Google_Service_Gmail($client);


#$users = $gmailService->users;
$users = $gmailService->users_messages;

$optParams['maxResults'] = 5; // Return Only 5 Messages
$optParams['labelIds'] = 'INBOX'; // Only show messages in Inbox
$messages     = $gmailService->users_messages->listUsersMessages('me',$optParams);

echo '<br/><pre>';
print_r($users);
print_r($messages);

----
---
---

But I am getting error -

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=5&labelIds=INBOX: (400) Bad Request' in /home3/shafico1/public_html/imsdev/frontend/web/atiq/vendor/google/apiclient/src/Google/Http/REST.php:110
Stack tra
Atiqur
  • 3,802
  • 1
  • 25
  • 26
  • Have a look at this question and the answers: http://stackoverflow.com/questions/24503483/reading-messages-from-gmail-in-php-using-gmail-api It will show you some good approaches. – peter_the_oak Mar 01 '16 at 12:50
  • I tried but there fatal error "Uncaught exception 'Google_Exception' with message 'Invalid client secret JSON file....'" – Atiqur Mar 01 '16 at 13:32

0 Answers0