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

发布于 2024-10-12 07:28:01 字数 872 浏览 2 评论 0原文

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

我使用过 [电子邮件受保护]作为测试邮件,这是更易于阅读的格式的结果:

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: <[email protected]>
=250 OK
> RCPT TO: <[email protected]>
=250 OK

我知道服务器上必须打开端口 25。

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 [email protected] 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: <[email protected]>
=250 OK
> RCPT TO: <[email protected]>
=250 OK

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-10-19 07:28:01

您可以通过 dns_get_record() 检索 MX 记录

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

我不确定“ping”SMTP 服务器是什么意思?也许发邮件?您可以使用 PEAR 的 Mail_MIME 来做到这一点。

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.

苯莒 2024-10-19 07:28:01

要获取与给定 Internet 主机名对应的 MX 记录,您可以使用 getmxrr

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

要通过SMTP与邮件服务器通信,可以使用PEAR的Net_SMTP包.

mixed Net_SMTP::vrfy ( string $string )

该软件包还具有HELO、MAIL FROM 和 RCPT TO 方法

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

汐鸠 2024-10-19 07:28:01

如何获取电子邮件的 mx 主机:

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 . "\n<br>";
            if($show == 1) echo "IP " . gethostbyaddr($ip) . "\n<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\n";
    }
}

如何将电子邮件发送到 gmail:

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

// Login email and password
$login = "[email protected]";
$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\n";
        return;
    }else{
        // Http
        // fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
        // Smtp
        echo fread($socket,8192);
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

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

        echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

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

        // echo fwrite($socket, "MAIL FROM: <[email protected]>\r\n");
        // echo fread($socket,8192);

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

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

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

        echo fwrite($socket, "rcpt to: <[email protected]>\r\n");
        echo fread($socket,8192);

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

        echo fwrite($socket, "Date: ".time()."\r\nTo: <[email protected]>\r\nFrom:<[email protected]\r\nSubject:Hello from php socket tls\r\n.\r\n");
        echo fread($socket,8192);

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

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

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

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 . "\n<br>";
            if($show == 1) echo "IP " . gethostbyaddr($ip) . "\n<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\n";
    }
}

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 = "[email protected]";
$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\n";
        return;
    }else{
        // Http
        // fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
        // Smtp
        echo fread($socket,8192);
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

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

        echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

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

        // echo fwrite($socket, "MAIL FROM: <[email protected]>\r\n");
        // echo fread($socket,8192);

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

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

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

        echo fwrite($socket, "rcpt to: <[email protected]>\r\n");
        echo fread($socket,8192);

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

        echo fwrite($socket, "Date: ".time()."\r\nTo: <[email protected]>\r\nFrom:<[email protected]\r\nSubject:Hello from php socket tls\r\n.\r\n");
        echo fread($socket,8192);

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

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

        fclose($socket);
    }
}catch(Exception $e){
    echo $e;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文