dev-master
9999999-devPHP wrapper for the Nylas API
MIT
The Requires
- php >=5.4
- guzzlehttp/guzzle ~5.0
The Development Requires
by Kartik Talwar
PHP wrapper for the Nylas API
PHP bindings for the Nylas REST API https://www.nylas.com, (*1)
Please feel free to use it and send us a pull request if you fix anything or add a feature, though. :), (*2)
You can install the library by running:, (*3)
cd nylas-php composer install
The Nylas REST API uses server-side (three-legged) OAuth, and this library provides convenience methods to simplify the OAuth process. Here's how it works:, (*4)
For more information about authenticating with Nylas, visit the Developer Documentation., (*5)
In practice, the Nylas REST API client simplifies this down to two steps., (*6)
index.php, (*7)
$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);
login_callback.php, (*8)
$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 'ben@nylas.com' $search_criteria = array("any_email" => "ben@nylas.com"); $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', 'kartik@nylas.com'); $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', 'kartik@nylas.com'); $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();
The Nylas Sync Engine is open-source, and you can also use the PHP library with the open-source API. Since the open-source API provides no authentication or security, connecting to it is simple. When you instantiate the Nylas object, provide null for the App ID, App Secret, and API Token, and pass the fully-qualified address of your copy of the sync engine:, (*9)
$client = new Nylas(CLIENT, SECRET, TOKEN, 'http://localhost:5555/');
We'd love your help making Nylas better. Join the Google Group for project updates and feature discussion. We also hang out in #nylas
on irc.freenode.net, or you can email support@nylas.com., (*10)
Please sign the Contributor License Agreement before submitting pull requests. (It's similar to other projects, like NodeJS or Meteor.), (*11)
PHP wrapper for the Nylas API
MIT