Mark email as read using microsoft graph api - php

I am writing a script that will use Microsoft Graph api ( using this library https://github.com/microsoftgraph/msgraph-sdk-php )
I managed to connect and search for the specific email, download the attachment but now I need to mark the email as read and set the flag but I have no idea how.
So far I have used this tutorial ( https://learn.microsoft.com/en-us/graph/tutorials/php?tabs=aad ) in order to connect and read the emails.
public static function getInbox() {
$token = GraphHelper::getUserToken();
GraphHelper::$userClient->setAccessToken($token);
// Only request specific properties
$select = '$select=from,isRead,receivedDateTime,subject,hasAttachments';
// Sort by received time, newest first
$orderBy = '$orderBy=receivedDateTime DESC';
$filter = '$filter=isRead eq false';
$requestUrl = '/me/mailFolders/inbox/messages?'.$filter.'&'.$select.'&'.$orderBy;
$messages = GraphHelper::$userClient->createCollectionRequest('GET', $requestUrl)
->setReturnType(Model\Message::class)
->setPageSize(100)
->getPage();
foreach ($messages as $message) {
if(strpos($message->getSubject(), 'XML')!==false ){
print('Message: '.$message->getSubject().PHP_EOL);echo PHP_EOL;
$expand="microsoft.graph.itemattachment/item";
$requestUrl = '/me/messages/'.$message->getId().'/attachments/?$expand= '.$expand;
$docDatas = GraphHelper::$userClient->createCollectionRequest('GET', $requestUrl)
->setReturnType(Model\Message::class)
->setPageSize(1)
->getPage();
$dat = $docDatas[0]->getProperties();
//parseXmlOrder(base64_decode($dat['contentBytes']));
$sendBody = array( 'isRead' => true );
var_dump( GraphHelper::$userClient->createRequest('PATCH', '/me/messages/'.$message->getId())
->attachBody($sendBody)
->execute() );
}
}
}
This is the code I have at the moment. Right at the end of the function I am trying to set the isRead attribute.
If someone could give me some advice where I am going wrong that would be amazing and help me stop banging my head against the wall.
Thanks,

Turns out giving readwrite persmissions does help. I only had read permissions.

Related

Run a files list on a list of google drive accounts programmatically

I'm currently doing a PHP script on a web app to scrap my drive :
By opening a PHP in my browser, I have a script based on examples provided by Google (https://github.com/google/google-api-php-client).
This script simply proceeds to a Files List and copy the files on the server.
It's working fine, but I want my Drive files to be synchronized with my server files. I have to run my PHP in my browser every time I want to copy again my files. Here are my questions about that :
Could I make a PHP script which would be executed programmatically by CRON everyday ?
At the moment the PHP opened in my browser needs an authentication (If no cookie of Google account already signed in is detected, I first need to login). I'd like to authorize my Google App once on my account to be able to scrap the drive every day.
Can I apply this script on multiple Google Accounts (Like having a list of G adresses or tokens, and the script runs for each one). The web app I'm doing is for a dizain of persons, and I'd like to synchronize their drives files with my server.
Thank you in advance for any answer. I don't know if this is very useful, but here is my code :
function retrieveAllFiles($service, $parameters) {
$results = array();
$pageToken = NULL;
do{
try {
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$results = array_merge($results, $files->getFiles());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "Une erreur s'est produite : " . $e->getMessage();
$pageToken = NULL;
}
}while($pageToken);
return $results;
}
// Function to get a list of files id
function scrapDrive($service, $excluded) {
$final_files = array();
$query = "";
if(sizeof($excluded) == 0){
$query = "name = '--------'";
}else{
$query = "name = '".$excluded[0]."'";
}
// I just do a custom query if the user has chosen to exclude personal folders from his drive
foreach ($excluded as $folder_name) {
$query .= " or name ='".$folder_name."'";
}
// Get folders ID
$excluded_folders = retrieveAllFiles($service, array('q' => $query));
$excluded_folders_id = array();
foreach ($excluded_folders as $folder) {
array_push($excluded_folders_id, $folder['id']);
}
$usermail = getUserMail($service);
// Getting only files from last month
$artworks_query = "modifiedTime > '".date("Y-m-d\TH:i:sP",strtotime("-1 month"))."'";
// Only getting images
$artworks_query .= " and mimeType contains 'image/' and '".$usermail."' in owners";
foreach ($excluded_folders_id as $id) {
$artworks_query .= " and not '".$id."' in parents";
}
$artworks = retrieveAllFiles($service, array('q' => $artworks_query));
foreach ($artworks as $artwork) {
$final_artworks[$artwork['id']] = $artwork['name'];
}
return $final_artworks;
}
$excluded = getExcludedFolders($_SESSION['id']);
$artworks = scrapDrive($service, $excluded);
$nb_of_artworks = sizeof($artworks);
$i = 1;
// Copy each file
foreach ($artworks as $artwork_id => $artwork_name) {
$response = $service->files->get($artwork_id, array(
'alt' => 'media' ));
$content = $response->getBody()->getContents();
file_put_contents("folder/".$artwork_id.'_'.$artwork_name, $content);
}
$i++;
}
Thank you in advance for any help ! :-)
Could I make a PHP script which would be executed programmatically by CRON everyday?
Yes you can run a php script in cron. how to run a php script daily with the cron job on Ubuntu os
At the moment the PHP opened in my browser needs an authentication (If no cookie of Google account already signed in is detected, I first need to login).
That is because you are using Oauth2. You could just as easily save your refresh token in a file and then have your script read the refresh token from the file rather than reading it from the cookie. check this
I'd like to authorize my Google App once on my account to be able to scrap the drive every day.
If this is an account that you personally have control of then you should consider using service account authentication rather than Oauth2. check this
Can I apply this script on multiple Google Accounts (Like having a list of G adresses or tokens, and the script runs for each one). The web app I'm doing is for a dizain of persons, and I'd like to synchronize their drives files with my server.
Using Oauth2 you can have a refresh token for as many accounts as you wish. The owner of the account must simply authenticate your application and you will have a refresh token to access it.

Problems with replacing CiviCRM tokens in API

I'm currently developing a CiviCRM extension, where I need to replace CiviCRM-Tokens (used in pdf and mailing generation) in html code.
I did a little bit of research in the core files and tried to recreate the behaviour in the PDFLetterCommon.php (/civicrm/CRM/Contact/Form/Task/PDFLetterCommon.php) where it replaces the tokens in the postProcess function.
Here is the original CiviCRM Code:
list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form);
$skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
$skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
foreach ($form->_contactIds as $item => $contactId) {
$params = array('contact_id' => $contactId);
list($contact) = CRM_Utils_Token::getTokenDetails($params,
$returnProperties,
$skipOnHold,
$skipDeceased,
NULL,
$messageToken,
'CRM_Contact_Form_Task_PDFLetterCommon'
);
...
}
And here is my version for testing:
(this code is located inside an api function in my extension)
$messageToken = CRM_Utils_Token::getTokens($params["html"]);
$returnProperties = array();
if (isset($messageToken['contact'])) {
foreach ($messageToken['contact'] as $key => $value) {
$returnProperties[$value] = 1;
}
}
$skipOnHold = FALSE;
$skipDeceased = TRUE;
$tokenParams = array("contact_id" => 67450);
list($contact) = CRM_Utils_Token::getTokenDetails($tokenParams,
$returnProperties,
$skipOnHold,
$skipDeceased,
NULL,
$messageToken,
'CRM_Contact_Form_Task_PDFLetterCommon'
);
I'm using the default values for $skipOnHold (false) and $skipDeceased (true) and also just passing one (existing) user id into the $params array ($tokenParams in my code).
Here is my problem:
My $messageToken and $returnProperties variables are being filled correctly via CiviCRM's core functions but when I pass them all into CRM_Utils_Token::getTokenDetails() the returned $contact variable holds an empty array.
I'm really out of ideas, I've been looking into CRM/Utils/Token.php where getTokenDetails() is located, but have been unsuccessful in finding the problem with my code.
Thanks in advance for any help!

Yii::app()->lang doesn't work sometimes with LimeSurvey

I am working on a custom script to automatically send out invites and reminders. I have everything working fine up until a point. My function to send invites looks like this:
function sendInvites($iSurveyID) {
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey)) {
die("could not load survey");
}
if(!tableExists("{{tokens_$iSurveyID}}")) {
die("survey has no tokens or something");
}
$SQLemailstatuscondition = "emailstatus = 'OK'";
$SQLremindercountcondition = '';
$SQLreminderdelaycondition = '';
$iMaxEmails = (int)Yii::app()->getConfig("maxemails");
$iMaxReminders = 1;
if(!is_null($iMaxReminders)) {
$SQLremindercountcondition = "remindercount < " . $iMaxReminders;
}
$oTokens = Tokens_dynamic::model($iSurveyID);
$aResultTokens = $oTokens->findUninvited(false, $iMaxEmails, true, $SQLemailstatuscondition, $SQLremindercountcondition, $SQLreminderdelaycondition);
if (empty($aResultTokens)) {
die("No tokens to send invites to");
}
$aResult = emailTokens($iSurveyID, $aResultTokens, 'invite');
}
I also have a simple little file that starts up Yii:
Yii::createApplication('LSYii_Application', APPPATH . 'config/config' . EXT);
Yii::app()->loadHelper('admin/token');
Yii::app()->loadHelper('common');
Everything works as expected up until I actually try to send emails to the tokens. I've tracked the problem down to the following, on of the functions called by emailTokens has this in it:
$clang = Yii::app()->lang;
$aBasicTokenFields=array('firstname'=>array(
'description'=>$clang->gT('First name'),
'mandatory'=>'N',
'showregister'=>'Y'
),
The Yii::app()->lang part seems to be causing issues because then php is unable to call the gT method. However, when LimeSurvey is running "properly" this never happens. I can't even seem to find where "lang" is in the LimeSurvey source.
What can I do to make it work?
Why do you make it so hard on yourself and not use the RemoteControl2 API ?
See http://manual.limesurvey.org/wiki/RemoteControl_2_API#invite_participants
On that page you will also find a PHP example script.
maybe
Yii::import('application.libraries.Limesurvey_lang');
$clang = new Limesurvey_lang($oTokens->language);

Yahoo Placefinder Geocoding randomly stopped working... Are they not supporting it now?

I have a web app that uses Yahoo Geocoding Placefinder API and it was working perfectly until earlier today. It just randomly quit working. Are they not supporting it anymore? I have looked all over the net and I can't find anything about them dropping support, but my code no longer works. Here's my code...
function geocode_yahoo($address,$city,$state,$country) {
$address = array($address, $city, $state, $country);
$address = array_filter($address);
$address = urlencode(implode(', ', $address));
$appid = 'CYxSRa64';
$url = 'http://where.yahooapis.com/geocode?location='.$address.'&flags=J&appid='.$appid;
$data = file_get_contents($url);
if ($data != '') {
$data = json_decode($data);
if ($data && $data->ResultSet && $data->ResultSet->Error == '0' && $data->ResultSet->Found) {
return (object) array('lat'=>$data->ResultSet->Results[0]->latitude, 'lng'=>$data->ResultSet->Results[0]->longitude);
}
}
return false;
}
Nothing wrong with your code, the service has simply been stopped.
Note that http://where.yahooapis.com is now returning a 404 page.
The service was set to retire at the end of November 2012 but was left running in order to provide time for developers to migrate to the replacement Yahoo BOSS services: http://developer.yahoo.com/boss/geo/
There is nothing here that states that the service will be stopped: https://developer.yahoo.com/boss/geo/docs/free_YQL.html#table_pf
In fact, there is a link on the bottom of this page: https://developer.yahoo.com/boss/geo/ to the free YQL version.

PHP iterate multidimensional array - query web service every x seconds

I have an array with user information and a web service on a site I can query for the status of a user (online/offline). What I would like to do is query the site every x seconds for the status of each user.
There are about 10 users and belwois an example of the array. I can change the array is needed. Only thing I need to enter manually is the username and full name. The "status" I can call from the server.
$users = array
(
"username"=>array("Fullname","Status"),
"johndoe"=>array("John Doe","Online"),
"janedoe"=>array("Jane Doe","Offline")
);
This is an example of the url I can use to query the site (the query returns only the users status (Online or Offline):
http://thesite.com:80/webservice/user/username/
This is the code I can use to get a specific user status:
$url = 'http://thesite.com:80/webservice/user/johndoe/';
$get = fopen($url, "r");
if ($get) {
while (!feof($get)) {
$state = fgets($get, 4096);
}
fclose($get);
}
echo "User johndoe is: ".$status;
// Output: User johndoe is: Online
Now I only need help with iterating through the users and site every x seconds and update the array with each user status in the last array field for the user.
Please note that below I use php and fopen as this is a cross-domain get function and I could not get ajax/jquery to work. I do not have the option to modify the webservice server.
Thanks :)
You need to create a cronjob script that runs every x seconds. That script should be an asynchronous request to this PHP function.
public function updateUsers(){
$users = $_SESSION['users'];
foreach($users as $username=>$data) {
$url = 'http://thesite.com:80/webservice/user/'.$username.'/';
$get = fopen($url, "r");
if ($get) {
while (!feof($get)) {
$status = fgets($get, 4096);
}
fclose($get);
}
$users[$username][] = $status;
}
$_SESSION['users'] = $users;
}
A guide for posting asynchronous requests . http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html
Hope it helps :)
If your $users array don't changes, you can do this:
foreach($users as $username=>$userdata) {
$url = 'http://thesite.com:80/webservice/user/'.$username.'/';
$get = fopen($url, "r");
if ($get) {
while (!feof($get)) {
$state = fgets($get, 4096);
}
fclose($get);
}
$users[$username][1] = $state;
}
If you can change your $users array to be associative like this:
$users = array(
"username"=>array("fullname"=>"Fullname","status"=>"Status"),
"johndoe"=>array("fullname"=>"John Doe","status"=>"Online"),
"janedoe"=>array("fullname"=>"Jane Doe","status"=>"Offline")
);
That would let you use more key/values and a bit safer.

Categories