title: How to set sendAsEmail via Google's Gmail api
body: <p>I'm updating a PHP application to create new email accounts in Google (using the Google Directory Service). I have no problem creating the account, but so far I've been unable to use the Google Gmail Service to set the sendAsEmail attribute (so that the alias will show up in the "From"). Also, var_dump($createSendAsResult) in the snippet below does not produce any output. Any help would be appreciated.  Thanks!
  Here's my code:</p>
  
  <pre><code>//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 -&gt; setGivenName('Generic');                                                                                                                                          
      $nameInstance -&gt; setFamilyName($acc_user);                                                                                                                                         
  
      $userInstance -&gt; setOrgUnitPath("/generic_email");                                                                                                                                 
      $userInstance -&gt; setName($nameInstance);                                                                                                                                           
      $userInstance -&gt; setHashFunction("MD5");                                                                                                                                           
      $domain = getDomain($server_name);                                                                                                                                                 
      $primary_email = $acc_user . '@' . $domain;                                                                                                                                        
      $userInstance -&gt; setPrimaryEmail($primary_email);                                                                                                                                  
      $userInstance -&gt; setPassword(hash("md5", $acc_password));                                                                                                                          
      $optParams = array( );                                                                                                                                                             
  
      $error_msg = null;                                                                                                                                                                 
      try                                                                                                                                                                                
      {                                                                                                                                                                                  
              $createUserResult = $dirService-&gt;users-&gt;insert($userInstance, $optParams);                                                                                                 
              var_dump($createUserResult);                                                                                                                                               
      }                                                                                                                                                                                  
      catch (Google_IO_Exception $gioe)                                                                                                                                                  
      {                                                                                                                                                                                  
              $error_msg =  "Error in connection: ".$gioe-&gt;getMessage();                                                                                                                 
      }                                                                                                                                                                                  
      catch (Google_Service_Exception $gse)                                                                                                                                              
      {                                                                                                                                                                                  
              $error_msg = "Service Exception: ".$gse-&gt;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 -&gt; setSendAsEmail($acc_user . $alias . $domain);                                                                                                                      
      $sendAsEmail -&gt; setIsDefault(TRUE);                                                                                                                                                
      $sendAsEmail -&gt; setIsPrimary(TRUE);                                                                                                                                                
  
      $error_msg = null;                                                                                                                                                                 
      try                                                                                                                                                                                
      {                                                                                                                                                                                  
              $createSendAsResult = $gmailService-&gt;users_settings_sendAs -&gt; create($primary_email, $sendAsEmail);                                                                        
              var_dump($createSendAsResult);                                                                                                                                             
      }                                                                                                                                                                                  
      catch (Google_IO_Exception $gioe)                                                                                                                                                  
      {                                                                                                                                                                                  
              $error_msg =  "Error in connection: ".$gioe-&gt;getMessage();                                                                                                                 
      }                                                                                                                                                                                  
      catch (Google_Service_Exception $gse)                                                                                                                                              
      {                                                                                                                                                                                  
              $error_msg = "Service Exception: ".$gse-&gt;getMessage();                                                                                                                     
      }                                                                                                                                                                                                                                                                                          
  }      
  </code></pre>