PDA

View Full Version : php email validity checker


hafley
08-27-2005, 11:02 PM
i am making a contact form and i want a script which dosent just ckeck for email address structure such as xxx@xxx.com but rather looks up dns and sees whether or not it's a valid address. any ideas?

this is my contact form: contact.php

<style type="text/css">
img.verify { border: #999999 solid 1px; }
</style>
<form method="POST" action="contact_script.php" onSubmit="return ValidateForm()" name="contact">
<table width="480" border="0" cellspacing="3" cellpadding="1">
<tr align="left" valign="top">
<td width="21%"><font size="2" face="Arial">Full Name</font></td>
<td width="31%"> <input type="text" name="FirstName"></td>
<td width="16%">&nbsp;</td>
<td width="32%">&nbsp;</td>
</tr>
<tr align="left" valign="top">
<td><font size="2" face="Arial">Company</font></td>
<td><input type="text" name="Company"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr align="left" valign="top">
<td height="24"><font size="2" face="Arial">Email Address</font></td>
<td><input type="text" name="EmailFrom"></td>
<td colspan="2"> <font size="2" face="Arial"><em>example me@mycompany.com</em></font></td>
</tr>
<tr align="left" valign="top">
<td><font size="2" face="Arial">Contact Number</font></td>
<td><input type="text" name="ContactNumber"></td>
<td colspan="2"><font size="2" face="Arial"><em>example 916-123-4567</em></font></td>
</tr>
<tr align="left" valign="top">
<td height="54"><font size="2" face="Arial">how can we help you?</font></td>
<td colspan="3"><p>
<textarea name="Comments" cols="30" rows="5"></textarea>
</p></td>
</tr>
<tr align="left" valign="top">
<td height="26" colspan="4">for your security enter the text you see in
the box below
<input name="txtNumber" type="text" id="txtNumber2" value=""> </td>
</tr>
<tr align="left" valign="top">
<td height="27" colspan="3"><input type="submit" onClick="return checkmail(this.form.EmailFrom)" name="submit" value="Send">
</td>
<td height="29"><img src="randomImage.php" border="0" align="absbottom" class="verify"></td>
</tr>
</table>
</form>


and this is the form processor: contact_script.php

<?php
session_start();

$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "contact@digitalsunday.com";
$Subject = "Website Feedback";
$FirstName = Trim(stripslashes($_POST['FirstName']));
$Company = Trim(stripslashes($_POST['Company']));
$ContactNumber = Trim(stripslashes($_POST['ContactNumber']));
$Comments = Trim(stripslashes($_POST['Comments']));
$number = $_POST['txtNumber'];
$ip = $_SERVER['REMOTE_ADDR'];

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (md5($number) == $_SESSION['image_random_value']) $validationOK=true;
else $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php?p=error\">";
exit;
}

// prepare email body text
$Body = "";
$Body .= "First Name: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Contact Number: ";
$Body .= $ContactNumber;
$Body .= "\n";
$Body .= "Comments: ";
$Body .= $Comments;
$Body .= "\n";
$Body .= "IP Address: ";
$Body .= $ip;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php?p=thanks\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php?p=error\">";
}
?>

gunman
08-28-2005, 09:41 AM
Hi, i will send you one desicion:

$arr = explode("@",$mail); //where $mail is email address
$ch =gethostbyname("www.".$arr[1]);
if (is_int($ch[1])) //dns of mail is correct and exists
else //incorrect dns

SeeIT Solutions
08-28-2005, 11:39 AM
I found this one somewhere, it's great... and a lot more in depth than the one above.

function checkemail($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("[^@]{1,64}@[^@]{1,255}", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}

missing-score
08-28-2005, 01:33 PM
Personally, I prefer not to go too far with initial e-mail checking, and instead just require that a user validates their account... For example:

1. User signs up
2. User gets sent a validation e-mail to the e-mail address they registered with. The validation contains a unique code which is stored for the user.
3. If their validation is successful (they enter the right code/click the right link), then they become a valid member.

This method cant really be beated when it comes to putting in bad-emails... yes, people can enter false e-mails, but then... whats the point? You will just delete them a week later!

chump2877
08-28-2005, 02:04 PM
Personally, I prefer not to go too far with initial e-mail checking, and instead just require that a user validates their account... For example:

1. User signs up
2. User gets sent a validation e-mail to the e-mail address they registered with. The validation contains a unique code which is stored for the user.
3. If their validation is successful (they enter the right code/click the right link), then they become a valid member.

This method cant really be beated when it comes to putting in bad-emails... yes, people can enter false e-mails, but then... whats the point? You will just delete them a week later!

Thats seems like a lot of work for a contact form that may or may not require a user to be a "member"....

chump2877
08-28-2005, 02:25 PM
Hi, i will send you one desicion:

$arr = explode("@",$mail); //where $mail is email address
$ch =gethostbyname("www.".$arr[1]);
if (is_int($ch[1])) //dns of mail is correct and exists
else //incorrect dns

In order for $ch to be an array, don;t you have to use gethostbynamel() here? And then if there is only one IP address associated with that host name, shouldn;t you use $ch[0] instead, to be safe?

And, will an IP address every actually be an integer?

Otherwise I like what a short script like this could do for quick an easy e-mail validation... ;) Is this a full-proof method, though?

missing-score
08-28-2005, 03:49 PM
Thats seems like a lot of work for a contact form that may or may not require a user to be a "member"....

Ahh, thats me not reading properly again :P Sorry :)