Tarun Kumar
Tarun Kumar

Reputation: 1

verifying email from yahoo domain is not working in php

function smtp_validate_yahoo_email($email)
{
list($user, $domain) = explode('@', $email);

    // Specifically check for Yahoo domains
    $mxhosts = ['mta5.am0.yahoodns.net', 'mta6.am0.yahoodns.net', 'mta7.am0.yahoodns.net'];
    
    // Set timeout for socket connection
    $timeout = 10;
    $port = 25;
    $max_retries = 5;
    $initial_delay = 5;

    foreach ($mxhosts as $host) {
        $retry_count = 0;
        $delay = $initial_delay;

        while ($retry_count < $max_retries) {
            $connection = @fsockopen($host, $port, $errno, $errstr, $timeout);
            
            if ($connection) {
                fgets($connection, 1024);
                fputs($connection, "HELO example.com\r\n");
                fgets($connection, 1024);
                fputs($connection, "MAIL FROM: <[email protected]>\r\n");
                fgets($connection, 1024);
                fputs($connection, "RCPT TO: <$email>\r\n");
                $response = fgets($connection, 1024);
                
                if (strpos($response, '451') !== false || strpos($response, '421') !== false) {
                    fclose($connection);
                    $retry_count++;
                    sleep($delay);
                    $delay *= 2;  // Exponential backoff
                } else {
                    fputs($connection, "QUIT\r\n");
                    fclose($connection);
                    
                    return (strpos($response, '250') !== false) ? "Email is valid." : "Email is invalid.";
                }
            }
        }
    }

    return "Yahoo mail servers could not be reached.";
}

It is always return invalid, i also tried so many libraries like Egulias\EmailValidator,EmailVerifier\EmailVerifier.

If I tried with my smtp server then it will always return valid.

so can you please help me to solve the yahoo mail validaton in php

Upvotes: 0

Views: 45

Answers (0)

Related Questions