0
votes


I try to write code for email verify like that
But i have one problem, 25 port not connect with any mail server
My code like that

Example.php

<?php

require('smtp-validate-email.php');

$from = '[email protected]'; // for SMTP FROM:<> command

$emails = '[email protected]';

$validator = new SMTP_Validate_Email($emails, $from);

$smtp_results = $validator->validate();

echo "<pre>";print_r($smtp_results);exit;

smtp-validate-email.php

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EmailValidation
 *
 * @author Harsukh Makwana  <[email protected]>
 */
class EmailValidation
{
    public $hellodomain = 'itsolutionstuff.com';
    public $mailfrom    = '[email protected]';
    public $rcptto;
    public $mx;
    public $ip;

    public function __construct()
    {
        $this->ip = '192.168.2.14';
    }

    public function checkEmail($email = null)
    {
        $this->rcptto = $email;
        $array        = explode('@', $this->rcptto);
        $dom          = $array[1];
        if (getmxrr($dom, $mx)) {
            $this->mx = $mx[rand(0, count($mx) - 1)];
            return $this->processRange($this->ip);
        }
        return false;
    }

    private function asyncRead($sock)
    {
        $read_sock   = array($sock);
        $write_sock  = NULL;
        $except_sock = NULL;
        
        if (socket_select($read_sock, $write_sock, $except_sock, 5) != 1) {
            return FALSE;
        }
        $ret = socket_read($sock, 512);
        return $ret;
    }

    private function smtpConnect($mta, $src_ip)
    {
        $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

        if ($sock == FALSE) {
            return array(FALSE, 'unable to open socket');
        }
        if (!socket_bind($sock, $src_ip)) {
            return array(FALSE, 'unable to bind to src ip');
        }

        $timeout = array('sec' => 10, 'usec' => 0);
        socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, $timeout);

        socket_set_nonblock($sock);
        var_dump(@socket_connect($sock, $mta, 25));exit;
        @socket_connect($sock, $mta, 25);

        $ret = $this->asyncRead($sock);
        if ($ret === FALSE) {
            return array(FALSE, 'inital read timed out');
        }

        if (!preg_match('/^220/', $ret)) { // Not a good connection.
            return array(FALSE, $ret);
        }

        // Now do the EHLO.
        socket_write($sock, "HELO ".$this->hellodomain."\r\n");
        $ret = $this->asyncRead($sock);
        if ($ret === FALSE) {
            return array(FALSE, 'ehlo timed out');
        }

        if (!preg_match('/^250/', $ret)) { // Not a good response.
            return array(FALSE, $ret);
        }

        // Now MAIL FROM.
        socket_write($sock, "MAIL FROM:<".$this->mailfrom.">\r\n");
        $ret = $this->asyncRead($sock);
        if ($ret === FALSE) {
            return array(FALSE, 'from timed out');
        }

        if (!preg_match('/^250/', $ret)) // Not a good response.
                return array(FALSE, $ret);

        // Now RCPT TO.
        socket_write($sock, "RCPT TO:<".$this->rcptto.">\r\n");
        $ret = $this->asyncRead($sock);
        if ($ret === FALSE) {
            return array(FALSE, 'rcpt to timed out');
        }

        if (!preg_match('/^250/', $ret)) {
            // Not a good response.
            return array(FALSE, $ret);
        }
        // All good.


        socket_close($sock);
        return array(true, $ret);
    }

    private function processRange($ip)
    {
        list($ret, $msg) = $this->smtpConnect($this->mx, $ip);
        $msg = trim($msg);
        return $ret;
    }
}

 

 

Output

Array
(
    [[email protected]] => 
    [domains] => Array
        (
            [gmail.com] => Array
                (
                    [users] => Array
                        (
                            [0] => harsukh
                        )
                    [mxs] => Array
                        (
                            [gmail-smtp-in.l.google.com] => 5
                            [alt1.gmail-smtp-in.l.google.com] => 10
                            [alt2.gmail-smtp-in.l.google.com] => 20
                            [alt3.gmail-smtp-in.l.google.com] => 30
                            [alt4.gmail-smtp-in.l.google.com] => 40
                            [gmail.com] => 0
                        )
                )
        )
)

Here [email protected] is a already exist, But i getting null responce
can i use another port for mail server?

1

1 Answers

0
votes

You can't use a port that's already in use. So probebly there is already an email server on the same machine where you are running your code.

can i use another port for mail server?

If you plan to use only clients written by yourself, so that you can change the port to which they send data, that can be acceptable, otherwise no.