doutangkao2789
doutangkao2789
2013-06-10 15:58
浏览 23
已采纳

php电子邮件脚本无法正常工作

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 = "xxxx@gmail.com";
    $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.

";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($name)."
";
    $email_message .= "Email: ".clean_string($email)."
";
    $email_message .= "Comment: ".clean_string($comment)."
";


// create email headers
$headers = 'From: '.$email_from."
".
'Reply-To: '.$email_from."
" .
'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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxun7962
    dongxun7962 2013-06-10 16:00
    已采纳

    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.

    点赞 评论

相关推荐