使用 Gmail 创建过滤器 API

Create a filter using Gmail API

Google 最近发布了 Gmail API 的新版本,现在可以 create filters.

但是文档非常有限,我在让它工作时遇到了问题。我正在使用他们 PHP client 的最新版本。如有任何帮助构建请求正文,我们将不胜感激。

public $gmail;
public function createFilter($userId) {

    try {

        $filter = new Google_Service_Gmail_Resource_UsersSettingsFilters();
        // Here, we should create the request body...
        // https://developers.google.com/gmail/api/v1/reference/users/settings/filters#resource
        // $filter->setCriteria() ??

        $this->gmail->users_settings_filters->create($userId, $filter);


    } catch (Exception $e) {
        // Logging errors...
    }

}

更新(工作方法)

public $gmail;
public function createFilter($userId) {

    try {

       $filter = new Google_Service_Gmail_Filter([
            'criteria' => [
                'from' => 'example@gmail.com'
            ],
            'action' => [
                'addLabelIds' => ['STARRED']
            ]
        ]);

        $this->gmail->users_settings_filters->create($userId, $filter);


    } catch (Exception $e) {
        // Logging errors...
    }

}

有关构建请求对象的指导,请参阅 https://github.com/google/google-api-php-client#making-requests。您应该能够使用本机 PHP 数组或自动生成的对象来填充过滤器属性。示例:

$filter = new Google_Service_Gmail_Resource_UsersSettingsFilters([
    'criteria' => [
        'from' => 'somebody@example.com'
    ],
    'action' => [
        'addLabelIds' => ['STARRED']
    ]
]);