I will start off by saying I am not a coder and would really like some help.
My contact.php
form keeps returning this error message:
Invalid email address entered
when I test it on a server.
I have used this contact.php
form for a different website and it works fine. I read that the php code may now be deprecated but I'm not sure how to fix it.
This is the php code:
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');
$your_email = "dimasir@yahoo.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:
";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."
";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>
edit original note****This is my HTML code:
<form method="post" action="contact.php">
<input type="hidden" name="send" value="1" />
<fieldset>
<label>First Name </label>
<input name="" type="text" class="filed1" />
<label>Last Name</label>
<input name="" type="text" class="filed1" />
<label>Your Email </label>
<input name="" type="text" class="filed1" />
<label>Company</label>
<input name="" type="text" class="filed1" />
<label>Message </label>
<textarea name="" cols="" rows="" ></textarea>
<input type="image" src="images/send.jpg" />
</fieldset>
</form>
</div>
<!-- \ CONTACT BOX / -->
</div>
if(!preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',$email )){
$error.="Invalid email address entered";
$errors=1;
}
Should be this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error .= "Invalid email address entered";
$errors = 1;
}
PHP has a built in function filter_var() to validate email addresses which is more faster and accurate than using a regex.
Use this HTML code (Make sure to fill out the rest of the attributes):
<label>First Name </label>
<input name="f_name" type="text" class="filed1" />
<label>Last Name</label>
<input name="l_name" type="text" class="filed1" />
<label>Your Email </label>
<input name="email" type="text" class="filed1" />
<label>Company</label>
<input name="company" type="text" class="filed1" />
<label>Message </label>
<textarea name="message" cols="" rows="" ></textarea>
<input type="image" src="images/send.jpg" />
Change your line that goes like
if(!preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',$email )){
into
if(!preg_match('/^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)* @[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$/',$email )){
.
This should fix the problem.
If this doesn't work, try changing $email = $_POST['email'];
into $email = urldecode($_POST['email'])
;