1
1
<?php namespace overint ;
2
2
3
-
3
+ /**
4
+ * Validate email address with Mailgun's validation service (Syntax checks, DNS validation, MX validation)
5
+ */
4
6
class MailgunValidator
5
7
{
8
+ /** @var string Mailgun API endpoint URL */
9
+ const API_ENDPOINT = 'https://api.mailgun.net/v3/address/validate ' ;
10
+
11
+ /** @var string Mailgun email validation API key */
6
12
private $ apiKey ;
7
13
14
+ /**
15
+ * MailgunValidator constructor.
16
+ * @param string $apiKey Mailgun email validation API key
17
+ */
8
18
function __construct ($ apiKey )
9
19
{
10
20
$ this ->apiKey = $ apiKey ;
11
21
}
12
22
23
+
24
+ /**
25
+ * Use curl to send the validation request to Mailgun
26
+ * @param string $email
27
+ * @return array
28
+ */
13
29
private function queryMailgun ($ email )
14
30
{
15
31
$ curl = curl_init ();
16
32
17
33
curl_setopt_array ($ curl , array (
18
- CURLOPT_URL => " https://api.mailgun.net/v3/address/validate ?api_key= " . $ this ->apiKey . "&address= " . $ email ,
34
+ CURLOPT_URL => self :: API_ENDPOINT . " ?api_key= " . $ this ->apiKey . "&address= " . $ email ,
19
35
CURLOPT_RETURNTRANSFER => true ,
20
36
CURLOPT_MAXREDIRS => 0 ,
21
37
CURLOPT_TIMEOUT => 30 ,
@@ -33,12 +49,23 @@ private function queryMailgun($email)
33
49
}
34
50
}
35
51
52
+
53
+ /**
54
+ * Validate an email address and return a boolean indicating validity
55
+ * @param string $email Email adddress to be validated
56
+ * @return boolean
57
+ */
36
58
public function validate ($ email )
37
59
{
38
60
$ ret = $ this ->queryMailgun ($ email );
39
61
return $ ret ->is_valid ;
40
62
}
41
63
64
+ /**
65
+ * Validate an email address and return a detailed infomation from Mailgun
66
+ * @param string $email Email adddress to be validated
67
+ * @return array
68
+ */
42
69
public function validateExtended ($ email )
43
70
{
44
71
return $ this ->queryMailgun ($ email );
0 commit comments