Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 201
elvispt Avatar asked Sep 09 '10 11:09

elvispt


People also ask

How can I tell if SMTP is working in PHP?

Show activity on this post. 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. Just before fclose($f); added line fwrite($f, 'QUIT'. "\r\n"); .

How do I know if my SMTP server is working Linux?

To check if SMTP is working from the command line (Linux), is one critical aspect to be considered while setting up an email server. The most common way of checking SMTP from Command Line is using telnet, openssl or ncat (nc) command. It is also the most prominent way to test SMTP Relay.


1 Answers

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) ;
like image 108
Fanis Hatzidakis Avatar answered Oct 17 '22 08:10

Fanis Hatzidakis