activix/nylas-php

此包已被放弃,不再维护。未建议替代包。

PHP 对 Nylas API 的包装(针对 Activix 适配)。

2.0.7 2023-06-13 15:00 UTC

This package is auto-updated.

Last update: 2023-10-13 15:50:23 UTC


README

nylas/nylas-php 分支而来。为 Activix 适配。

Nylas REST API 的 PHP 绑定 https://www.nylas.com

安装

您可以通过运行以下命令安装库

cd nylas-php
composer install

用法

Nylas REST API 使用服务器端(三脚)OAuth,此库提供了简化 OAuth 流程的便利方法。以下是工作原理:

  1. 您将用户重定向到我们的登录页面,并带上您的 App Id 和 Secret
  2. 您的用户登录
  3. 她将被重定向到您自己的回调 URL,并带有访问代码
  4. 您使用此访问代码获取 API 的授权令牌

有关使用 Nylas 进行身份验证的更多信息,请访问 开发者文档

在实际操作中,Nylas REST API 客户端将此简化为两个步骤。

获取账户信息

$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
$firstThread = $client->threads()->first();
echo $firstThread->id;

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

foreach ($twoThreads as $thread) {
    echo $thread->id;
}

// List all threads with 'ben@nylas.com'
$searchCriteria = ['any_email' => 'ben@nylas.com'];
$getThreads = $client->threads()->where($searchCriteria)->items();

foreach ($getThreads as $thread) {
    echo $thread->id;
}

处理线程

// List thread participants
foreach ($thread->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
$toAdd = ['cfa1233ef123acd12'];
$toRemove = ['inbox'];
$thread->addTags($toAdd);
$thread->removeTags($toRemove);

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

处理文件

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

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

处理草稿

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

$personObj = new \Nylas\Models\Person('Kartik Talwar', 'kartik@nylas.com');

$messageObj = [
    'to' => [$personObj],
    'subject' => 'Hello, PHP!',
    'body' => 'Test <br> message'
];

$draft = $client->drafts()->create($messageObj);
$sendMessage = $draft->send();
echo $sendMessage->id;

处理事件

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

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

$personObj = new \Nylas\Models\Person('Kartik Talwar', 'kartik@nylas.com');

$calendarObj = [
    'title' => 'Important Meeting',
    'location' => 'Nylas HQ',
    'participants' => [$personObj],
    'calendar_id' => $calendar->id,
    'when' => [
        'start_time' => time(),
        'end_time' => time() + (30 * 60),
    ],
];

// Create event
$event = $client->events()->create($calendarObj);
echo $event->id;

// Update
$event = $event->update(['location' => 'Meeting room #1']);

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

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