Check validity of domain

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Check validity of domain

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
timhortons
Forum Commoner
Posts: 29
Joined: Thu Jan 15, 2004 11:48 am
Location: Calgary, AB, Canada

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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);

?>
Post Reply