0

I'm trying to create a custom "from" send-as alias (sendAsEmail) with Gmail API and Google Admin: https://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs/create

I'm getting a 400 error with sendAsEmail is not a valid user or group.

In Google Admin I have a 3 verified domains, all with MX properly registered with gmail services :

@domain-a.com is primary domain
@domain-b.com (not a domain alias) 
@domain-c.com (not a domain alias)

user@domain-a.com is a user, with no send-as alias defined. When I create the sendAsEmail user@domain-b.com, no issue. I can see it appears in gmail. When I create the sendAsEmail user@domain-c.com, it fails. I can not find out why. If I search for user@domain-c.com in Google Admin to be sure if it is not a group, no result. Any help would be very welcomed.

I'm doing my api calls with PHP, but I dont thing that the issue is related with the language.

sbouba
  • 337
  • 1
  • 2
  • 9

1 Answers1

0

Finally found a solution. There was actually multiples causes :

Google Admin :

First on Google Admin side, I had to create an alias : https://developers.google.com/admin-sdk/directory/v1/guides/manage-user-aliases. If you dont do this, the Gmail sendAsEmail creation will fail. Note that the Client HTTP used to do this call needs to use an admin user :

<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=./client_secrets.json');
$this->client = new Google_Client();
$this->client->useApplicationDefaultCredentials();  // loads whats in that json service account file.
$this->client->setScopes([
  Google_Service_Directory::ADMIN_DIRECTORY_USER,
  Google_Service_Directory::ADMIN_DIRECTORY_USER_SECURITY,
  Google_Service_Directory::ADMIN_DIRECTORY_USER_ALIAS
]);
$this->client->setSubject('admin@domain.com');

Gmail :

Create a sendAsEmail like explained here : https://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs/create Note that the Client Http used to do this call needs to impersonate the user :

<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=./client_secrets.json');
$this->client = new Google_Client();
$this->client->useApplicationDefaultCredentials();  // loads whats in that json service account file.
$this->client->setScopes([
  Google_Service_Gmail::GMAIL_SETTINGS_BASIC,
  Google_Service_Gmail::GMAIL_SETTINGS_SHARING
]);
$this->client->setSubject('user@domain.com');
sbouba
  • 337
  • 1
  • 2
  • 9