PHP code example of sumacrm / nylas-php

1. Go to this page and download the library: Download sumacrm/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/ */

    

sumacrm / nylas-php example snippets


composer 

$OPTIONS =
[
    'app_id'     => 'your app id',
    'app_secret' => 'your app secret',
    'app_server' => 'default https://api.nylas.com/',

    'token'      => 'token',
    'debug'      => 'true or false',
];

$client = new Nylas($OPTIONS);

$redirect_url = 'http://localhost:8080/login_callback.php';

$get_auth_url = $client->oauth($OPTIONS)->createAuthURL($redirect_url);

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

$access_code = $_GET['code'];

$client = new Nylas($OPTIONS);

$get_token = $client->oauth($OPTIONS)->getAuthToken($access_code);

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

// init client!
$client = new Nylas($OPTIONS);

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

echo $first_thread->id;

// Fetch first 2 latest threads
$two_threads = $client->threads($OPTIONS)->all(2);

// Fetch threads from offset 30, and limit 50
$part_threads = $client->threads($OPTIONS)->part(30, 50);

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($OPTIONS)->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;
}

// Unread or Read
$thread->unread($id, $unread = false);

// Move to folder
$thread->move($id, $target_id, $type = 'folder');

// Star
$thread->starred($id, $starred = true);

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

$client = new Nylas($OPTIONS);

$file_path = '/var/my/folder/test_file.pdf';

$upload_resp = $client->files($OPTIONS)->create($file_path);

echo $upload_resp->id;

$client = new Nylas($OPTIONS);

$message =
[
     "to"      =>
     [
         ["name" => "Nylas", "email" => "[email protected]"],
         ["name" => "Goole", "email" => "[email protected]"],
     ],
     "subject" => "Hello, PHP!",
     "body"    => "Test <br> message"
];

$draft = $client->drafts($OPTIONS)->create($message_obj);

$send_message = $draft->send( ['id' => $draft->id] );

echo $send_message->id;

$client = new Nylas($OPTIONS);

$message =
[
     "to"      =>
     [
         ["name" => "Nylas", "email" => "[email protected]"],
         ["name" => "Goole", "email" => "[email protected]"],
     ],
     "subject" => "Hello, PHP!",
     "body"    => "Test <br> message"
];

$draft = $client->drafts($OPTIONS)->send($message_obj);

$client = new Nylas($OPTIONS);

$calendars = $client->calendars($OPTIONS)->all();

$calendar = null;

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

$calendar_data =
[
    "title"        => "Important Meeting",
    "location"     => "Nylas HQ",
    "participants" => [ ["name" => "nylas", "email" => "[email protected]"] ]
    "calendar_id"  => $calendar->id,
    "when"         => array("start_time" => time(),
    "end_time"     => time() + (30*60))
];

// create event
$event = $client->events($OPTIONS)->create($calendar_data);

echo $event->id;

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

// delete event
$event->delete();

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

$client = new Nylas($OPTIONS);
$is_valid = $client->xSignatureVerification($code, $data, $app_secret)