Google GMail API服务帐户以列出来自多个帐户的电子邮件 [英] Google GMail API service account to list e-mails from several accounts

查看:68
本文介绍了Google GMail API服务帐户以列出来自多个帐户的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个CRM应用程序,其中所有员工用户都拥有一个位于同一域下的GMail帐户.我是该域的管理员. 以前,我使用IMAP对CRM中的所有GMail用户使用了自定义访问权限,以便获取他们发送和接收的所有电子邮件.为此,我询问了他们的GMail密码,这些密码未经加密就存储在数据库中(我知道这很糟糕). 现在,我想使用Google GMail API进行相同的工作,并且我有一些问题:

I'm working on a CRM application where all the staff users have a GMail account which is under a same domain. I'm the admin of this domain. Before, I used a custom access for all GMail users in my CRM using IMAP in order to get all the e-mails sent and received by them. For that I asked their GMail passwords that are stored without any encryption in my DB (It's crappy I know). Now, I want to do the same work but with the Google GMail API, and I have some questions :

-我能实现我想要的功能吗(列出每个工作人员从GMail发送的所有电子邮件,并使用GMail API和给定的GMail帐户通过GMail发送电子邮件)?

-Can I achieve what I want (list all the e-mails from GMail for every staff user and also send e-mails throug GMail using the GMail API and a given GMail account) ?

-我不确定,但是我已经看到使用服务帐户"和域范围的授权是可能的.如果是这样,即使使用服务帐户",我也需要浏览OAuth 2.0吗?也许有人可以指导我学习教程,或向我展示实现我想要的工作流程吗?最好的可能是PHP. 预先感谢您阅读我!

-I'm not sure but I've seen that it could be possible using a "service account" and domain-wide delegation of authority. If it's true, do I need to go through the OAuth 2.0 even when using a "service account" ? Can maybe anybody guide me on a tutorial or show me the workflow to achieve what I want please ? Best could be in PHP. Thanks in advance for reading me !

推荐答案

已解决!

对于那些正在寻找这种问题的人,我正在回答自己.

I'm answering myself, for people who look for this kind of question.

是的,我们能够列出属于同一域的帐户中的所有电子邮件. 为此,我们必须使用google admin帐户创建具有广域授权的服务帐户.它为您提供了一个要下载的Json文件(*).您将其放在服务器上.

Yes, we're able to list all e-mails from accounts which belong to a same domain. For that we must create a service account with wide-domaine delegation of authority, using the google admin account. It gives you a Json file(*) to download. You put it on your server.

与OAuth2.0无关.

There is nothing to do with OAuth2.0.

然后用这段简单的代码从您想要的帐户(在您的域中)中获取消息:

Then with this simple piece of code you get the messages from the account you want (in your domain) :

<?php
require_once __DIR__.'/vendor/autoload.php'; // <-- path for the google-api lib autoload
// simple function to get e-mails' headers
function getHeader($headers, $name) {
    foreach($headers as $header) {
        if($header['name'] == $name) {
            return $header['value'];
        }
    }
}

$user   = "myUser@myDomain.com";

putenv('GOOGLE_APPLICATION_CREDENTIALS='path/to/your/credentials.json'); // (*)
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://mail.google.com']);
$client->setSubject($user);

$service = new Google_Service_Gmail($client);

$results = $service-> users_messages->listUsersMessages($user);
foreach($results as $mail){
   $message = $service->users_messages->get($user, $mail['id']);
    $headers = $message->getPayload()->getHeaders();
    $subject = getHeader($headers, 'Subject');
    echo $subject."\n";
}
?>

这篇关于Google GMail API服务帐户以列出来自多个帐户的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆