Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if ports 465 and 587 are open with PHP?

I'm trying to use PHPMailer to send e-mails with SMTP and gmail. The exact script am using works on other servers but it is not working on this particular hosting company's server.

I have checked the phpinfo() and it tells me that allow_url_fopen is on and there are no disabled_functions like fopen listed.

The script fails and it tells me either:

SMTP -> ERROR: Failed to connect to server: Connection timed out (110) 

or else

SMTP Error: Could not authenticate.

I'm assuming this is because it can not connect, because again this work on other servers and the authentication credentials are correct.

So I ask more generally, is there a way I can use PHP or jailshell ssh to check and see if the ports are actually open or not?

like image 653
cwd Avatar asked Apr 12 '12 16:04

cwd


People also ask

How do you check port 465 is open or not?

There are multiple ways to check if a particular port is blocked on your network, the simpliest one to check this is using the telnet command on your terminal as shown in the above screenshot. If Port 465 is blocked, you will get a connection error or no response at all.

How do you check if SMTP port is open or not?

Press Enter. Type telnet MAILSERVER 25 (replace MAILSERVER with your mail server (SMTP) which may be something like server.domain.com or mail.yourdomain.com). Press Enter. If that port is blocked, you will receive a connection error.

How check SMTP is working or not in PHP?

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'.


1 Answers

You can check for open/available ports with fsockopen:

$fp = fsockopen('127.0.0.1', 25, $errno, $errstr, 5);
if (!$fp) {
    // port is closed or blocked
} else {
    // port is open and available
    fclose($fp);
}

...where 5 is the timeout in seconds until the call fails.

This is probably due to a firewall issue where your hosting provider is blocking you from connecting to outbound sockets and/or specific ports. Keep in mind that it is a very usual security configuration to block outbound SMTP ports. Back in the day, only port 25 was blocked, but I'm starting to see more and more SSL variants being blocked as well.

Most providers and hosting companies will only allow you to connect to their own SMTP server to prevent spammers from relaying junk mail.

like image 118
netcoder Avatar answered Oct 13 '22 11:10

netcoder