PHP code example of o323 / nylas-php

1. Go to this page and download the library: Download o323/nylas-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

o323 / nylas-php example snippets


composer 

$client = new Nylas(CLIENT, SECRET);
$redirect_url = 'http://localhost:8080/login_callback.php';
$get_auth_url = $client->createAuthURL($redirect_url);

// redirect to Nylas auth server
header("Location: ".$get_auth_url);

$access_code = $_GET['code'];
$client = new Nylas(CLIENT, SECRET);
$get_token = $client->getAuthToken($access_code);

// save token in session
$_SESSION['access_token'] = $get_token;

$client = new Nylas(CLIENT, SECRET, TOKEN);
$account = $client->account();

echo $account->email_address;
echo $account->provider;

$client = new Nylas(CLIENT, SECRET, TOKEN);

// Fetch the first thread
$first_thread = $client->threads()->first();
echo $first_thread->id;

// Fetch first 2 latest threads
$two_threads = $client->threads()->all(2);
foreach($two_threads as $thread) {
    echo $thread->id;
}

// List all threads with '[email protected]'
$search_criteria = array("any_email" => "[email protected]");
$get_threads = $client->threads()->where($search_criteria)->items()
foreach($get_threads as $thread) {
    echo $thread->id;
}

// List thread participants
foreach($thead->participants as $participant) {
    echo $participant->email;
    echo $participant->name;
}

// Mark as Read
$thread->markAsRead();

// Mark as Seen
$thread->markAsSeen();

// Archive
$thread->archive();

// Unarchive
$thread->unarchive();

// Trash
$thread->trash();

// Star
$thread->star();

// Unstar
$thread->unstar();

// Add or remove arbitrary tags
$to_add = array('cfa1233ef123acd12');
$to_remove = array('inbox');
$thread->addTags($to_add);
$thread->removeTags($to_remove);

// Listing messages
foreach($thread->messages()->items() as $message) {
    echo $message->subject;
    echo $message->body;
}

$client = new Nylas(CLIENT, SECRET, TOKEN);

$file_path = '/var/my/folder/test_file.pdf';
$upload_resp = $client->files()->create($file_path);
echo $upload_resp->id;

$client = new Nylas(CLIENT, SECRET, TOKEN);

$person_obj = new \Nylas\Models\Person('Kartik Talwar', '[email protected]');
$message_obj = array( "to" => array($person_obj),
                      "subject" => "Hello, PHP!",
                      "body" => "Test <br> message");

$draft = $client->drafts()->create($message_obj);
$send_message = $draft->send();
echo $send_message->id;

$client = new Nylas(CLIENT, SECRET, TOKEN);
$calendars = $client->calendars()->all();

$calendar = null;
foeach($calendars as $i) {
  if(!$i->read_only) {
    $calendar = $i;
  }
}

$person_obj = new \Nylas\Models\Person('Kartik Talwar', '[email protected]');
$calendar_obj = array("title" => "Important Meeting",
                      "location" => "Nylas HQ",
                      "participants" => array($person_obj),
                      "calendar_id" => $calendar->id,
                      "when" => array("start_time" => time(),
                                      "end_time" => time() + (30*60)));
// create event
$event = $client->events()->create($calendar_obj);
echo $event->id;

// update
$event = $event->update(array("location" => "Meeting room #1"));

// delete event
$event->delete();
// delete event (alternate)
$remove = $client->events()->find($event->id)->delete();

$client = new Nylas(CLIENT, SECRET, TOKEN, 'http://localhost:5555/');