Javacadabra
Javacadabra

Reputation: 5758

php email script not working correctly

I have a form on my website:

 <form name="contact" action="contact.php" method="post">
      <label for="name">Name:</label><br/><input type="text" name="name" id="name"><br/>
      <label for="email">Email:</label><br/><input type="text" name="email" id="email"><br/>
      <label for="comment">Question:</label><br/><textarea name="comment" id="comment"></textarea><br/>
      <input type="submit" value="Send" id="submit">
      </form>

This is the script it submits the data to:

    <?php
header("Refresh: 3;url=http://www.xyz.com/");

if(isset($_POST['email'])) {

    $email_to = "[email protected]";
    $email_subject = "Enquiry";


    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['comment'])){
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $name = $_POST['name']; // required
    $email = $_POST['email']; // required
    $comment = $_POST['comment']; // required

    $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 .= 'The Email Address you entered does not appear to be valid.<br />';
  }

  if(strlen($comment) < 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 .= "Email: ".clean_string($email)."\n";
    $email_message .= "Comment: ".clean_string($comment)."\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 -->

Thank you for contacting us. We will be in touch with you very soon.


<?php
}
?>

For some reason the script outputs that the message was sent successfully but when I check my inbox there is no new emails. I don't know what is going on, can anyone help me?

Upvotes: 0

Views: 760

Answers (1)

Robert
Robert

Reputation: 20286

Mail function has errors turned off

 @mail($email_to, $email_subject, $email_message, $headers);  

remove @ from this and add simple if

if(mail($email_to, $email_subject, $email_message, $headers))
{
  echo 'mail was sent'; //success message here
}
else
{
  echo 'there were errors during sending mail'; //error message there
}

When you find out what errors occur then you can remove them. It can be problem with configuration of SMTP server.

You can set some settings affecting mail() function in php.ini

Read more here.

Moreover, this Thank you for contacting us. We will be in touch with you very soon. message is shown always it doesn't check if something is wrong it is just shown.

Upvotes: 3

Related Questions