-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailChecker.php
121 lines (102 loc) · 2.92 KB
/
EmailChecker.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
<?php
namespace Hostinger;
class EmailChecker
{
private $timeout = 2;
public function setTimeout($value)
{
$this->timeout = $value;
}
/**
* Check if it's a valid email, ie. not a throwaway email.
*
* @param string $email The email to check
*
* @return bool true for a throwaway email
*/
public function isValid($email)
{
if (strlen($email) > 254) {
return false;
}
if (false === $email = filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
try {
list($local, $domain) = explode('@', $email);
} catch (\Exception $e) {
return false;
}
$domains = $this->getDomains();
if (in_array($domain, $domains)) {
return false;
}
$mxs = $this->getMxsRecords($domain);
if (empty($mxs)) {
return false;
}
$mx_info = $mxs[array_rand($mxs)];
$mx_host = $mx_info['target'];
if (in_array($mx_host, $domains)) {
return false;
}
$mx = substr($mx_host, ($pos = strpos($mx_host, '.')) !== false ? $pos + 1 : 0);
if (in_array($mx, $domains)) {
return false;
}
return true;
}
public function getDomains()
{
return json_decode(file_get_contents(__DIR__ . '/blacklist.json'));
}
public function getMxsRecords($domain)
{
if ($this->execEnabled()) {
return $this->getMxRecordWithDigCmd($domain);
}
return dns_get_record($domain, DNS_MX);
}
public function execEnabled()
{
if (!function_exists('exec')) {
return false;
}
$disabled = explode(',', ini_get('disable_functions'));
return !in_array('exec', $disabled);
}
/**
* Use shell command dig to get MX record of provided domain
* @param string $domain
* @return array - same return values as dns_get_record()
* @see dns_get_record()
*/
public function getMxRecordWithDigCmd($domain)
{
$lines = [];
$command = 'dig +noall +answer +time=' . escapeshellarg($this->timeout) . ' MX ' . escapeshellarg($domain);
exec($command, $lines);
$output = [];
if (empty($lines)) {
return $output;
}
if (strpos($lines[0], 'connection timed out') !== FALSE){
return $output;
}
foreach ($lines as $line) {
$mxInfo = explode(' ', preg_replace('!\s+!', ' ', $line));
if ($mxInfo[3] != 'MX'){
continue;
}
$output[] = [
'host' => $mxInfo[0],
'ttl' => $mxInfo[1],
'class' => $mxInfo[2],
'type' => $mxInfo[3],
'pri' => $mxInfo[4],
'target' => $mxInfo[5],
];
}
return $output;
}
}