Hi all,
I have a form for people to sign up for an online newsletter and I am trying to add code to check for a valid MX record for the server. The issue is, it always return TRUE.
I am not sure where I am going wrong. Here is my code.
<?
//get the email info and date - time
$EmailAddress = addslashes($_POST['EmailAddress']);
$Date = date('Y\-m\-d');
$DisplayDate = date('F\ d\, Y');
$DisplayDate2 = date('d\-M\-y');
$Time = date('g\:i A', time() + 10800);
$email = $EmailAddress;
////////////Back Function
function Back() {
echo"<p align='center'><font face='Arial' size='4' color='#FF0000'><a href='javascript:history.go(-1)'><br><br><br>You did not enter a valid email adrress.<br><br><br>Please click here to go back to correct your entry.</a></font></div><br><br>";
}
////////////
////////////WriteFiles Function
function WriteFiles(){
//saving record in a text file
$outputtext = addslashes("".$email."".","
. ","
. "".$DisplayDate.","
. "".$Time.""
. "\r\n");
// Create the text file name from the stat picked
$write_file_txt = "x.txt";
$fp = fopen($write_file_txt, 'a+') or die("Couldn't open ".$write_file_txt);
if (!$fp)
{
echo '<p><strong> Your application could not be saved to the server at this time.</strong></p>';
echo '<p><strong> Please try again later.</strong></p></body></html>';
exit;
}
// Write out the text data
fwrite($fp, $outputtext, strlen('--'.$outputtext));
fclose($fp);
// Create the text file name from the stat picked
$outputcsv = $email.","
. "".","
. $DisplayDate2.","
. $Time.","
. "\r\n";
$write_file_csv = "x.csv";
$fp2 = fopen($write_file_csv, 'a+') or die("Couldn't open ".$write_file_csv);
if (!$fp2)
{
echo '<p><strong> Your application could not be saved to the server at this time.</strong></p>';
echo '<p><strong> Please try again later.</strong></p></body></html>';
exit;
}
// Write out the text data
fwrite($fp2, $outputcsv, strlen('--'.$outputcsv));
fclose($fp2);
}
//////////////////
///////////////Create the Function to check for a valid email address
function check_email_address($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;
}
/////////////////////////
//Tell them if it's a bad address or write out the text and csv files if it's valid
if (check_email_address($email)) {
echo WriteFiles();
} else {
echo $email;
echo '<br> is not a valid email address.';
die ("");
}
/////Continue and write out the files
?>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#111111" style="border-collapse: collapse">
<tr>
<td width="50%" height="151" valign="middle" bgcolor="#FFFFFF">
<p align="center"><span class="font"><font face="Arial" color="#FFFFFF"><strong><font face="Arial" size="2"><img src="../../images/itcm_logo.jpg" alt="In The Classroom Media" width="215" height="87"></font></strong></font></span>
<p align="center"><span class="font"> Thank you for joining the In The Classroom
Media Email Newsletter! We will periodically contact you with the latest
developments. In the meantime, please send any comments to <a href="mailto:info@intheclassroom.org">info@intheclassroom.org<br>
</a>
<br>
<a href="javascript: self.close()">Click here to close this window.</a></span></p></td>
</tr>
</table>