Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Laravel rule to ensure Gmail emails actually exist
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class ValidGmailAddress implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$response = Http::post('https://accounts.google.com/InputValidator?resource=signup&service=mail', ['input01' => [
'Input' => 'GmailAddress', 'GmailAddress' => Str::before($value, '@gmail.com'), 'FirstName' => '', 'LastName' => ''
], 'Locale' => 'en'])->json();
return data_get($response, 'input01.ErrorMessage') == 'That username is taken. Try another.';
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The provided :attribute is not a valid Gmail address.';
}
}
@ivansieder

This comment has been minimized.

Copy link

@ivansieder ivansieder commented Nov 30, 2020

Great! For those like me, who are looking for the HTTP equivalent:

POST /InputValidator?resource=signup&service=mail HTTP/1.1
Host: accounts.google.com
Content-Type: application/json
Content-Length: 176

{
    "input01": {
        "Input": "GmailAddress",
        "GmailAddress": "example@gmail.com",
        "FirstName": "",
        "LastName": "",
        "Locale": "en"
    }
}
@top9xy

This comment has been minimized.

Copy link

@top9xy top9xy commented Jan 24, 2021

Additional

$valid_google = $response['input01']['Valid'];
if ($valid_google == true || ($valid_google == false && $response['input01']['ErrorMessage'] == 'This username isn&#39;t allowed. Try again.')) {
        return true;
    } else {
        return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment