【问题标题】:PHP Errors when using form validator使用表单验证器时的 PHP 错误
【发布时间】:2016-05-30 22:44:51
【问题描述】:

我在使用联系表单时遇到了 PHP 错误,该表单一直运行良好,直到客户说他们一周以来都没有收到电子邮件。作为一个朋友帮我做后端,他现在不能问,我想问你是否可以给我一个提示,我需要修复什么。

错误:

`[2016 年 2 月 17 日星期三 16:43:44] [警告] [客户端 37.24.118.150] mod_fcgid:stderr:PHP 注意:未定义变量:/var/www/vhosts/wscgmbh.de/httpdocs/php 中的标头/contact-form-handler.php 第 29 行,referer:http://www.wscgmbh.de/contact.html

[2016 年 2 月 17 日星期三 16:43:44] [警告] [客户端 37.24.118.150] mod_fcgid:stderr:PHP 警告:mail():在 /var/www/vhosts/wscgmbh 的 Additional_header 中发现了多个或格式错误的换行符.de/httpdocs/php/contact-form-handler.php 第32行,referer:http://www.wscgmbh.de/contact.html

[2016 年 2 月 17 日星期三 16:45:18] [警告] [客户端 37.24.118.150] mod_fcgid:stderr:PHP 注意:未定义变量:/var/www/vhosts/wscgmbh.de/httpdocs/php/ 中的标头第29行的contact-form-handler.php,referer:http://www.wscgmbh.de/contact.html

[2016 年 2 月 17 日星期三 16:45:18] [警告] [客户端 37.24.118.150] mod_fcgid:stderr:PHP 警告:mail():在 /var/www/vhosts/wscgmbh 的 Additional_header 中发现了多个或格式错误的换行符.de/httpdocs/php/contact-form-handler.php 第32行,referer:http://www.wscgmbh.de/contact.html

[2016 年 2 月 17 日星期三 17:02:25] [警告] [客户端 37.24.118.150] mod_fcgid:stderr:PHP 注意:未定义索引:/var/www/vhosts/wscgmbh.de/httpdocs/php/ 中的名称第 12 行的contact-form-handler.php

[2016 年 2 月 17 日星期三 17:02:25] [警告] [客户端 37.24.118.150] mod_fcgid:stderr:PHP 注意:未定义索引:/var/www/vhosts/wscgmbh.de/httpdocs/php/ 中的电子邮件第 13 行的contact-form-handler.php

[2016 年 2 月 17 日星期三 17:02:25] [警告] [客户端 37.24.118.150] mod_fcgid:stderr:PHP 注意:未定义索引:/var/www/vhosts/wscgmbh.de/httpdocs/php/ 中的消息第 14 行的contact-form-handler.php

[Thu Feb 18 09:40:15 2016] [warn] [client 213.23.122.15] mod_fcgid:stderr:PHP 注意:未定义变量:/var/www/vhosts/wscgmbh.de/httpdocs/php/ 中的标头第29行的contact-form-handler.php,referer:http://www.wscgmbh.de/contact.html

[Thu Feb 18 09:40:15 2016] [warn] [client 213.23.122.15] mod_fcgid:stderr:PHP 警告:mail():在 /var/www/vhosts/wscgmbh 的 Additional_header 中发现多个或格式错误的换行符.de/httpdocs/php/contact-form-handler.php 第32行,referer:http://www.wscgmbh.de/contact.html`


这是一直工作的表单处理程序的代码,但突然不工作了

<meta http-equiv="Content-Type" content="text/html"; charset="utf-8" />
 <?php 
$errors = '';
$myemail = 'service@wscgmbh.de';
if(empty($_POST['name'])  || 
empty($_POST['email']) || 
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]    {2,3})$/i", 
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "Sie haben eine neue Nachricht von: $name";
$email_body = "Sie haben eine neue Nachricht erhalten, hier sind die     Details: \n\nName: $name \nEmail: $email_address \nNachricht: \n$message"; 

$headers [] .= 'From: WSC-Kontaktformular' . "\r\n";
$headers [] = "Antworten Sie: {$email_address}";

mail($to,$email_subject,$email_body,implode("\r\n",$headers));
//redirect to the 'thank you' page

echo '<script> 
        alert("Danke für Ihre Nachricht. Wir werden uns bald bei  melden!");
        window.location = "http://www.wscgmbh.de"
    </script>';
 } 
 ?>

我不是专家和 PHP,所以我真的希望有人可以帮助我! 提前非常感谢您!!!

【问题讨论】:

  • 我不久前发布了一个答案,但注意到您的代码中有其他内容,在标题中使用Antworten Sie: 似乎转换为Reply (to):。我已在答案底部附近编辑了关于它的答案,您需要在 Edit... 下重新加载它

标签: php forms handler


【解决方案1】:

这里的问题是您缺少(并将标头声明为数组):
(关于Antworten Sie:,请参阅下面我的编辑)。

$headers   = array();

放在上面:

$headers [] .= 'From: WSC-Kontaktformular' . "\r\n";
$headers [] = "Antworten Sie: {$email_address}";

您还需要删除点(连接)和. "\r\n"

implode("\r\n",$headers) 会处理它。

$headers   = array();
$headers [] = 'From: WSC-Kontaktformular';
$headers [] = "Antworten Sie: {$email_address}";

根据邮件手册的示例:

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <sender@domain.com>";
$headers[] = "Bcc: JJ Chong <bcc@domain2.com>";
$headers[] = "Reply-To: Recipient Name <receiver@domain3.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $email, implode("\r\n", $headers));

另外,From: 需要的是电子邮件而不是姓名,因此邮件可能会因此而成为垃圾邮件。

  • 使用电子邮件地址。

参考:


如果您想使用点(连接)和\r\n,那么您需要使用以下内容,并根据手册中的示例:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

编辑:

我注意到Antworten Sie: 在德语中的意思似乎是“回复”(当我访问谷歌翻译时)。

如果打算将其用作From:Reply-to,则不能将其用作邮件标头的一部分,而是按照手册所述将其用作Reply-To:

Reply-To: webmaster@example.com

因此,您的代码需要读作:

$headers   = array();
$headers [] = 'From: WSC-Kontaktformular';
$headers [] = "Reply-To: {$email_address}";

这也是导致标头失败的一个因素。

更改mail($to,$email_subject,$email_body,implode("\r\n",$headers));

到:

if(mail($to,$email_subject,$email_body,implode("\r\n",$headers))) { 
      echo "Mail sent."; 
   } else { 
      echo "Error."; 
   }

如果您看到“已发送邮件”,则表明 mail() 已完成其工作。还可以查看您的垃圾邮件。

error reporting 添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

旁注:显示错误应该只在暂存阶段完成,而不是在生产阶段。


考虑使用 PHPMailer 或 Swiftmailer:

它们是 PHP 的 mail() 函数的替代品。

【讨论】:

  • 您好,感谢您的快速答复!我像你说的那样改变了它,添加了 $headers = array();上面,也将 From 更改为邮件地址。但是当我使用联系表格时,我仍然没有收到电子邮件:-(
  • @carlpoppa 不客气。将mail($to,$email_subject,$email_body,implode("\r\n",$headers)); 更改为if(mail($to,$email_subject,$email_body,implode("\r\n",$headers))) { echo "Mail sent."; } else { echo "Error."; } - 如果您看到“邮件已发送”,则mail() 已完成其工作。还可以查看您的垃圾邮件。
  • @carlpoppa 你在编辑中也看到了我关于Antworten Sie: - Reply-To: 的注释,是吗?
  • @carlpoppa 请再次仔细检查我的答案,并且非常仔细。如果您只添加了$headers = array(); 部分,则它需要读取为$headers = array(); $headers [] = 'From: WSC-Kontaktformular'; $headers [] = "Reply-To: {$email_address}";,如我在Edit 下的答案底部所述,以及其中的更多信息。没有像 $headers [] .= 那样的点。
  • 是的,我看到了,谢谢。我收到了发送的邮件,所以它可以工作,但我的收件箱中没有任何邮件。还有什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-30
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多