php联系表单代码无效

时间:2016-11-29 12:22:07

标签: php html email

我一直在我的网站上工作,突然发现它不会在我的网站域发送电子邮件,例如email@domain.com但它仍然会发送电子邮件至name@gmail.com

所以我不知道,尝试了很多脚本和代码并尝试交叉检查很多东西,但都失败了。此外,我无法调查为什么会发生这种情况的代码。我希望有人可以在这里检查代码,并帮助我找到错误,以便我可以解决它。

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
   $email_to = "name@domain.com,name@gmail.com"; 
    $email_subject = "Contact Form Request";
    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['subject']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
    $name = $_POST['name']; // required
    $email = $_POST['email']; // required
    $subject = $_POST['subject']; // not required
    $message = $_POST['message']; // 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 />';
  }
 /*   $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  } */

  if(strlen($message) < 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 .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";

// create email headers
//  $headers = 'From: '.$email."\r\n".
$headers = "From: username@gmail.com";
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
 echo "<h1>THANK YOU!!!</h1><br><h3>Thank you for submitting the form. We will contact you soon!</h3>";
}

?>

3 个答案:

答案 0 :(得分:0)

您是否尝试使用mail()的第5个参数强制返回路径?

看看这里: https://translate.google.de/translate?sl=de&tl=en&js=y&prev=_t&hl=de&ie=UTF-8&u=http%3A%2F%2Fwww.codemercenary.de%2F2010%2F02%2Freturn-path-bei-php-mail-erzwingen.html&edit-text=&act=url

可能是您的配置缺失的部分;)

答案 1 :(得分:0)

这可能是MX条目的问题。

  

登录您的cPanel

  1. 点击电子邮件下的MX条目图标。
  2. 从中选择域名 有你的形式的下拉列表。
  3. 选择远程电子邮件交换机
  4. 然后命中         变化

答案 2 :(得分:0)

你的脚本没问题。它托管在domain.com上。如果你做单位测试,如:

mail('name@domain.com,name@gmail.com', 'my subject', 'my message');

然后您在gmail收件箱中收到一封邮件,而域收件箱中没有邮件。

由于错误的标题可能会导致可交付性问题(它不一定在垃圾邮件文件夹中,它可以在您的托管服务商之前被阻止)。你能做的是:

  • 阅读domain.com邮箱的垃圾邮件政策并检查您的设置。
  • 使用此工具检查邮箱name@domain.com的DNS和MX条目:http://mailboxtools.com/tools/smtptest.aspx
  • 使用此工具检查您的SPF记录:http://www.kitterman.com/spf/validate.html
  • 使用these tools发送包含您脚本的电子邮件并获取结果。
  • 检查gmail收到的电子邮件的完整标题(菜单 &#34;显示原文&#34;)并尝试解释。
  • 如果您向this tool
  • 提供的临时地址发送电子邮件,请在发送脚本时检查您的垃圾邮件分数