联系表格电子邮件不会到达目的地

时间:2013-12-03 12:43:06

标签: php forms email hosting contact-form

我有一个设置了php联系表单的简单网站。 原始联系表格包括:

$to = "my_email@gmail.com";

$subject = "New message: $name";
$message = "$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

但是,它实际上从未发送过一个emial。我的托管服务提供商声称他们不能允许这样的事情,因为它可以完成电子邮件欺骗。精细。因此,在阅读了一个帖子(php Contact Form on website and reply-to email)之后,我将上面的部分修改为以下内容:

$to = "my_email@gmail.com";


$subject = "New message: $name";
$message = "$message";
$headers = "From: my_email@gmail.com" . "\r\n" .
    "Reply-To: $email" . "\r\n" .
    "X-Mailer: PHP/" . phpversion();

mail($to, $subject, $message, $headers);

工作得很好。

现在,我几乎只是在同一主机(即使是同一个共享主机服务器)的新域下上传了同一个站点(一些设计和文本更改,没什么大不了),但它不起作用。联系表格确认发送但电子邮件未到达。

有什么想法吗?

非常感谢您的帮助:)


更新:这是值得的,这是完整的php文件:

    <?php

// Clean up the input values
foreach($_POST as $key => $value) {
    if(ini_get('magic_quotes_gpc'))
        $_POST[$key] = stripslashes($_POST[$key]);

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
    if(!$name) {
        $errors[] = "You must enter a name.";
    } else {
        $errors[] = "Name must be at least 2 characters.";
    }
}
if(!$email) {
    $errors[] = "You must enter an email.";
} else if(!validEmail($email)) {
    $errors[] = "You must enter a valid email.";
}
if(strlen($message) < 10) {
    if(!$message) {
        $errors[] = "You must enter a message.";
    } else {
        $errors[] = "Message must be at least 10 characters.";
    }
}

if($errors) {
    // Output errors and die with a failure message
    $errortext = "";
    foreach($errors as $error) {
        $errortext .= "<li>".$error."</li>";
    }
    die("<span class='failure'><h3>Sorry, The following errors occured:</h3><ol>". $errortext ."</ol><a href='contact.html' class='more'>Refresh Form</a></span>");
}


// --------------------------------------//
// Send the email // INSERT YOUR EMAIL HERE
$to = "myemail@my_domain.com";
// --------------------------------------//


$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: $email' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


mail($to, $subject, $message, $headers);

// Die with a success message
die("<span class='success'><h3>Successfully Sent!</h3> Your message is on its way, we will respond to you shortly.</span>");

// A function that checks to see if
// an email is valid
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

?>

1 个答案:

答案 0 :(得分:0)

我认为您要从表单中的$_POST$_GET变量设置$ email,$ message等, e.g

$email = $_POST['email']; 

如果您的托管服务提供商已禁止php mail()php.ini内发送电子邮件

,则可能仍然无效

您是否查看过php pear mail函数?

可能值得简单地向您的托管服务提供商询问允许的示例。写出样本不应超过5分钟。