如何通过Google的Gmail API设置sendAsEmail

时间:2017-02-03 18:20:45

标签: php gmail gmail-api

我正在更新PHP应用程序以在Google中创建新的电子邮件帐户(使用Google Directory Service)。我创建帐户没有问题,但到目前为止,我还无法使用Google Gmail服务设置sendAsEmail属性(因此别名将显示在" From")。此外,下面的代码段中的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();                                                                                                                     
    }                                                                                                                                                                                                                                                                                          
}      

1 个答案:

答案 0 :(得分:1)

最后,经过对代码的大量试验和一些同事的一些帮助,我弄清楚问题是什么以及如何解决它。

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