【问题标题】:Not receiving email from PHP mail() contact form未收到来自 PHP mail() 联系表的电子邮件
【发布时间】:2012-06-07 18:34:06
【问题描述】:

我知道这个问题已被问过无数次,但我一直找不到特定于我的答案。

我的联系表格似乎验证得很好,当输入合适的数据并单击“发送消息”按钮时,会显示一条消息,说明消息实际上已发送。屏幕上没有错误消息。

我的 html 代码如下所示:

<form action="send.php" id="contact_form" method="post" name="contact_form">
  <ul>
    <li>
      <label>NAME <span class="required">*</span></label>
      <input type="text" name="name" id="name" value="" class="requiredField" />
    </li>
    <li>
      <label>EMAIL <span class="required">*</span></label>
      <input type="text" name="email" id="email" value="" class="requiredField email" />
    </li>
    <li class="textarea">
      <label>MESSAGE <span class="required">*</span></label>
      <textarea name="message" id="message" rows="20" cols="30" class="requiredField"></textarea>
    </li>
    <li class="button_form">
      <input name="submitted" id="submitted" value="Send Message" class="submit" type="submit" />
      <input id="reset" type="reset" value="Reset Form" class="submit" />
    </li>
  </ul>
</form>
<script type="text/javascript">
  //<![CDATA[
  $(document).ready(function(){
    $("#form").validate();
  });
  //]]>

我的 JS 看起来像这样:

$(document).ready(function() {
  $('form#contact_form').submit(function() {
    $('form#contact_form .error').remove();
      var hasError = false;
      $('.requiredField').each(function() {
        if(jQuery.trim($(this).val()) == '') {
          var labelText = $(this).prev('label').text();
          $(this).parent().append('<span class="error">Please enter your '+labelText+'</span>');
          $(this).addClass('inputError');
          hasError = true;
        }
        else if($(this).hasClass('email')) {
          var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
          if(!emailReg.test(jQuery.trim($(this).val()))) {
            var labelText = $(this).prev('label').text();
    $(this).parent().append('<span class="error">Please a valid '+labelText+'</span>');
            $(this).addClass('inputError');
            hasError = true;
          }
        }
      });

      if(!hasError) {
        $('form#contact_form input.submit').fadeOut('normal', function() {
          $(this).parent().append('');
        });
        var formInput = $(this).serialize();
        $.post($(this).attr('action'),formInput, function(data){
        $('form#contact_form').slideUp("fast", function() {
      $(this).before('<p class="success">Thank you!<br/>Your email was sent successfully.<br/>I will contact you as soon as possible.</p>');
        });
      });
    }

    return false;
  });
});

我的 PHP 看起来像这样:

<?php 
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP

if(!isset($hasError)) {

  $emailTo = 'name@domain.com';
  $subject = 'Submitted message from '.$name;
  $sendCopy = trim($_POST['sendCopy']);
  $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
  $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

  mail($emailTo, $subject, $body, $headers);


  $emailSent = true;
}
}
?>

我在代码中遗漏了什么吗?任何帮助将不胜感激!

【问题讨论】:

  • 仅供参考,禁用E_NOTICE坏事,因为大多数通知意味着您的代码是坏的或错误的。
  • 您要发送到 hotmail 地址吗? Hotmail(和其他几个邮件服务提供商)似乎阻止了来自 mail() 的邮件。您可能想使用邮件程序类。
  • 除了ThiefMaster和Sietse所说的:检查mail()的返回值,如果可能的话检查你的邮件服务器日志(如果使用sendmail)。
  • 谢谢大家,(ThiefMaster 我会记住这一点)...是的 Sietse 我使用的是 hotmail 地址,但是当它不起作用时,我尝试使用 googlemail,然后使用 @me.com 地址没有任何运气。最后决定尝试使用我的网络主机通过我的网站域提供的电子邮件地址。这也没有成功。
  • 您只是假设邮件队列接受了消息,而不是检查 mail() 的返回值。虽然 mail() 返回 true 并不意味着邮件已发送(它实际上仅意味着邮件传输子系统接受了有效的消息),如果它返回 false 则意味着它肯定没有发送消息。请更新您的代码以检查 mail() 的返回值。如果它是假的,那么你的脚本可能有问题,如果它是真的,那么问题可能出在 PHP 之外。

标签: php forms email cpanel contact


【解决方案1】:

您的服务器中使用的电子邮件系统是什么。你有 sendmail、SMTP 等吗?我的意思是你是否检查了 php.ini 文件中的邮件端口(例如:smtp_port)和 smtp ip 地址(“localhost 或 192.168.132.1 等”)等。

如果这些设置有问题,您的邮件将无法正常工作。最好是先检查 smtp,因为它比 sendmail 快。您可以为此目的使用 google smtp 设置。 检查此链接https://www.digitalocean.com/community/articles/how-to-use-google-s-smtp-server

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 1970-01-01
    • 2010-12-09
    • 2017-05-27
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2014-06-05
    相关资源
    最近更新 更多