-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgmail-account-finder.php
143 lines (117 loc) · 3.94 KB
/
gmail-account-finder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
require_once 'vendor/autoload.php';
if (php_sapi_name() !== 'cli') {
throw new \Exception('This appication must be run from the CLI');
}
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Gmail Account Finder');
$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);
if (!file_exists('credentials.json')) {
throw new \Exception(
"credentials.json is MISSING!
You can generate one here:
https://developers.google.com/gmail/api/quickstart/php#step_1_turn_on_the\n
");
}
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
if (array_key_exists('error', $accessToken)) {
throw new \Exception(join(', ', $accessToken));
}
}
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
function decodeMessages($messages)
{
$client = getClient();
$gmail = new Google_Service_Gmail($client);
foreach($messages as $message) {
$response = $gmail->users_messages->get('me', $message->getId(), ['format' => 'full']);
$parts = $response->getPayload()->getParts();
$headers = $response->getPayload()->getHeaders();
if (isset($parts[0]['body'])) {
$rawData = $parts[0]['body']->data;
$sanitizedData = strtr($rawData,'-_', '+/');
$decodedEmail = trim(base64_decode($sanitizedData));
if ($foundKeyword = findWebsiteAssocWithEmail($decodedEmail)) {
printf("Website: %s\t Keyword: %s\n", parseSenderEmailFromHeaders($headers), $foundKeyword);
}
}
}
}
function getMessages($nextPage = null)
{
$client = getClient();
$gmail = new Google_Service_Gmail($client);
$options = [
'labelIds' => 'INBOX',
'maxResults' => 100
];
if ($nextPage) {
$options['pageToken'] = $nextPage;
}
$response = $gmail->users_messages->listUsersMessages('me', $options);
decodeMessages($response->getMessages());
$nextPageToken = $response->getNextPageToken();
if ($nextPageToken) {
getMessages($nextPageToken);
}
}
function findWebsiteAssocWithEmail(string $emailContent)
{
$keywords = [
'account created',
'welcome to',
'verify your account',
'confirm your email',
'verify your email address',
'registration',
'activate account',
'confirmation',
'unsubscribe'
];
foreach ($keywords as $keyword) {
if (stripos($emailContent, $keyword) !== false) {
return $keyword;
}
}
return false;
}
function parseSenderEmailFromHeaders($headers)
{
foreach($headers as $header)
{
if ($header['name'] === 'From') {
return parseDomain($header['value']);
}
}
}
function parseDomain($string)
{
preg_match ("/@(.*?)>/", $string, $result);
$parts = explode(".", $result[1]);
return (array_key_exists(count($parts) - 2, $parts) ? $parts[count($parts) - 2] : "").".".$parts[count($parts) - 1];
}
getMessages();