有没有办法使用GMail API获取GSuite中用户的所有电子邮件?

时间:2017-03-02 04:55:59

标签: gmail-api gmail-imap

如何获取与GSuite组织类似的用户的电子邮件 https://www.googleapis.com/gmail/v1/users/userId/messages/id

1 个答案:

答案 0 :(得分:0)

如果您使用的是Google PHP SDK,则可以执行以下操作。

$dir = 'GOOGLE_APPLICATION_CREDENTIALS='service-account.json';
        putenv("$dir");
        // var_dump($dir);
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->setScopes(array('https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.directory.user.alias'));
        $client->setSubject("$this->admin_email");
        $dir = new Google_Service_Directory($client);
        $users = $dir->users->listUsers(Array('domain' => "your-super@admin-email.com");

foreach ($users["users"] as $key => $value) {
                $email = $value['primaryEmail'];
                $name = $value['name']['fullName'];
                foreach ($value["emails"] as $key => $email_alias) {
                    $alias = $email_alias['address'];
                    $users[] = ["email" => $email,
                                "alias" => $alias,
                                "name" => $name
                                ];
                }

            }
            print_r($users);

您需要确保已授予您的GSuite应用程序的权限。如果您不确定,可以在我的博客上获取有关如何执行此操作的更多信息。

http://moometric.com/integrations/gsuite/use-gmail-api-update-signatures-gsuite-users-php/