My web host has closed off port 25 to user code as it was being used by other users to send spam. Unfortunately, I have the following code which I use to validate email addresses:

function fixed_gethostbyname ($host) {
    // Try the lookup as normal...
    $ip = gethostbyname($host);
    // ...but if it fails, FALSE is returned instead of the unresolved host
    if ($ip != $host) { return $ip; } else return false;
}
function verifyemail() {
        global $domain, $username, $email, $HTTP_HOST;
        $domainexists = fixed_gethostbyname($domain);
        $mxexists = checkdnsrr("$domain.", 'MX');
        if (!$domainexists && !$mxexists) return false;
        else if (!$mxexists) return false;
        else {
                if (getmxrr($domain, $MXHost))  {
                        $ConnectAddress = $MXHost[0];
                } else {
                        $ConnectAddress = $domain;
                }
                $Connect = @fsockopen ( $ConnectAddress, 25 );
                if ($Connect) {
                        if (ereg("^220", $Out = fgets($Connect, 1024))) {
                                fputs ($Connect, "HELO $HTTP_HOST\r\n");
                                $Out = fgets ( $Connect, 1024 );
                                fputs ($Connect, "MAIL FROM: <{$email}>\r\n");
                                $From = fgets ( $Connect, 1024 );
                                fputs ($Connect, "RCPT TO: <{$email}>\r\n");
                                $To = fgets ($Connect, 1024);
                                fputs ($Connect, "QUIT\r\n");
                                fclose($Connect);
                                if (!ereg ("^250", $From) || !ereg ( "^250", $To )) {
                                        return false;
                                }
                        } else {
                                return false;
                        }
                } else {
                        return false;
                }
        }
        return true;
}

Yes I know there's better code, but this does the trick for me well enough for now. The problem is that it doesn't do the trick any more, because port 25 is closed.

Is there a way within PHP to have my message leave on another port, say 625, and arrive at port 25 at the other end?

Dani AI

Generated

Short expert note for (and ):

What says about fsockopen is technically true — you can open whatever local TCP port your host allows — but that does not make a remote mail server accept SMTP on a nonstandard port. Recipient MX servers normally listen on TCP/25. The only way your traffic can leave on a different port and still be delivered to the recipient on port 25 is to send it to an SMTP relay that accepts connections on that alternate port and then forwards the message to the destination MX on port 25.

Practical, reliable options:

  • Ask your host for their outgoing SMTP relay (they usually provide a submission service that accepts on port 587 or 465 with authentication). Send mail through that relay rather than connecting directly to recipient MX hosts.
  • Use a mature mail library (PHPMailer, SwiftMailer, Symfony Mailer) so you get SMTP auth, STARTTLS and proper error handling instead of rolling your own socket SMTP with fsockopen.
  • If you only need to confirm addresses, stop relying on RCPT checks — many MTAs use greylisting, catch-alls or anti-probing rules that make RCPT results unreliable. Use DNS/MX sanity checks plus a double opt-in confirmation email for true verification.
  • If your host refuses to provide a relay, consider a transactional email provider (SendGrid/Mailgun/SES) which exposes an API or an authenticated SMTP endpoint on non-25 ports.

Quick troubleshooting notes: raw sockets to port 587 often fail unless you implement STARTTLS and authentication (not trivial). Don’t try to bypass host port filters — that can violate terms of service. The safest, most future-proof path is authenticated submission through your host or a reputable mail service and a confirmation email flow.

fsockopen is setting the port number for you to write to. If you change it from 25 to 625 (assuming that is what your hosting company accepts emails on) then it will be delivered. You don't have to worry about the destination port.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.