socket_connect()不适用于25端口

时间:2016-09-01 10:37:40

标签: php sockets smtp


我尝试编写电子邮件验证代码 但我有一个问题,25端口没有连接任何邮件服务器
我的代码就像那样

使用example.php

  

< PHP

     

要求( 'SMTP-验证-email.php');

     

$ from ='harsukh21@gmail.com'; //对于SMTP FROM:<>命令

     

$ emails ='harsukh@gmail.com';

     

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

     

$ smtp_results = $ validator-> validate();

     

echo“< pre>”; print_r($ smtp_results);退出;

SMTP-验证-email.php

  

<?php
/ *
*要更改此许可证标题,请在项目属性中选择许可证标题。
*要更改此模板文件,请选择工具|模板
*并在编辑器中打开模板。
* /


/ ** * * EmailValidation的说明
*
* *作者Harsukh Makwana< harsukh21@gmail.com>
* / /类EmailValidation
{
public $ hellodomain ='itsolutionstuff.com';
public public mailfrom = 'harsukh21@gmail.com';
public $ rcptto;
public $ mx;
public $ ip;

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

公共功能checkEmail($ email = null)
{
$ this - > rcptto = $ email;
$ array = explode('@',$ this-> rcptto);
$ dom = $ array [1];
if if(getmxrr( $ dom,$ mx)){
$ this-> mx = $ mx [rand(0,count($ mx) - 1)];
返回$ this-> processRange($ this - > ip);
} return false;
}
私有函数asyncRead($ sock)
{
$ read_sock = array($ sock);
$ write_sock = NULL;
$ except_sock = NULL;

如果(socket_select($ read_sock,$ write_sock,$ except_sock,5)!= 1){
return FALSE;
} < br /> $ ret = socket_read($ sock,512);
返回$ ret;
}

私有函数smtpConnect($ mta,$ src_ip)
{
$ sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

如果($ sock == FALSE){
返回数组(FALSE,'无法打开套接字');
} if(!socket_bind($ sock,$ src_ip)){
返回数组(FALSE,'无法绑定到src ip');
}


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

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

$ $ ret = $ this-&gt; asyncRead($ sock);
if if($ ret === FALSE){
返回数组(FALSE,'inital read timed out');
}

如果(!preg_match('/ ^ 220 /',$ ret)){//不是很好的连接。
返回数组(FALSE,$ ret);



//现在做EHLO。
socket_write( $ sock,“HELO”。$ this-&gt; hellodomain。“\ r \ n”);
$ ret = $ this-&gt; asyncRead($ sock);
if if($ ret == = FALSE){
返回数组(FALSE,'ehlo timed out');


如果(!preg_match('/ ^ 250 /',$ ret)){ //不是一个好的回应/>返回数组(FALSE,$ ret);
}

//现在MAIL FROM。
socket_write($ sock,“MAIL FROM:&lt;”。$ this- &gt; mailfrom。“&gt; \ r \ n”);
$ $ ret = $ this-&gt; asyncRead($ sock);
if if($ ret === FALSE){
返回数组(FALSE,'从超时'开始);
}

如果(!preg_match('/ ^ 250 /',$ ret))//不是一个好的回复。 />返回数组(FALSE,$ ret);

现在RCPT TO。
socket_write($ sock,“RCPT TO:&lt;”。$ this-&gt; rcptto。“ &gt; \ r \ n“);
$ $ ret = $ this-&gt; asyncRead($ sock);
if if($ ret === FALSE){
return array(FALSE, 'rcpt to timed out');


如果(!preg_match('/ ^ 250 /',$ ret)){
//不是一个好的回应。< br />返回数组(FALSE,$ ret);
}所有好的。


socket_close($ sock);
返回数组(true,$ ret);
}

私人功能processRange($ ip)
{
list($ ret,$ msg)= $ this-&gt; smtpConnect($ this-&gt; mx,$ ip) ; $ msg = trim($ msg);
返回$ ret;
}
}

 

输出

Array
(
    [harsukh@gmail.com] => 
    [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
                        )
                )
        )
)

这里harsukh@gmail.com已经存在,但我得到了无效的回应 我可以使用另一个端口用于邮件服务器吗?

1 个答案:

答案 0 :(得分:0)

您无法使用已在使用的端口。因此,在运行代码的同一台机器上已经存在一个电子邮件服务器。

  

我可以将另一个端口用于邮件服务器吗?

如果您打算只使用自己编写的客户端,以便可以更改发送数据的端口,那么这是可以接受的,否则为否。

相关问题