-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail-validation.php
52 lines (45 loc) · 1.86 KB
/
email-validation.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
<?php
/*
* Use https://emailvalidation.io/ API to create an application that
* allows to enter the email address and return if its valid or not.
*
* Send an email with basic "hello world" information to validated email
* You can use anything from different APis, mailgun, sparkpost, gmail, smtp services etc.
* TODO export API_KEY_EMAIL=yourownkey
* TODO export API_KEY_MAILGUN=yourownkey
*/
require 'vendor/autoload.php';
use Mailgun\Mailgun;
$appKeyValidation = getenv("API_KEY_EMAIL");
$emailToValidate = (string)readline("Enter the email address for validation: ");
$emailUrl = "https://api.emailvalidation.io/v1/info?apikey=$appKeyValidation&email=$emailToValidate";
$contextOptions = [
"http" => [
"ignore_errors" => true,
],
];
$context = stream_context_create($contextOptions);
$data = file_get_contents($emailUrl, false, $context);
$http_response_header = $http_response_header ?? [];
if (strpos($http_response_header[0], '200') === false) {
exit ("Error: Failed to fetch data. HTTP Response: " . $http_response_header[0] . "\n");
}
$emailsData = json_decode($data);
$apiKeyMailgun = getenv("API_KEY_MAILGUN");
$domain = "sandbox139eb76be3f444ad90f13165a0df1cf0.mailgun.org";
echo "Email validity score: $emailsData->score\n";
if ($emailsData->format_valid) {
try {
$mg = Mailgun::create($apiKeyMailgun);
$result = $mg->messages()->send($domain, [
'from' => 'Mailgun Sandbox <[email protected]>',
'to' => $emailToValidate,
'subject' => 'The PHP is awesome!',
'text' => 'hello world'
]);
} catch (\Mailgun\Exception\HttpClientException $e) {
echo "Mailgun API Exception: " . $e->getMessage();
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
echo "Mailgun API Exception Interface: " . $e->getMessage();
}
}