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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名