April 27, 201214 yr Hi, I am creating a website from a template. There is a pre designed html form, which uses a contact.php page. When I try and use the form it keeps giving me the message "invalid email address entered" although it is a valid email address that I am entering into the form. I have had a look at the contact.php page and can see the refernces to the error message that I am seeing, but I do not understand the programming. Hre is a jsfiddle with the relevant informations. Can anyone help please. Dave My link
April 27, 201214 yr Try replacing !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$", with !preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', eregi is a function that is now deprecated in php and is generally being replaced by preg_match, so I am surprised that the previous line has been commented out. Some servers still support that function so I suspect the problem is with the regular expression being used. If it doesn't work, search the web for some other email regex and paste it in.
April 27, 201214 yr Try replacing !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$", with !preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', eregi is a function that is now deprecated in php and is generally being replaced by preg_match, so I am surprised that the previous line has been commented out. Some servers still support that function so I suspect the problem is with the regular expression being used. If it doesn't work, search the web for some other email regex and paste it in. mmm i have a better method if(!filter_var($email,FILTER_VALIDATE_EMAIL)){ //Invalid email }
April 27, 201214 yr Author Hi, would i replace: !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$", with: if(!filter_var($email,FILTER_VALIDATE_EMAIL)){ //Invalid email } sorry, I have no idea when it comes to programming
April 27, 201214 yr yea php filters are a good option, they were introduced with php 5.2 I think so as long as your server uses at least that version you're good to go. The filter is somewhat strict which is good but means it will not work on email addresses which have special characters - it does the trick for most though.
April 30, 201214 yr Author Thank you for your help, the form is now sending emails ok. I have a question, my sites are hosted by GoDaddy and when the form sends emails it obviously goes via thier email server and when it arrives at the requested inbox it says it comes from noreply@onlineserver.cc, is there a way of changing this?
April 30, 201214 yr Thank you for your help, the form is now sending emails ok. I have a question, my sites are hosted by GoDaddy and when the form sends emails it obviously goes via thier email server and when it arrives at the requested inbox it says it comes from noreply@onlineserver.cc, is there a way of changing this? I haven't tested it, but try this... <?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(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."{:content:}quot;,$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 = "info@mill-lock.co.uk"; $email_subject = "New Message: ".$_POST['subject']; $email_content = "new message:\n"; 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]."\n"; } } $headers = 'From: from@yourdomain.com' . "\r\n" . 'Reply-To: from@yourdomain.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if(@mail($your_email,$email_subject,$email_content,$headers)) { echo 'Message sent!'; } else { echo 'ERROR!'; } } ?> Edited April 30, 201214 yr by Marvedia
May 3, 201214 yr Hi, I am creating a website from a template. There is a pre designed html form, which uses a contact.php page. When I try and use the form it keeps giving me the message "invalid email address entered" although it is a valid email address that I am entering into the form. I have had a look at the contact.php page and can see the refernces to the error message that I am seeing, but I do not understand the programming. Hre is a jsfiddle with the relevant informations. Can anyone help please. Dave My link Why don't you add this function to your contact for to validate the email? function is_email( $email, $check_dns = false ) { // Test for the minimum length the email can be if ( strlen( $email ) < 3 ) { return false; } // Test for an @ character after the first position if ( strpos( $email, '@', 1 ) === false ) { return false; } // Split out the local and domain parts list( $local, $domain ) = explode( '@', $email, 2 ); // LOCAL PART // Test for invalid characters if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) { return false; } // DOMAIN PART // Test for sequences of periods if ( preg_match( '/\.{2,}/', $domain ) ) { return false; } // Test for leading and trailing periods and whitespace if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) { return false; } // Split the domain into subs $subs = explode( '.', $domain ); // Assume the domain will have at least two subs if ( 2 > count( $subs ) ) { return false; } // Loop through each sub foreach ( $subs as $sub ) { // Test for leading and trailing hyphens and whitespace if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) { return false; } // Test for invalid characters if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) { return false; } } // DNS // Check the domain has a valid MX and A resource record if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) { return false; } // Congratulations your email made it! return true; } Now use if(!is_email($email)){ echo "Invalid Email"; } Edited May 3, 201214 yr by Narendra
May 3, 201214 yr Why don't you add this function to your contact for to validate the email? function is_email( $email, $check_dns = false ) { // Test for the minimum length the email can be if ( strlen( $email ) < 3 ) { return false; } // Test for an @ character after the first position if ( strpos( $email, '@', 1 ) === false ) { return false; } // Split out the local and domain parts list( $local, $domain ) = explode( '@', $email, 2 ); // LOCAL PART // Test for invalid characters if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) { return false; } // DOMAIN PART // Test for sequences of periods if ( preg_match( '/\.{2,}/', $domain ) ) { return false; } // Test for leading and trailing periods and whitespace if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) { return false; } // Split the domain into subs $subs = explode( '.', $domain ); // Assume the domain will have at least two subs if ( 2 > count( $subs ) ) { return false; } // Loop through each sub foreach ( $subs as $sub ) { // Test for leading and trailing hyphens and whitespace if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) { return false; } // Test for invalid characters if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) { return false; } } // DNS // Check the domain has a valid MX and A resource record if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) { return false; } // Congratulations your email made it! return true; } Now use if(!is_email($email)){ echo "Invalid Email"; } lol im confused theres already a function to do this which is called filter_var
Create an account or sign in to comment