如何使用PHP ping一个SMTP服务器并检查MX记录?

如何使用PHP ping一个SMTP服务器并检查MX记录?

问题描述:

How to, using PHP, ping an SMTP server and check MX records? I'm willing to write a script such as the one that can be found on http://bit.ly/z4RE

I've used aaa@mailinator.com as the test mail and this is the result in more human-readable format:

Result: Ok

Log:
MX record about mailinator.com exists.
Connection succeeded to mailinator.com SMTP.
220 mail.sogetthis.com ESMTP Postfix
> HELO verify-email.org
250 Hello
> MAIL FROM: <check@verify-email.org>
=250 OK
> RCPT TO: <aaa@mailinator.com>
=250 OK

I know that port 25 must be open on the server.

如何使用PHP ping SMTP服务器并检查MX记录? 我愿意写一个脚本,例如可以在 http://bit.ly/z4RE上找到的脚本。 > p>

我使用 aaa@mailinator.com code>作为测试邮件,这是更易于阅读的格式的结果: p>

 结果:Ok 
 
日期:关于mailinator.com的
 
MX记录存在。
 
 
连接成功发送到mailinator.com SMTP。
220 mail.sogetthis.com ESMTP Postfix 
&gt;  HELO verify-email.org 
250你好
&gt;  MAIL FROM:&lt; check@verify-email.org> 
 = 250 OK 
&gt;  RCPT TO:&lt; aaa@mailinator.com> 
 = 250 OK 
  code>  pre> 
 
 

我知道端口25必须在服务器上打开。 p> \ n div>

To get MX records corresponding to a given Internet host name you can use getmxrr:

bool getmxrr ( string $hostname , array &$mxhosts [, array &$weight ] )

To communicate with the mail server via SMTP, you can use PEAR'S Net_SMTP package.

mixed Net_SMTP::vrfy ( string $string )

The package also has methods for HELO, MAIL FROM and RCPT TO

You can retrieve the MX record via dns_get_record():

$rr = dns_get_record('example.com',DNS_MX);

I'm not sure, what you mean by "pinging" an SMTP server? Maybe send mail? That you can do with PEAR's Mail_MIME.

How to get mx hosts for email:

function getMX($hostname = "boo.xx", $show = 0){
    if(dns_get_mx($hostname, $mxhosts, $weights)) {
        $i = 0;
        $mxList = NULL;
        foreach($mxhosts as $key => $host) {
            if($show == 1) echo "Hostname: $host (Weight: {$weights[$key]}) <br>";
            $ip = gethostbyname($host);
            if($show == 1) echo "IP " . $ip . "
<br>";
            if($show == 1) echo "IP " . gethostbyaddr($ip) . "
<br>";
            $mxList[$i]['host'] = $host;
            $mxList[$i]['ip'] = $ip;
            $mxList[$i]['weight'] = $weights[$key];
            $i++;
        }
        return $mxList;
    } else {
        echo "Could not find any MX records for $hostname
";
    }
}

How to send email to gmail:

<?php
// Send with smtp ssl
// ini_set("SMTP","ssl://smtp.gmail.com");
// ini_set("smtp_port","465");

// Login email and password
$login = "your-email@cool.xx";
$pass = "123456";

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
    // echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    echo $socket = stream_socket_client('tcp://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$socket) {
        print "Failed to connect $err $errstr
";
        return;
    }else{
        // Http
        // fwrite($socket, "GET / HTTP/1.0
Host: www.example.com
Accept: */*

");
        // Smtp
        echo fread($socket,8192);
        echo fwrite($socket, "EHLO cool.xx
");
        echo fread($socket,8192);

        // Start tls connection
        echo fwrite($socket, "STARTTLS
");
        echo fread($socket,8192);

        echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

        // Send ehlo
        echo fwrite($socket, "EHLO cool.xx
");
        echo fread($socket,8192);

        // echo fwrite($socket, "MAIL FROM: <hello@cool.com>
");
        // echo fread($socket,8192);

        echo fwrite($socket, "AUTH LOGIN
");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($login)."
");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($pass)."
");
        echo fread($socket,8192);

        echo fwrite($socket, "rcpt to: <to-email@boome.com>
");
        echo fread($socket,8192);

        echo fwrite($socket, "DATA
");
        echo fread($socket,8192);

        echo fwrite($socket, "Date: ".time()."
To: <to-email@boome.com>
From:<zour-email@cool.xx
Subject:Hello from php socket tls
.
");
        echo fread($socket,8192);

        echo fwrite($socket, "QUIT 
");
        echo fread($socket,8192);

        /* Turn off encryption for the rest */
        // stream_socket_enable_crypto($fp, false);

        fclose($socket);
    }
}catch(Exception $e){
    echo $e;
}