Search code examples
google-apps-scriptgmailgoogle-formsgoogle-directory-api

Creating user aliases in gmail using google apps script


I am reading in alias email information from a google form entry and I wish to use it to create an alias email.

This link talks about using the directory API to accomplish this but from its description about setting up authorization etc. it seems to be for a web application.

How do I create an email alias for an account using a Google apps script associated with my Google Form?


Solution

  • You can use the Admin SDK's Directory API in Apps Script, but you need to use the Advanced Admin SDK Directory service (That must be enabled before use. In the Script Editor select Resources > Advanced Google services... and then enable it in the Google Developers Console.)

    Once enabled you can add an alias like this:

    function myFunction() {
      var userKey = '[email protected]';
      var resource = {
        alias: '[email protected]'
      }
    
      AdminDirectory.Users.Aliases.insert(resource, userKey)
    }
    

    Then you can verify the alias in your Admin Console in Users > Select User > Account > Aliases:

    enter image description here