Skip to content

Commit 07b8b21

Browse files
Update to support for v2 API.
1 parent e6b89d9 commit 07b8b21

3 files changed

Lines changed: 133 additions & 76 deletions

File tree

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,24 @@ Open the terminal, locate to your project root and run the following command :
1818
`composer require mailboxvalidator/mailboxvalidator-cakephp`
1919

2020

21+
If you want to manually install this plugin, firstly clone the plugin folder to the plugins folder under your website project. After that, add the following line into your project's composer.json file like this:
22+
23+
```json
24+
{
25+
....
26+
"autoload": {
27+
"psr-4": {
28+
....
29+
"MailboxValidatorCakePHP\\": "plugins/mailboxvalidator-cakephp/src/"
30+
}
31+
},
32+
}
33+
```
2134

22-
If you want to manually install this plugin, firstly clone the plugin folder to the plugins folder under your website project. After that, include the following line in config/bootstrap.php:
35+
Remember to run this command to autoload our plugin:
2336

24-
```php
25-
Plugin::load('mailboxvalidator-cakephp', ['bootstrap' => false, 'routes' => true, 'autoload' => true]);
37+
```bash
38+
composer dumpautoload
2639
```
2740

2841

@@ -74,7 +87,7 @@ $MBV = new MailboxValidatorController();
7487
Add the below line right after the $validator:
7588

7689
```php
77-
->provider('mbv', $MBV)
90+
->setProvider('mbv', $MBV)
7891
```
7992

8093
After that, add a new rule to your form field. For example, if you want to validate the disposable email, your rule will be like this:
@@ -106,4 +119,4 @@ The validators available to validate the email are: single, free and disposable.
106119

107120
## Copyright
108121

109-
Copyright (C) 2018-2020 by MailboxValidator.com, support@mailboxvalidator.com
122+
Copyright (C) 2018-2023 by MailboxValidator.com, support@mailboxvalidator.com

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"license": "MIT",
1414
"require": {
15-
"cakephp/cakephp": "^3.5"
15+
"cakephp/cakephp": "^5.*"
1616
},
1717
"autoload": {
1818
"psr-4": {

src/Controller/MailboxValidatorController.php

Lines changed: 114 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,122 @@
55

66
class 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

Comments
 (0)