Lesson 20 - Another test of the email address
Features
OK. so the reuestor typed in what appears to be a valid email address. But, does the domain really exist? After all, we are going to try to send email to it.
|

Click here to see a larger image
|
This catches those typos where a person types .cmo instead of .com. What this does not detect is an invalid email address where the domain is valid. For example, yourname@ibm.com is not a valid email address at IBM, but we have no way of checking that.
Actions
 |
Click to see Lesson20_DNSCheck.php in action on this tutorial site. Note: if you enter name an email in this form, you will send me an email. |
Download this file: Lesson20_DNSCheck.txt to your PC. Change the recipient address in the Lesson20 file and the pointers to the style sheet and then upload all to your website. The thank yous should be there from the previous lesson.
Surf to the Lesson20_DNSCheck.php file on your website with your browser. You should see results similar to the image above on the right.
After completing the form and clicking Send me more info, you should receive an email with this in the body:
Please send me more information about Acme products
First Name = your first name
Last Name = your last name
Email = you@yourdomain.com
And, you should receive a confirming email.
And, you should be sent to the Thank You page.
Gotchas
Ah, yes. The function checkdnsrr() is not available in the Windows version of PHP.
Source Code
We added another error trap on $contactEmail. We use checkdnsrr() to determine if the domain name in the email address is valid.
| |
<?php
ob_start(); /* Start buffer so we can use header anywhere in the page */
?>
<html>
<head>
<title>A Request Info Form that checks the email domain.</title>
<link rel="stylesheet" href="/PHP_Tutorials/Styles/PHP_Tutorials_Forms_Style.css" type="text/css">
</head>
<body>
<h1>Request Information from the Acme Company</h1>
<form method="POST" name="FirstEmailForm" action="<?php echo($PHP_SELF); ?>" >
<table width="550" border="0" cellspacing="0" cellpadding="0" bgcolor="#DFDFDF">
<tr>
<td colspan="2" class="FormCategoryName">About You</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
First Name:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactFirstName" value="<?php print $_POST['contactFirstName']; ?>" >
<span class="Footer" style="color:red">* = Required</span>
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
Last Name:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactLastName" value="<?php print $_POST['contactLastName']; ?>" >
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
Email:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactEmail" value="<?php echo $_POST['contactEmail']; ?>" >
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
</td>
<td width="350">
<input type="hidden" name="firstPass" value="No">
<input type="submit" name="subRequestButton" value="Send me more info">
<br>
<span class="Footer">Please click only once.</span>
</td>
</tr>
</table>
</form>
<?php
/* Set variables equal to their "posted" values". We need to do this because "register_globals = off" is the default. */
$contactFirstName = $_POST['contactFirstName'];
$contactLastName = $_POST['contactLastName'];
$contactEmail = $_POST['contactEmail'];
$firstPass = $_POST['firstPass'];
if($firstPass == "No")
{
/* If the user skipped required fields or entered invalid values, write an appropriate error message. */
if ($contactFirstName == "")
exit("<p class='PhpError'>Your First Name is missing.</p>");
if ($contactLastName == "")
{
exit("<p class='PhpError'>Your Last Name is missing.</p>");
}
if (!(eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$",$contactEmail))):
exit("<p class='PhpError'>Email Address appears to be invalid.</p>");
endif;
$emailDomain = ltrim(strstr($contactEmail, '@'), '@');
if(!(checkdnsrr($emailDomain, ANY)))
exit("<p class='PhpError'>Email Address Domain can not be found on the internet.</p>");
/* Construct the email messages */
$messageToAcme = "Please send me more information about Acme products";
$messageToAcme .= "\n\n First Name = " . $contactFirstName;
$messageToAcme .= "\n Last Name = " . $contactLastName;
$messageToAcme .= "\n Email = " . $contactEmail;
$fullName = $contactFirstName ." " .$contactLastName;
$mailAcmeHeaders = "From: \"".$fullName."\"<".$contactEmail.">\n";
$mailAcmeHeaders .= "Reply-to: ".$contactEmail;
$messageToRequestor = "Thanks, " .$contactFirstName .", for requesting information about Acme products";
$messageToRequestor .= "\n Your email will totally ignored just like 38% of the F500 ignore emails.";
$fullName = $contactFirstName ." " .$contactLastName;
$mailRequestorHeaders = "From: \"Dave Barnes\"<dave@marketingtactics.com>\n";
$mailRequestorHeaders .= "Reply-to: " ."dave@marketingtactics.com";
/* You need to change dave@marketingtactics.com to YOUR email address */
if (mail("dave@marketingtactics.com", "Please Send Acme Info", $messageToAcme, $mailAcmeHeaders))
{
mail ($contactEmail, "Acme Request received", $messageToRequestor, $mailRequestorHeaders);
header("Location: ../../ThankYous/Thanks_Request_Info_Success.html"); /* Redirect browser */
exit; /* Make sure that code below does not get executed when we redirect. */
}
else
{
header("Location: ../../ThankYous/Thanks_Request_Info_Failure.html");
mail("webmaster@marketingtactics.com", "Acme, Request Info Failure", $messageToAcme, $mailAcmeHeaders);
exit;
}
}
?>
</body>
</html>
<?php
ob_end_flush();
?>
|
Final Thoughts
None.
|