Dear developer,
In order to check validation email address of email address text box, I’ve code the function as below. But unfortunately, it doesnt check the email (from) text box.
Here are my code…….
<?php
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!#$%&
↪’*+/=?^_.^(
$local_array[$i])) {
return false;
}
}
// Check if domain is IP. If not,
// it should be valid domain name
if (!ereg(“^[.]
$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;
}
$from = $_POST[“from”];
$subject = $_POST[“subject”];
$message = $_POST[“message”];
$header = “From: ” . $from . “rn”;
if(check_email_address($from))
{
if(empty($from))
{
echo “Error: Please Enter the Sender Email Address.”;
exit;
}
else
{
echo “Error: ‘$from’ is NOT a Valid Email Address.”;
exit;
}
}
$mailsent = mail(“[email protected]”, $subject, $message, $header);
if($mailsent)
{
echo “Congrats! The following message has been sent: <br><br>”;
echo “<b>To:</b> [email][email protected][/email]<br>”;
echo “<b>From:</b>$from<br>”;
echo “<b>Subject:</b> $subject<br>”;
echo “<b>Message:</b> $message<br>”;
echo $message;
}
else
{
echo “There was an error…”;
}
?>
Q : What’s wrong with these codes? Why it doesn’t check the valid email address in the from text box? Even I don’t type the email address in the from text box, it still sends the message to the reciever.
Cheers,
Visoth