0

im having issues with my script not sending an email, the return error message is: Email address not valid! This happens when ever I enter my email address into the text field. I have a feeling that it is the (preg_match) method that is creating the issue, but after looking online I dont really understand the content of the method. Hope you guys can help, thanks.

SOURCE CODE:

  <?php
  /*Select email recipient*/
  $myemail = "info@shadowempires.url.ph";

  /*Check all form inputs using check input function*/
  $name = check_input($_POST['name'], "Please enter your name");
  $email = check_input($_POST['email'], "Please enter your email address.");
  $comment = check_input($_POST['comment'], "Please write a message.");

  /*If email is not valid show error message*/
  if (!preg_match("/(\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){
      show_error("Email address not valid!");
  }

  /*Lets prepare the message for the email*/
  $message = "Customer Question!

  Contact form has been submitted by:

  Name: $name
  Email: $email
  Comments: $comment

  End of message";

  /*Send the message using mail() function*/
  mail($myemail, $message);

  /*Redirect visitor to the thank you page*/
  header('Location: thankyou.htm');
  exit();

  /*Functions we used*/
  function check_input($data, $problem=''){
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      if ($problem && strlen($data) == 0){
          show_error($problem);
      }
      return $data;
  }
  function show_error($myError){
  ?>
      <html>
      <body>
      <b>Please correct the following error:</b><br />
      <?php echo $myError; ?>
      </body>
      </html>
  <?php exit();
  }
  ?>
user4163554
  • 51
  • 10

2 Answers2

1

What about filter_var:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // valid email address
}

This is an easy way to validate an email address.

UPDATE 1

Take a look to this answer. Here there is more information about using regex for validate email address: How to validate an email address in PHP

UPDATE 2

There is a tool for test regex email patterns, look here

Community
  • 1
  • 1
rogelio
  • 1,541
  • 15
  • 19
0

You need to fix your regexp, here is example of working one:

/((\w|\-))+\@((\w|\-))+\.((\w|\-))+/
Esse
  • 3,278
  • 2
  • 21
  • 25
  • Thanks, this has got me a little further, I am now diverted to thankyou.htm but no email has been sent? The email account exists and is working! – user4163554 Oct 24 '14 at 22:46
  • @user4163554 See [this question/answer](http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail) for troubleshooting that. – John Conde Oct 24 '14 at 22:53
  • Very good link, and thanks for the regex thingy, all is working smoothly now. Thanks. – user4163554 Oct 24 '14 at 23:19