无法使用 PHP shell_exec() 执行 telnet 命令

Cannot execute telnet commands using PHP shell_exec()

我正在尝试使用 telnet 和 php 进行邮件验证。整个过程在terminal里没问题,但是当我使用php shell_exec()时,它只运行到Telnet连接命令。连接 telnet 后,其余的 telnet 特定命令将不再起作用。

是否有其他方法需要我们使用 php 执行 telnet?

更新:我正在尝试复制 this 教程。

这是我的代码

public function mailtest(Request $request){
        //Taking input email
        $email = $request->input("email");
        $divide = explode("@", $email);
        //Find out the Domain
        $server = $divide[1];
        $response = shell_exec("nslookup -q=mx $server");
        //Response of the nslookup
        print_r($response);
        $mailServerList = explode(PHP_EOL, $response);
        $line = $mailServerList[4];

        $serverArr = preg_split('/\s+/', $line);
        $n = sizeof($serverArr);
        $mailServer = $serverArr[$n-1];
        //Printing out the mail server out of the nslookup response
        print_r($mailServer);
        //Executing Telnet command
        $telnet = shell_exec("telnet $mailServer 25");
        print_r("telnet response ".$telnet);

        //Telnet Helo
        $helo = shell_exec("Helo testing.com");
        print_r("Helo response ".$helo);
        //Telnet mail from 
        $from = shell_exec('mail from: testing@gmail.com');
        print_r("MAil from response ".$from);
        //Telnet RCPT to
        $finalResponse = shell_exec("rcpt to: $email");
        print_r("Mail to response ".$finalResponse);
    }

这是回复

Server:     10.0.80.11
Address:    10.0.80.11#53

Non-authoritative answer:
gmail.com   mail exchanger = 5 gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 40 alt4.gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 30 alt3.gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 20 alt2.gmail-smtp-in.l.google.com.

Authoritative answers can be found from:
gmail-smtp-in.l.google.com  internet address = 173.194.64.27
gmail-smtp-in.l.google.com  has AAAA address 2607:f8b0:4003:c02::1a
alt1.gmail-smtp-in.l.google.com internet address = 173.194.219.27
alt1.gmail-smtp-in.l.google.com has AAAA address 2607:f8b0:4002:c03::1b
alt4.gmail-smtp-in.l.google.com internet address = 64.233.190.26
alt4.gmail-smtp-in.l.google.com has AAAA address 2800:3f0:4003:c01::1b
alt3.gmail-smtp-in.l.google.com internet address = 74.125.141.26
alt3.gmail-smtp-in.l.google.com has AAAA address 2607:f8b0:400c:c06::1a
alt2.gmail-smtp-in.l.google.com internet address = 173.194.205.27
alt2.gmail-smtp-in.l.google.com has AAAA address 2607:f8b0:400d:c02::1b

gmail-smtp-in.l.google.com.telnet response Trying 2607:f8b0:4003:c02::1a...
Connected to gmail-smtp-in.l.google.com.
Escape character is '^]'.
Helo response 
MAil from response No message, no subject; hope that's ok
Mail to response 

shell_exec() 开始一个新进程。它首先启动一个 shell ,然后将给定的字符串作为命令执行。您不能使用多个 shell_exec() 命令与同一个 shell 进行交互,例如远程控制终端。

对于你的情况,我会做的是尝试在现有代码的基础上构建。比如现有一堆PHP SMTP libraries。我很确定他们中至少有一个能够做你想做的事,或者至少向你展示一种实现缺失功能的方法。

如果您真的坚持自己动手,请查看现有答案,例如interacting with command line program or PHP's expect module。除非绝对需要,否则我会避免这样做,因为它效率低下。此外,出于安全原因,网络服务器通常被配置为不允许启动新进程,因此如果您的代码在您 运行 从命令行而不是在网络服务器内部运行时有效,请记住这一差异。

shell_exec 不适合(参见 Ulrich 的解释)。

fsockopen 应该可以解决问题。

$fp = @fsockopen('yourMailServer.com', 9001);      

if ($fp) { 
    fwrite($fp, "username\n");
    fwrite($fp, "password\n");

    while ($line = fread($fp, 2048)) {    
       // do things with $line    
    }    
} else {    
    //return error    
}

在参考了@Lavi 的回答后,我就是这样解决问题的。 fputs 成功了,而不是 fwrite

$connect = @fsockopen($mailServer, 25);
if($connect){
    fputs ($connect , "HELO $mailServer\r\n");
    $out = fgets ($connect, 1024);
    $details .= $out."\n";

    fputs ($connect , "MAIL FROM: <$fromemail>\r\n");
    //$from = fgets ($connect, 1024);
    fputs ($connect , "RCPT TO: <$toemail>\r\n");
    //$to = fgets ($connect, 1024);
    fputs ($connect , "QUIT");
    fclose($connect);
}