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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: