11<?php namespace overint ;
22
3-
3+ /**
4+ * Validate email address with Mailgun's validation service (Syntax checks, DNS validation, MX validation)
5+ */
46class MailgunValidator
57{
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 */
612 private $ apiKey ;
713
14+ /**
15+ * MailgunValidator constructor.
16+ * @param string $apiKey Mailgun email validation API key
17+ */
818 function __construct ($ apiKey )
919 {
1020 $ this ->apiKey = $ apiKey ;
1121 }
1222
23+
24+ /**
25+ * Use curl to send the validation request to Mailgun
26+ * @param string $email
27+ * @return array
28+ */
1329 private function queryMailgun ($ email )
1430 {
1531 $ curl = curl_init ();
1632
1733 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 ,
1935 CURLOPT_RETURNTRANSFER => true ,
2036 CURLOPT_MAXREDIRS => 0 ,
2137 CURLOPT_TIMEOUT => 30 ,
@@ -33,12 +49,23 @@ private function queryMailgun($email)
3349 }
3450 }
3551
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+ */
3658 public function validate ($ email )
3759 {
3860 $ ret = $ this ->queryMailgun ($ email );
3961 return $ ret ->is_valid ;
4062 }
4163
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+ */
4269 public function validateExtended ($ email )
4370 {
4471 return $ this ->queryMailgun ($ email );
0 commit comments