Adding sendAsMail Alias in GMail

I've been trying to add another sendasmail alias from sometimes visa app scripts but the endpoint doesn't seem to work or I'm not sure if that's the intended behavior of it.

This is the endpoint https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs/%s',getEmail, "007@alias.domain.com" that I'm using.

Here's the AppScipt Code along with payload which I'm trying to use.

            var service = getOAuthService(getEmail);
            var sendAPIUrl = Utilities.formatString('https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs/%s',getEmail, "007@alias.domain.com");
console.log(sendAPIUrl);
            var response = UrlFetchApp.fetch(sendAPIUrl, {
              method: "PUT",
              muteHttpExceptions: true,
              contentType: "application/json",
              headers: {
                Authorization: 'Bearer ' + service.getAccessToken()
              },
              payload: JSON.stringify({
                 "sendAsEmail": "007@alias.domain.com",
                  "isDefault": true,
                  "replyToAddress": "007@alias.domain.com",
                  "treatAsAlias": true
              })

            });

In getEmail, it holds the primary email Address of user and getOauthService is basically another function for user impersonation as for changing senAsEmail;s only service accounts will work.

I tried sending a POST request to create one, but failed with an error code while alias pattern is allowed in domain alias list and alias is available.

Here's the code for POST

    var service = getOAuthService(getEmail);
                var sendAPIUrl = 
Utilities.formatString('https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs',getEmail);
    console.log(sendAPIUrl);
                var response = UrlFetchApp.fetch(sendAPIUrl, {
                  method: "POST",
                  muteHttpExceptions: true,
                  contentType: "application/json",
                  headers: {
                    Authorization: 'Bearer ' + service.getAccessToken()
                  },
                  payload: JSON.stringify({
                     "sendAsEmail": "sid@alias.atos-team.com",
                  })
                });
                console.log(response.getContentText())

and this is the error that I got.

{
  "error": {
    "code": 400,
    "message": "sendAsEmail is not a valid user or group",
    "errors": [
      {
        "message": "sendAsEmail is not a valid user or group",
        "domain": "global",
        "reason": "invalidArgument"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}


Read more here: https://stackoverflow.com/questions/67937184/adding-sendasmail-alias-in-gmail

Content Attribution

This content was originally published by Sid Kaushik at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: