【问题标题】:How to set sendAsEmail via Google's Gmail api如何通过 Google 的 Gmail api 设置 sendAsEmail
【发布时间】:2017-02-03 18:20:44
【问题描述】:

我正在更新一个 PHP 应用程序以在 Google 中创建新的电子邮件帐户(使用 Google 目录服务)。我创建帐户没有问题,但到目前为止,我一直无法使用 Google Gmail 服务来设置 sendAsEmail 属性(因此别名将显示在“发件人”中)。此外,下面 sn-p 中的 var_dump($createSendAsResult) 不会产生任何输出。任何帮助,将不胜感激。谢谢! 这是我的代码:

//Create account in Google                                                   
function createGoogleAccount($server_name, $acc_user, $acc_password)                                                                                                           
{                                                                                                                                                                                 
    $clientDir = getClientDir($server_name);                                                                                                                                           
    $dirService = new Google_Service_Directory($clientDir);                                                                                                                            

    $userInstance = new Google_Service_Directory_User();                                                                                                                               
    $nameInstance = new Google_Service_Directory_UserName();                                                                                                                           

    $nameInstance -> setGivenName('Generic');                                                                                                                                          
    $nameInstance -> setFamilyName($acc_user);                                                                                                                                         

    $userInstance -> setOrgUnitPath("/generic_email");                                                                                                                                 
    $userInstance -> setName($nameInstance);                                                                                                                                           
    $userInstance -> setHashFunction("MD5");                                                                                                                                           
    $domain = getDomain($server_name);                                                                                                                                                 
    $primary_email = $acc_user . '@' . $domain;                                                                                                                                        
    $userInstance -> setPrimaryEmail($primary_email);                                                                                                                                  
    $userInstance -> setPassword(hash("md5", $acc_password));                                                                                                                          
    $optParams = array( );                                                                                                                                                             

    $error_msg = null;                                                                                                                                                                 
    try                                                                                                                                                                                
    {                                                                                                                                                                                  
            $createUserResult = $dirService->users->insert($userInstance, $optParams);                                                                                                 
            var_dump($createUserResult);                                                                                                                                               
    }                                                                                                                                                                                  
    catch (Google_IO_Exception $gioe)                                                                                                                                                  
    {                                                                                                                                                                                  
            $error_msg =  "Error in connection: ".$gioe->getMessage();                                                                                                                 
    }                                                                                                                                                                                  
    catch (Google_Service_Exception $gse)                                                                                                                                              
    {                                                                                                                                                                                  
            $error_msg = "Service Exception: ".$gse->getMessage();                                                                                                                     
    }                                                                                                                                                                                  

    addSendAs($server_name, $acc_user, $domain);                                                                                                      return $error_msg;                                                                                                                                                                 
}                                                                                                                                                                                          
function addSendAs($server_name, $acc_user, $domain)                                                                                                                                       
{                                                                                                                                                                                          
    $clientGmail = getClientGmail($server_name);                                                                                                                                       
    $gmailService = new Google_Service_Gmail($clientGmail);                                                                                                                            
    $primary_email = $acc_user . '@' . $domain;                                                                                                                                        
    $sendAsEmail = new Google_Service_Gmail_SendAs();                                                                                                                                  

    $alias = '';                                                                                                                                                                       
    if (($server_name == null) || (strpos($server_name, "dev") != false))                                                                                                              
    {                                                                                                                                                                                  
            $alias = '@g.';                                                                                                                                                            
    }                                                                                                                                                                                  
    else                                                                                                                                                                               
    {                                                                                                                                                                                  
            $alias = '@mail.';                                                                                                                                                         
    }                                                                                                                                                                                  

    $sendAsEmail -> setSendAsEmail($acc_user . $alias . $domain);                                                                                                                      
    $sendAsEmail -> setIsDefault(TRUE);                                                                                                                                                
    $sendAsEmail -> setIsPrimary(TRUE);                                                                                                                                                

    $error_msg = null;                                                                                                                                                                 
    try                                                                                                                                                                                
    {                                                                                                                                                                                  
            $createSendAsResult = $gmailService->users_settings_sendAs -> create($primary_email, $sendAsEmail);                                                                        
            var_dump($createSendAsResult);                                                                                                                                             
    }                                                                                                                                                                                  
    catch (Google_IO_Exception $gioe)                                                                                                                                                  
    {                                                                                                                                                                                  
            $error_msg =  "Error in connection: ".$gioe->getMessage();                                                                                                                 
    }                                                                                                                                                                                  
    catch (Google_Service_Exception $gse)                                                                                                                                              
    {                                                                                                                                                                                  
            $error_msg = "Service Exception: ".$gse->getMessage();                                                                                                                     
    }                                                                                                                                                                                                                                                                                          
}      

【问题讨论】:

  • 好吧,如果我尝试使用 Gmail API 的 Try it 部分设置此 sendAsEmail 属性,我会收到错误 403 "Access restricted to service accounts that have been delegated domain-wide authority" 我在此 thread 中发现了此错误是“初始帐户必须具有管理员或超级管理员委派才能在管理控制面板中操作,然后您可以在其中授予帐户添加权限(委派)。”
  • 自从我第一次发帖以来,我做了一些代码更改,包括添加了一个 error_log() 语句来输出异常消息。 (我的大部分编程经验都是用 Java 编写的,但我需要在这个项目中使用 php,所以我还在习惯它。)这个帐户确实具有域范围的授权,并且访问范围包括 Gmail 的所有内容。这是我得到的错误: Service Exception: {\n "error": {\n "errors": [\n {\n "domain": "global",\n "reason": "forbidden",\n " message": "generic_test_svc@sandbox.xxx.edu 的委托被拒绝"\n }\n ],\n ...

标签: php gmail gmail-api


【解决方案1】:

最后,在对代码进行了大量实验并得到了几位同事的帮助后,我弄清楚了问题所在以及如何解决它。

显然,Google 需要时间来设置新用户的电子邮件帐户。当我在 php 中添加 10 秒延迟 - sleep(10) - 这足以让帐户准备好调用 Gmail API,包括创建 SendAs 别名。

【讨论】:

  • 在现实生活中,解决办法就是睡觉。 :)
猜你喜欢
  • 2019-09-19
  • 2021-04-10
  • 2014-11-04
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 2014-10-19
  • 1970-01-01
相关资源
最近更新 更多