【问题标题】:Contact form won't send messages if user specified a gmail or yahoo return address如果用户指定了 gmail 或 yahoo 的返回地址,联系表单将不会发送消息
【发布时间】:2013-12-25 02:26:31
【问题描述】:

我在网站上设置了一个联系表。我买了一个模板,并附带了联系表格。 奇怪的是,如果访问者写下 gmail 地址或 yahoo 地址作为他对地址的回复,电子邮件将无法通过。用户将看到“成功”消息,但电子邮件不会通过。

这是表格:

    <?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) < 3) {
    if(!$message) {
        $errors[] = "You must enter a message.";
    } else {
        $errors[] = "Message must be at least 3 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 = "my_email@gmail.com";
// --------------------------------------//


$subject = "Selfy Contact Form: $name";
$message = "$message";
$headers = "From: $email";

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

// Die with a success message
die("<span class='success'><h3>success</h3>  :) </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;
}

?>

我尝试完全注释掉这些验证字段,但没有成功。无法理解。

有人可以帮忙吗?

【问题讨论】:

  • 请查看邮件头注入。在这种情况下,您至少应该检查$_POST["email"] 是否包含有效的电子邮件地址,否则垃圾邮件发送者会对您的表单感到满意。为什么你在所有输入变量上都使用strip_tagshtmlspecialchars
  • 您检查了垃圾邮件文件夹中的 gmail 和 yahoo 吗?
  • 它不在垃圾箱...
  • @MarcelKorpel 这只是我使用模板获得的 php 表单...我想尽可能少地进行编辑。
  • 那么该模板不安全。不要开箱即用!

标签: php email contact email-validation contact-form


【解决方案1】:

PHP mail 函数仅适用于简单的案例消息传递。无需赘述,此功能生成的消息(通常不会)被反垃圾邮件软件过滤掉。它们甚至可以在不进入垃圾邮件箱的情况下被删除,因为以这种方式发送的消息可以在没有传出服务器身份验证的情况下生成。

您应该改为使用现有的 SMTP 帐户发送电子邮件。你需要一个 PHP 客户端,你可以在这里找到一个:PHPMailer

【讨论】:

  • PHPMailer 内部不也使用mail 函数吗?
  • 可以配置为使用SMPT。 See example.
  • 我认为这与电子邮件欺骗有关。我尝试了这里的建议:stackoverflow.com/questions/19007032/…,神奇的是它现在似乎可以工作了!感谢所有回答/评论的人:)
【解决方案2】:

您的电子邮件可能会进入垃圾邮件文件夹。我建议您在标题中添加更多信息。

在此处查看示例 #4:http://php.net/manual/en/function.mail.php

也许您还应该添加一个附加标题,其中包含您发送的电子邮件。这在“参数”下的同一页面上。

希望这会让你走上正确的道路。

【讨论】:

    【解决方案3】:

    使用 PHPMailer 并设置 SMTP 中继而不是 mail();功能 PHPMailer 连接到谷歌 smtp 中继服务器。我相信 gmail 仍然是smtp.gmail.com

    require_once('../class.phpmailer.php');
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    $mail             = new PHPMailer();
    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[\]",'',$body);
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.yourdomain.com"; // SMTP server
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
    $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
    $mail->Username   = "yourusername@gmail.com";  // GMAIL username
    $mail->Password   = "yourpassword";            // GMAIL password
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    $mail->AddReplyTo("name@yourdomain.com","First Last");
    $mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    
    if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
    echo "Message sent!";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2023-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多