How to check, by PHP, if my script is connecting to SMTP server

前端 未结 3 1756
猫巷女王i
猫巷女王i 2020-12-19 13:04

Simply what the title says. Want to know how to check if the connection is working and if not, what is the error. Btw the SMTP server is exchange 2007.

相关标签:
3条回答
  • 2020-12-19 13:55

    If you want to know if you can access the SMTP server from wherever you are running PHP, then you just need to connect to it on the appropriate port (default 25) and see if you get back a "220" code in the result.

    $f = fsockopen('smtp host', 25) ;
    if ($f !== false) {
        $res = fread($f, 1024) ;
        if (strlen($res) > 0 && strpos($res, '220') === 0) {
            echo "Success!" ;
        }
        else {
            echo "Error: " . $res ;
        }
    }
    fclose($f) ;
    
    0 讨论(0)
  • 2020-12-19 13:56

    Since this is most likely related to your other question: Configure mail server to work with PHP, I'll put the answer here too:

    You're using the PEAR Mail package. The send() method returns TRUE on success, or a PEAR_Error object otherwise, which will contain details of the failure. Most likely you'd want $PEAR_Error::message. Full details here: Configure mail server to work with PHP

    0 讨论(0)
  • 2020-12-19 14:04

    it could be due to your SMTP port is not allowed on firewall so Try this php script to check open ports, it's have maximum popular port checking:

    <?php
    
    $ports[] = array('host'=>'interspire.smtp.com','number'=>25);
    $ports[] = array('host'=>'interspire.smtp.com','number'=>2525);
    $ports[] = array('host'=>'interspire.smtp.com','number'=>25025);
    $ports[] = array('host'=>'helpme.interspire.smtp.com','number'=>80);
    
    $ports[] = array('host'=>'google.com','number'=>80);
    $ports[] = array('host'=>'smtp.gmail.com','number'=>587);
    $ports[] = array('host'=>'smtp.gmail.com','number'=>465);
    $ports[] = array('host'=>'pop.gmail.com','number'=>995);
    $ports[] = array('host'=>'imap.gmail.com','number'=>993);
    
    $ports[] = array('host'=>'ftp.mozilla.org','number'=>21);
    $ports[] = array('host'=>'smtp2go.com','number'=>8025);
    
    $ports[] = array('host'=>'relay.dnsexit.com','number'=>25);
    $ports[] = array('host'=>'relay.dnsexit.com','number'=>26);
    $ports[] = array('host'=>'relay.dnsexit.com','number'=>940);
    $ports[] = array('host'=>'relay.dnsexit.com','number'=>8001);
    $ports[] = array('host'=>'relay.dnsexit.com','number'=>2525);
    $ports[] = array('host'=>'relay.dnsexit.com','number'=>80);
    
    $ports[] = array('host'=>'mail.authsmtp.com','number'=>23);
    $ports[] = array('host'=>'mail.authsmtp.com','number'=>25);
    $ports[] = array('host'=>'mail.authsmtp.com','number'=>26);
    $ports[] = array('host'=>'mail.authsmtp.com','number'=>2525);
    
    foreach ($ports as $port)
    {
        //$connection = @fsockopen($port['host'], $port['number']);
        $connection = @fsockopen($port['host'], $port['number'], $errno, $errstr, 5); // 5 second timeout for each port.
    
        if (is_resource($connection))
        {
            echo '<h2>' . $port['host'] . ':' . $port['number'] . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";
    
            fclose($connection);
        }
    
        else
        {
            echo '<h2>' . $port['host'] . ':' . $port['number'] . ' is not responding.</h2>' . "\n";
        }
    }
    
    
    ?>
    

    Source From: https://www.interspire.com/support/kb/getattachment.php?data=MTA2OHxwb3J0Y2hlY2sucGhw

    0 讨论(0)
提交回复
热议问题