Page 1 of 1

Check validity of domain

Posted: Thu Jan 15, 2004 10:22 am
by hawleyjr
Hello,

I have an email program that sends emails to my customer. However if the domain of the email is incorrect or their mail client is down I'm getting an error.

for example: (assuming that thisdomaindoesnotwork.com doesn't work)

Bad email: name@thisdomaindoesnotwork.com

This would cause an error because thisdomaindoesntowork.com is not a legit domain name.

Some thoughts I had were as follows but I'm not sure if I'm taking the right approach.

Use fopen() to open the webpage.
[syntax=php]fopen("http://www.thisdomaindoesnotwork.com");[/syntax]
Error check this to see if its unable to read the file then don't send email.

Potential problems:
The server locked/secure or doesn't have a page on the domain.
The domain is accessible by http://thisdomaindoesnotwork.com but not http://www.thisdomaindoesnotwork.com
The email domain is not the same as their web page therefore their email domain is unreachable via php’s fopen() function.

Thanks for the help.

Posted: Thu Jan 15, 2004 11:11 am
by kettle_drum
Theres not much that you can do really. Maybe try to do a dns lookup on the domain name to see if its registered. Other than that theres always pinging the mail port on the domain etc. But i think you will always find a few cercumstances that will slip through your checking.

Posted: Thu Jan 15, 2004 11:51 am
by hawleyjr
I don't mind having a "few cercumstances" where emails slip through my user group is very defined its just hard to account for user error. Is it possible to ping the server with php? I know I could do it through my server but I haven't heard of it being done w/ php.

Thanks for the help.

Posted: Thu Jan 15, 2004 12:22 pm
by timhortons
Hmmm, if safe mode is off, you could try some of the PHP shell commands, however, some security issues arise... but for example, you could do something like:

<?
$domain_name = 'whatever.com';

exec('ping -c 5 '.$domain_name);
?>

to run the ping command, and then do some string stuff to deduce whether it was successful in pinging or not.

Just my thoughts, dunno.

Posted: Thu Jan 15, 2004 2:19 pm
by kettle_drum
Just try to open a socket to the mail port on the domain:

Code: Select all

<?php

$portnumber = 25; #default mail port
$fp = fsockopen($domainname,$portnumber, &$err_num, &$err_msg, 30);

if($fp){
   echo "There seems to be a valid working mail server on $domainname";
}else{
   echo "This may be a fake email - do more tests";
}

fclose($fp);

?>