55
66class MailboxValidatorController{
77
8- public function single ($ email ) {
9- $ api_key = Configure::read ('MBV_API_KEY ' );
10- $ source = 'cakephp ' ;
11- if (trim ($ email ) != '' ) {
12- $ results = file_get_contents ('https://api.mailboxvalidator.com/v1/validation/single?key= ' . $ api_key . '&email= ' .$ email . '&source= ' .$ source );
13- // Decode the return json results and return the data as an array.
14- $ data = json_decode ($ results ,true );
15- if (trim ($ data ['error_code ' ]) == '' ) {
16- $ this ->custom_log ($ results );
17- if ( $ data ['status ' ] == 'False ' ) {
18- return false ;
19- } else {
20- return true ;
21- }
22- }else {
23- $ this ->mbv_error_log ($ data );
24- return false ;
25- }
26- }
27- }
8+ private $ source = 'cakephp ' ;
9+ private $ singleValidationApiUrl = 'https://api.mailboxvalidator.com/v2/validation/single ' ;
10+ private $ disposableEmailApiUrl = 'https://api.mailboxvalidator.com/v2/email/disposable ' ;
11+ private $ freeEmailApiUrl = 'https://api.mailboxvalidator.com/v2/email/free ' ;
12+
13+ public function __construct ()
14+ {
15+ $ this ->api_key = Configure::read ('MBV_API_KEY ' );
16+ }
17+
18+ public function __destruct ()
19+ {
20+
21+ }
2822
29- public function disposable ($ email ) {
30- $ api_key = Configure::read ('MBV_API_KEY ' );
31- $ source = 'cakephp ' ;
32- if (trim ($ email ) != '' ) {
33- $ results = file_get_contents ('https://api.mailboxvalidator.com/v1/email/disposable?key= ' . $ api_key . '&email= ' .$ email . '&source= ' .$ source );
34- // Decode the return json results and return the data as an array.
35- $ data = json_decode ($ results ,true );
36- if (trim ($ data ['error_code ' ]) == '' ) {
37- $ this ->custom_log ($ results );
38- if ( $ data ['is_disposable ' ] == 'True ' ) {
39- return false ;
40- } else {
41- return true ;
42- }
43- }else {
44- $ this ->mbv_error_log ($ data );
45- return false ;
46- }
47- }
48- }
23+ public function single ($ email ) {
24+ if (trim ($ email ) != '' ) {
25+ try {
26+ $ params = [ 'email ' => $ email , 'key ' => $ this ->api_key , 'format ' => 'json ' , 'source ' => $ this ->source ];
27+ $ params2 = [];
28+ foreach ($ params as $ key => $ value ) {
29+ $ params2 [] = $ key . '= ' . rawurlencode ($ value );
30+ }
31+ $ params = implode ('& ' , $ params2 );
32+
33+ $ results = file_get_contents ($ this ->singleValidationApiUrl . '? ' . $ params );
34+
35+ if ($ results !== false ) {
36+ if (!isset ($ results ->error )) {
37+ $ data = json_decode ($ results ,true );
38+ if ( $ data ['status ' ] ) {
39+ return true ;
40+ } else {
41+ return false ;
42+ }
43+ } else {
44+ return true ;
45+ }
46+ } else {
47+ return true ;
48+ }
49+ } catch (Exception $ e ) {
50+ return true ;
51+ }
52+ } else {
53+ return true ;
54+ }
55+ }
4956
50- public function free ($ email ){
51- $ api_key = Configure::read ('MBV_API_KEY ' );
52- $ source = 'cakephp ' ;
53- if (trim ($ email ) != '' ) {
54- $ results = file_get_contents ('https://api.mailboxvalidator.com/v1/email/free?key= ' . $ api_key . '&email= ' .$ email . '&source= ' .$ source );
55- // Decode the return json results and return the data as an array.
56- $ data = json_decode ($ results ,true );
57- if (trim ($ data ['error_code ' ]) == '' ) {
58- $ this ->custom_log ($ results );
59- if ( $ data ['is_free ' ] == 'True ' ) {
60- return false ;
61- } else {
62- return true ;
63- }
64- }else {
65- $ this ->mbv_error_log ($ data );
66- return false ;
67- }
68- }
69- }
57+ public function disposable ($ email ) {
58+ if (trim ($ email ) != '' ) {
59+ try {
60+ $ params = [ 'email ' => $ email , 'key ' => $ this ->api_key , 'format ' => 'json ' , 'source ' => $ this ->source ];
61+ $ params2 = [];
62+ foreach ($ params as $ key => $ value ) {
63+ $ params2 [] = $ key . '= ' . rawurlencode ($ value );
64+ }
65+ $ params = implode ('& ' , $ params2 );
66+
67+ $ results = file_get_contents ($ this ->disposableEmailApiUrl . '? ' . $ params );
68+
69+ if ($ results !== false ) {
70+ if (!isset ($ results ->error )) {
71+ $ data = json_decode ($ results , true );
72+ Log::write ('debug ' , json_encode ($ data ));
73+ if ( $ data ['is_disposable ' ] ) {
74+ return false ;
75+ } else {
76+ return true ;
77+ }
78+ } else {
79+ return true ;
80+ }
81+ } else {
82+ return true ;
83+ }
84+ } catch (Exception $ e ) {
85+ return true ;
86+ }
87+ } else {
88+ return true ;
89+ }
90+ }
7091
71- public function custom_log ($ result ) {
72- file_put_contents ( '../logs/mbv_log.log ' , date ('d M, Y h:i:s A ' ) . PHP_EOL , FILE_APPEND );
73- file_put_contents ( '../logs/mbv_log.log ' , $ result . PHP_EOL , FILE_APPEND );
74- }
75-
76- public function mbv_error_log ($ data ) {
77- file_put_contents ( '../logs/mbv_error_log.log ' , date ('d M, Y h:i:s A ' ) . PHP_EOL , FILE_APPEND );
78- file_put_contents ( '../logs/mbv_error_log.log ' , 'Error Code: ' . $ data ['error_code ' ] . PHP_EOL , FILE_APPEND );
79- file_put_contents ( '../logs/mbv_error_log.log ' , 'Error Message: ' . $ data ['error_message ' ] . PHP_EOL , FILE_APPEND );
80- }
92+ public function free ($ email ){
93+ if (trim ($ email ) != '' ) {
94+ try {
95+ $ params = [ 'email ' => $ email , 'key ' => $ this ->api_key , 'format ' => 'json ' , 'source ' => $ this ->source ];
96+ $ params2 = [];
97+ foreach ($ params as $ key => $ value ) {
98+ $ params2 [] = $ key . '= ' . rawurlencode ($ value );
99+ }
100+ $ params = implode ('& ' , $ params2 );
101+
102+ $ results = file_get_contents ($ this ->freeEmailApiUrl . '? ' . $ params );
103+
104+ if ($ results !== false ) {
105+ if (!isset ($ results ->error )) {
106+ $ data = json_decode ($ results ,true );
107+ if ( $ data ['is_free ' ] ) {
108+ return false ;
109+ } else {
110+ return true ;
111+ }
112+ } else {
113+ return true ;
114+ }
115+ } else {
116+ return true ;
117+ }
118+ } catch (Exception $ e ) {
119+ return true ;
120+ }
121+ } else {
122+ return true ;
123+ }
124+ }
81125
82126}
0 commit comments