Skip to content

Commit

Permalink
feat(app): add helper email check
Browse files Browse the repository at this point in the history
  • Loading branch information
dimonvelsk committed Sep 3, 2023
1 parent 73f82a9 commit 3d25c67
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 2 deletions.
8 changes: 7 additions & 1 deletion website/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"vlucas/phpdotenv": "^5.5",
"php-amqplib/php-amqplib": "^2.0"
},
"autoload": {
"psr-4": {
"App\\" : "src/"
}
},
"license": "MIT",
"authors": [
{
Expand All @@ -16,7 +21,8 @@
],
"minimum-stability": "stable",
"require-dev": {
"squizlabs/php_codesniffer": "^3.7"
"squizlabs/php_codesniffer": "^3.7",
"symfony/var-dumper": "^6.3"
},
"scripts": {
"phpcs": "phpcs --standard=PSR2"
Expand Down
153 changes: 152 additions & 1 deletion website/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions website/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@

declare(strict_types=1);

use App\Helpers\Email;

require 'vendor/autoload.php';

$emailRecords = [
'[email protected]',
'[email protected]',
'werewr.ertr',
'[email protected]'
];

try {
dump(Email::validateEmails($emailRecords));
} catch (RuntimeException $e) {
dump($e);
}
35 changes: 35 additions & 0 deletions website/src/Helpers/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Helpers;

class Email
{
protected static array $DNS = [];

public static function validateEmails(array $emails = []): array
{
$result = [];
foreach ($emails as $email) {
$result[] = filter_var($email, FILTER_VALIDATE_EMAIL) && static::checkDNS($email);
}

return array_combine($emails, $result);
}

public static function checkDNS(string $email): bool
{
$domain = substr(strrchr($email, '@'), 1);
if (isset(static::$DNS[$domain])) {
return static::$DNS[$domain];
}

try {
$resultEmail = getmxrr($domain, $mx_records) && count($mx_records) > 0;
} catch (\RuntimeException $e) {
$resultEmail = false;
}

static::$DNS[$domain] = $resultEmail;
return $resultEmail;
}
}

Check failure on line 35 in website/src/Helpers/Email.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 newline at end of file; 0 found

0 comments on commit 3d25c67

Please sign in to comment.