I am using gmail api in php application. While I send mail using the sender_email it returns that invalid email
. I have a table sender_email
: in this table I enlisted all gmail address.
I need to check the email is valid or not :: If not valid then update the table sender_email
for the gmail adresss and then it will find another gmail address which is valid. This is continous process.
Here is the code for checking emails using gmail api::
public static function check_gmail_authentication($from_email, $auth_token){
while (true)
{
try
{
$result = GmailSendMessage::re_check_gmail_authentication($from_email, $auth_token);
if($result == null){
DB::table('sender_email')->where('email', $from_email)->update(['status' => 'invalid']);
$sender_email = SenderEmail::where('status','=', 'public')->where('api_type', 'google')->first();
$from_email = $sender_email->auth_token;
$auth_token = $sender_email->email;
//again try
$result = GmailSendMessage::check_gmail_authentication($from_email, $auth_token);
}
break;
}
catch(Exception $e)
{
print_r($e->getMessage() . ' - failed. Retrying...');
continue;
}
}
}
and re-checking the method in same controller::
public static function re_check_gmail_authentication($from_email, $auth_token){
//Google client
$client = new Google_Client();
$client->setAuthConfigFile(public_path() . '/apis/api_for_sender_email.json');
$client->setLoginHint($from_email);
$client->setAccessType('offline');
$json_token = $auth_token;
try {
$client->setAccessToken($json_token);
// If access token is not valid use refresh token
if ($client->isAccessTokenExpired()) {
$refresh_token = $client->getRefreshToken();
$client->refreshToken($refresh_token);
}
return $from_email;
} catch (\Exception $e) {
return false;
}
}
Parameter $from_email = gmail address
and $auth_token = access token
.
Issue :: My code returns NULL
What I want ::
- I need a valid email
- I need to update invalid email in the
sender_eamil
table
Please help me how do I get valid email using while
loop. Thanks in advanced.