PHP bindings for the Nylas REST API (V2.7). https://developer.nylas.com/docs/api/v2/</br> I’ll try to keep up with NyLas API Changelog in future updates.
Last check at the point: [2023-05-16] Improvements to Scheduler and changes to message link tracking
What’s new?</br>
version 3.x for php >= 7.3 (branch 3.0)
version 4.x for php >= 7.4 (branch 4.0)
version 5.x for php >= 8.0 (branch master)
Tips: There are many breaking changes since version 5.0
This library is available on https://packagist.org/packages/lanlin/nylas-php</br> You can install it by running
composer require lanlin/nylas-php
Before you can interact with the Nylas REST API,</br> you need to create a Nylas developer account at https://www.nylas.com/.</br> After you’ve created a developer account, you can create a new application to generate an App ID / Secret pair.</br>
Generally, you should store your App ID and Secret into environment variables to avoid adding them to source control.</br> The test projects use configuration files instead, to make it easier to get started.</br>
use Nylas\Client;
$options =
[
'client_id' => 'your client id', // required
'client_secret' => 'your client secret' // required
'debug' => true,
'region' => 'oregon', // server region, can be oregon of United States, ireland of Europe, default is oregon
'log_file' => dirname(__FILE__) . '/test.log', // a file path or a resource handler
'access_token' => 'your access token',
];
$nylas = new Client($options);
You can modify options with methods of \Nylas\Utilities\Options
$nylas->Options->setXxx();
Most of the methods that have the get & delete prefix support batch request.
$id = 'id_xxx';
$ids = ['id_xxx', 'id_yyy', ...];
// one per time
$dataA = $nylas->Contacts->Contact->returnAContact($id);
$dataB = $nylas->Contacts->Contact->deleteAContact($id);
// batch request
$dataC = $nylas->Contacts->Contact->returnAContact($ids);
$dataD = $nylas->Contacts->Contact->deleteAContact($ids);
For more detail about the batch request, you should have to read the source code.</br> Sorry, I have no time to write documents.
There are two ways you can authenticate users to your application.</br> Hosted & Native are both supported.</br>
For Native Authentication example, please visit Native Auth Document </br> For Hosted OAuth(server-side three-legged) example:</br>
For more information about authenticating with Nylas,</br> visit the Developer Documentation.</br>
In practice, the Nylas REST API client simplifies this down to two steps.</br>
Step 1: Redirect the user to Nylas:
$params =
[
'state' => 'testing',
'login_hint' => 'test@gmail.com',
'redirect_uri' => 'https://www.test.com/redirect_callback',
];
// generate the url that your user need be redirect to.
$url = $nylas->Authentication->Hosted->authenticateUser($params);
Step 2: your user logs in:</br> Step 3: you got the access code from the nylas callback:</br> Please implement the above 2 & 3 steps yourself.</br>
Step 4: Get authorization token with access code:
$data = $nylas->Authentication->Hosted->sendAuthorizationCode($params);
// save your token some where
// or update the client option
$nylas->Options->setAccessToken("pass the token you got");
common error codes that response from nylas are wrapped as exceptions, (see src/Exceptions
)
and the exception code is the same as nylas api error list
you will get an array like below, when response data was not a valid json string or even not json content type:
[
'httpStatus' => 'http status code',
'invalidJson' => true,
'contentType' => 'response header content type',
'contentBody' => 'response body content',
]
for all methods that execute as the async mode will not throw an exception when an error occurs, instead, it will return an array which contains all data and exceptions inside like below:
[
// ...
[
'error' => true,
'code' => 'exception code',
'message' => 'exception message',
'exception' => 'exception instance',
],
// ...
]
some email provider may not support all features, exp: calendar, event.
for that reason you may get an exception named BadRequestException
with 400 code and the msg:
Malformed or missing a required parameter, or your email provider not support this.
the log_file
parameter only works when debug
set to true
,
then the detailed info of the http request will be logged.
Tips: nylas-php use the guzzlehttp for http request. but guzzlehttp only support a resource type as the debug handler (cURL CURLOPT_STDERR required that).
for anyone who wants to use psr/log interface to debug, you can init a temp resource, and pass the handler to nylas-php, then get log content from temp resource after calling some methods.
$handler = fopen('php://temp', 'w+');
$options =
[
'log_file' => $handler,
...
];
$nylas = new Client($options);
$nylas->doSomething();
....
rewind($handler);
$logContent = stream_get_contents($handler);
fclose($handler);
$yourPsrLogger->debug($logContent);
composer install
tests/AbsCase.php
composer run-script test
./tests/do.sh foo.php --filter fooMethod
, see tests/do.sh
Each method’s comment has a link to their specific API document, see here
The parameters that required by methods almost the same as nylas official api required.
For more detail, you can view the tests or the source code of validation rules for that method.
$nylas->Management->Account->xxx();
$nylas->Management->Application->xxx();
For more usage demos, please view the tests.</br> Please feel free to use it and send me a pull request if you fix anything or add a feature, though.</br>
This project is licensed under the MIT license.