0
votes

Context:

My gmail account is not a service account. I am writing a PHP CLI application which checks for an email from a specific sender and specific subject. Assume this application is for a custom domain but not part of GSuite. I do not have server-wide control over our domain's emails.

I am using my own credentials.json file for Auth from Google APIs portal. I modified the code from the Quickstart to fetch emails based on my query and get the specific email I need. (The user is 'me' by the way from Quickstart example).

Reference: PHP documentation for Gmail API

Question:

How can I make this application work for any user within my domain? Is there any possible way, I can do this without requiring another account with server-wide delegation(I hope the term is correct) access?

For example

$tmp = new CustomClassUsingGmailAPI();
$emailIds = ["email1@customdomain", "email2@customdomain"];
foreach($emailIds as $emailId){
    $tmp->doTheRequiredTask($emailId);
}

1
If its not a gsuite domain. Your going to have to have each of your users authenticate your application. You need their permission to access their accounts. - DaImTo
I expected this answer. But,Thank you for information. I will still wait for somebody else to show up with any alternative - Chinni Srikar
Unfortunately this is the way the world of Oauth2 and data security works. You may have a long wait if you think someone else is going to respond and tell you how to access private user data without the owners permission. - DaImTo

1 Answers

2
votes

The tutorial you are following uses Oauth2 to authorize your applications access to users data.

The user is 'me' by the way from Quickstart example).

Actually the user is always the person who logs in and authenticates the application. The only way to access private user data is to have the permission of the person who owns that data.

There is no way to access the email of a non gsuite domain user. This is called data privacy and security. You and your application can not access my data with out my permission.

Unless you convert your domain to a gsuite domain, you will not be able to access the users emails for that domain, without their permission.