Contact Form Validating Email & Non-Latin Characters - php

I am having problems making a website with a contact form.
First of all, any email address entered it spews out as invalid.
Also, I have a expectancy for the name, and I'm not sure how to add special characters (Eg. É) and spaces for the last name.
Here is the existing PHP:
$error_message = "";
$email_exp = "/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z];{2,4}$/";
if (!preg_match($email_exp, $email)) {
$error_message .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
}
if(strlen($subject) < 3){
$error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';
}
if(strlen($message) < 2){
$error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';
}
Updated PHP for #J. Robertson:
<?php
if (isset($_POST['email'])){
// email info
$email_to ="example#example.com";
$email_subject = $_POST['subject'];
$email_from = $_POST['email'];
//error code
function died($error) {
echo "I am sorry, but there seems to be error(s) found within the form you submitted. <br/>";
echo "The error(s) will appear below: <br/> <br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these error(s). <br/>";
die();
}
//validation
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('I am sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//expected strings
$error_message = "";
$email_exp = '/[^\s]*#[a-z0-9.-]*/i';
if (!preg_match($email_exp, $email)) {
$error_message .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
}
if(strlen($subject) < 3){
$error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';
}
if(strlen($message) < 2){
$error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';
}
if(strlen($error_message) > 0 ){
died($error_message);
}
$email_message = "Form Details below.\n\n";
//sanitization
function clean_string($string){
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string($name) . "\n";
$email_message .= "E-Mail:" . clean_string($email) . "\n";
$email_message .= "Subject:" . clean_string($subject) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";
//email headers
$headers = 'From: ' .$email_From . '\r\n'. 'Reply-To' .
$email. "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
}
?>

I suspect the problem lies with your reg ex. Using http://gskinner.com/RegExr/ to test it against test#test.com, your reg ex does not pick up even that basic email address.
It's highly unlikely you'll come up with a regex that covers all email possibilities- it may be more worthwhile to simply check for an '#' sign, and then send a test email to the server to see if it's valid.
Otherwise, if you'd like a valid regex to test against, try
/[^\s]*#[a-z0-9.-]*/i

Related

Why is my auto respond code not working?

I have a contact form that sends me the information that has been submitted. The problem is that it doesn't send an email to the client to confirm the submission. The complete code is listed below. If anyone can tell me what I am missing or what I should change, it would be greatly appreciated.
<?php
if(isset($_POST['Email_Address'])) {
include 'freecontactformsettings.php';
function died($error) {
echo "Sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['Full_Name']) ||
!isset($_POST['Email_Address']) ||
!isset($_POST['Telephone_Number']) ||
!isset($_POST['Your_Message']) ||
!isset($_POST['AntiSpam'])
) {
died('Sorry, there appears to be a problem with your form submission.');
}
$full_name = $_POST['Full_Name']; // required
$email_from = $_POST['Email_Address']; // required
$telephone = $_POST['Telephone_Number']; // not required
$comments = $_POST['Your_Message']; // required
$antispam = $_POST['AntiSpam']; // required
if (isset($_POST['newsletter'])) {
$newsletter = "yes";
} else {
$newsletter = "no";
}
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(preg_match($email_exp,$email_from)==0) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($full_name) < 2) {
$error_message .= 'Your Name does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if($antispam <> $antispam_answer) {
$error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\r\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:");
return str_replace($bad,"",$string);
}
$email_message .= "Full Name: ".clean_string($full_name)."\r\n";
$email_message .= "Email: ".clean_string($email_from)."\r\n";
$email_message .= "Telephone: ".clean_string($telephone)."\r\n";
$email_message .= "Message: ".clean_string($comments)."\r\n";
$email_message .= "Newsletter: ".clean_string($newsletter)."\r\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
header("Location: $thankyou");
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
if ($email->send()){
$autoemail = new PHPMailer();
$autoemail->From = "noreply#altfuels.com";
$autoemail->FromName = "Alt Fuels";
$autoemail->AddAddress($email_from->Email Address, $full_name->Full Name);
$autoemail->Subject = "Autorepsonse: We received your submission";
$autoemail->Body = "We received your submission. We will contact you soon ...";
$autoemail->Send();
}
<?php
}
die();
?>
(*note: I changed some of the newsletter code print a "no" answer to my email.)
Looks like you are referencing $thankyou which is undefined, unless it was defined in freecontactformsettings.php which you have not listed.

How to customise the php script so that the feedback is exactly the same as the user have typed out in a form?

Does anyone have any idea how to customize the php page so that the user can see a feedback of what he has already filled in? I noticed many php scripts have a 'thank you' feedback but I want more than that. The codes for the php as shown below :
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "james#ace.com.sg";
$email_subject = "Photography Courses that I want to sign up";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['tel']) ||
/*!isset($_POST['basic']) ||
!isset($_POST['advanced']) ||
!isset($_POST['raw']) ||
!isset($_POST['lightroom']) ||*/
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['tel']; // required
$basic = $_POST['basic']; // not required
$advanced = $_POST['advanced']; // not required
$raw = $_POST['raw']; // not required
$lightroom = $_POST['lightroom']; // not required
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
/*if(!preg_match($string_exp,$telephone)) {
$error_message .= 'You need to insert the telephone number.<br />';
}*/
/* if(strlen($comments) < 2) {
$error_message .= 'Insufficient Words for comments! <br />';
}*/
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "\n Name: ".clean_string($name)."\n\n";
$email_message .= "Email: ".clean_string($email_from)."\n\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n\n";
$email_message .= "Courses Taking: \n\n".clean_string($basic)."\n\n" .clean_string($advanced)."\n\n" .clean_string($raw)."\n\n" .clean_string($lightroom)."\n\n";
$email_message .= "Comments: ".clean_string($comments)."\n\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<?php
echo '<h2>Thanks for filling up this form, We will get back to you by next Monday (29 Sep). Have a terrific weekend!</h2>';
echo '<p>**How do I feedback of what the user has already filled in the form here?** </p>';
?>
<?php
$redirect = 'http://www.acetraining.com.sg';
?>
<?php
}
?>
<SCRIPT LANGUAGE="JavaScript">
redirTime = "4000";
redirURL = "<?php echo $redirect ?>";
function redirTimer() {
self.setTimeout("self.location.href=redirURL;",redirTime);}
</script>
<BODY onLoad="redirTimer()">
Thanks!
Create a Variable for date in your if(isset) condition. after this line
if(isset($_POST['submit'])){
$date=date("j M \(D\)");
$date2=date("l \(j D)", strtotime($Date. ' + 3 days'));
//I added Three days. You can its +3, you can add any number of days. its up to you
Read you last commment its according to that.
I want the result to look something like this
echo 'Thanks for filling up the form This is the confiermation what had filled up:' .
'<br/>'. 'Name: '.$name.' Email: ' .$email_from.' Tel: '. $telephone .' Courses: '.
$advanced. ' '.' - '.$date.' Right Now';
the feedback that the user sees after filling up is
echo 'Thanks for filling up the form We will get beck to you by next '. $date2;
Thanks for helping. I have managed to solve the problem. This is what I had done in the php page.
echo '<h2>Thanks for filling up the form. This is the confirmation of what you had filled up:<br/><br/>' .'<br/>'. 'Name: '.$name.'<br/><br/>'.' Email: '.$email_from.'<br/><br/>'.'Tel: '.$telephone.'<br/><br/>'.' Courses: '.$basic.'<br/><br/>'.$advanced.'<br/><br/>'.$raw.'<br/><br/>'.$lightroom.'<br/><br/>'.' Comments: '.$comments.'<h2>';
It's working fine now!

Redirecting error messages

How do I redirect the error messages from my html_form_send.php back to my email.php page. Here is what I got, I have my form email.php:
email.php
<form name = 'htmlform' action = '$template/html_form_send.php'
method = 'post' class = 'form-horizontal well' >
My email.php links to my html_form_send.php which has the following code:
html_form_send.php
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "my email";
$email_subject = "my subject";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found \
with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['name']) || !isset($_POST['email']) ||
!isset($_POST['telephone']) || !isset($_POST['password'])) {
died('We are sorry, but there appears to be a problem \
with the form you submitted.');
// and instead redirect the user to your error page
header("Location: http://hurstblog.co.uk/contact-error");
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$password = $_POST['password']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not \
appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not \
appear to be valid.<br />';
}
if (strlen($telephone) < 2) {
$error_message .= 'The telephone you entered do not \
appear to be valid.<br />';
}
if (strlen($password) < 2) {
$error_message .= 'The password you entered do not \
appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$email_message .= "Telephone: " . clean_string($telephone) . "\n";
$email_message .= "Password: " . clean_string($password) . "\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n".
'Reply-To: ' . $email_from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
if (mail($email_to, $email_subject, $email_message, $headers)) {
header("Location: http://domain.net");
}
}
die();
?>
My question is how can I redirect the error?
html_form_send.php
// validation expected data exists
if(!isset($_POST['name'])){
header("location: email.php?error=name");
}
if(!isset($_POST['email'])){
header("location: email.php?error=email");
}
if(!isset($_POST['telephone'])){
header("location: email.php?error=telephone");
}
if(!isset($_POST['password'])){
header("location: email.php?error=password");
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$password = $_POST['password']; // required
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
header("location: email.php?error=invalid_email");
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
header("location: email.php?error=invalid_firstname");
}
if(strlen($telephone) < 2) {
header("location: email.php?error=invalid_telephone");
}
if(strlen($password) < 2) {
header("location: email.php?error=invalid_password");
}
The $_GET['error'] will contain the error ID, so in your email.php, you should put the error messages.
$error_message = "";
if($_GET['error']=='invalid_email'){
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if($_GET['error']=='invalid_firstname'){
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
//etc
if(strlen($error_message) > 0) {
echo($error_message);
}
What I always do is get the error code or assign the text for the error on the validating page to a session variable, and then just echo out that session variable on the page where you want the user to see the variable. Don't forget to include session_start(); at the beginning of each page though!

syntax error, unexpected 'Â Â Â Â ' (T_STRING)

Why does my PHP mailing code always give:
syntax error, unexpected 'Â Â Â Â ' (T_STRING) in C:\xampp\htdocs\GSP\members.php on line 4
<?php
if(!isset($_POST['email'])) {
     
    $email_to = 'kennydharmawan#gmail.com'; //this is line 4
    $email_subject = "GSP Rent Order";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['unit']) ||
        !isset($_POST['startdate']) ||
!isset($_POST['enddate']) ||
        !isset($_POST['telephone']) ||
!isset($_POST['rname']) ||
!isset($_POST['city']) ||
!isset($_POST['adress'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
     
    $name = $_POST['name'];
    $email_from = $_POST['email'];
    $unit = $_POST['unit'];
    $startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
    $telephone = $_POST['telephone'];
$rname = $_POST['rname'];
$city = $_POST['city'];
$adress = $_POST['adress'];
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
$phone_exp = "/^[1-9][0-9]{0,15}$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$rname)) {
    $error_message .= 'The Recipient Name you entered does not appear to be valid.<br />';
  }
if(!preg_match($string_exp,$city)) {
    $error_message .= 'The City you entered does not appear to be valid.<br />';
  }
if(!preg_match($phone_exp,$telephone)) {
    $error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
  }
  if(strlen($adress) < 2) {
    $error_message .= 'The Adress you entered do not appear to be valid.<br />';
  }
list($dd,$mm,$yyyy) = explode('/',$startdate);
if (!checkdate($mm,$dd,$yyyy)) {
$error_message .= 'The Start Date you entered do not appear to be valid.<br />';
}
list($dd,$mm,$yyyy) = explode('/',$enddate);
if (!checkdate($mm,$dd,$yyyy)) {
$error_message .= 'The End Date you entered do not appear to be valid.<br />';
}
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Unit: ".clean_string($unit)."\n";
    $email_message .= "Start Date: ".clean_string($startdate)."\n";
    $email_message .= "End Date: ".clean_string($enddate)."\n";
$email_message .= "Telephone Number: ".clean_string($telephone)."\n";
$email_message .= "Recipient Name: ".clean_string($rname)."\n";
$email_message .= "Recipient City: ".clean_string($city)."\n";
$email_message .= "Recipient Adress: ".clean_string($adress)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers); 
echo "Thank you for contacting us. We will be in touch with you very soon."
}
?>
Why does it always keep saying this error?
Your problem is that the PHP file has been saved with exotic white space characters -- ie not standard spaces, but some other characters that are rendered as spaces (or even not shown at all), but are unparseable by PHP.
Delete all the white space characters on lines 3 and 4, re-type them, and save the file. (that's all white space, including line feeds and spaces between words)
This should solve the problem.
If after doing this, you still get the error, but on a different line, then you will need to repeat the process for that line too.
I see some errors in your code:
Try replacing:
$email_to = 'kennydharmawan#gmail.com';
With:
$email_to = "kennydharmawan#gmail.com";
And in this line:
echo "Thank you for contacting us. We will be in touch with you very soon."
Your are missing an ";", so fixed it:
echo "Thank you for contacting us. We will be in touch with you very soon.";
PS: Try replacing all your single quotes with double quotes( '' with "") i all your strings vars ;)
Saludos.

Unknown Sender PHP (Code inside)

I've created a PHP file to work with my form on my jQuery mobile site. The form works perfectly and sends the email and errors work and all. But I keep getting the email from an (unknown sender). Subject line and email information is there. (Email coming from the form to host the host email address). Thanks for any help that can be provided.
<?php
if(isset($_POST['email'])){
// Here is the email to information
$email_to = "hostemail#email.com";
$email_subject = "Customer Service Form";
$email_from = "Company";
//error code
function died($error){
echo "We are sorry, but there were errors found with the form you submitted.";
echo "These errors appear bellow.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
//validation
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
//$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z] {2,4}$/';
//(!preg_match($email_exp, $email)) {
//$error_message .='The Email Address you entered does not appear to be valid.<br/>';
// }
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The name you entered does not seem to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The comments you entered do not appear to be valid.<br/>';
}
if(strlen($error_message) > 0 ) {
died($error_message);
}
$email_message = "Form details below. \n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string ($name) . "\n";
$email_message .= "E-Mail:" . clean_string ($email) . "\n";
$email_message .= "Message:" . clean_string ($message) . "\n";
//create email headers
$headers = 'From:' .$email_From . "\r\n" . 'Reply-To:' . $email. "\r\n" .
'X-MAILER: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success Message Goes Here -->
Thank you for contacting us. We will be in touch with you shortly. <br/>
Please Click here to go back to the contact page.
<?php
}
?>
The format for the From header is:
Display Name <email address>
For example:
Company <foo#company.com>
Right now, you're just using "Company", which is neither a valid e-mail address on its own, nor has an e-mail address at the end.

Categories