【问题标题】:PHP email issue with $Error message带有 $Error 消息的 PHP 电子邮件问题
【发布时间】:2016-02-08 20:49:54
【问题描述】:

在 Linux 服务器上,我有一个 ajax 表单,它被发布到一个 js 脚本,该脚本又发布到一个用于发送网站电子邮件联系表单的 php 例程(Styleshout-Puremedia 模板)。我知道该帖子发生在 js 和 php 页面上。我在“$Error 未定义”的 php 页面上收到 httpd 服务器错误。但是,如果我将它定义为 NULL 或 '' 它存在,因此没有通过测试“if (!$Error)”。如果我使用 mail 函数创建一个硬编码的 php 脚本,则电子邮件可以正常工作,因此 php 和 sendmail 都已正确配置。有人可以帮助解释为什么这个 php 脚本不起作用,或者如何修复“$Error”错误?

PHP 代码:

<?php

// Replace this with your own email address
$siteOwnersEmail = 'info@mydomain.com';


if($_POST) {
	
   $fname = trim(stripslashes($_POST['contactFname']));
   $lname = trim(stripslashes($_POST['contactLname']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check First Name
	if (strlen($fname) < 2) {
		$error['fname'] = "Please enter your first name.";
	}
	// Check Last Name
	if (strlen($lname) < 2) {
		$error['lname'] = "Please enter your last name.";
	}
	// Check Email
	if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
		$error['email'] = "Please enter a valid email address.";
	}
	// Check Message
	if (strlen($contact_message) < 15) {
		$error['message'] = "Please enter your message. It should have at least 15 characters.";
	}
   // Subject
	if ($subject == '') { $subject = "Contact Form Submission"; }

	// Set Name
	$name = $fname . " " . $lname;

   // Set Message
   $message = "Email from: " . $name . "<br />";
   $message .= "Email address: " . $email . "<br />";
   $message .= "Message: <br />";
   $message .= $contact_message;
   $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

   // Set From: header
   $from =  $name . " <" . $email . ">";

   // Email Headers
	$headers = "From: " . $from . "\r\n";
	$headers .= "Reply-To: ". $email . "\r\n";
 	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


   if (!$error) {

      ini_set("sendmail_from", $siteOwnersEmail); // for windows server
      $mail = mail($siteOwnersEmail, $subject, $message, $headers);

		if ($mail) { echo "OK"; }
      else { echo "Something went wrong. Please try again."; }
		
	} # end if - no validation error

	else {

		$response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
		$response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
		$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
		$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
		
		echo $response;

	} # end if - there was a validation error

}

?>

感谢您的帮助!!

-罗伯

【问题讨论】:

标签: javascript php ajax email


【解决方案1】:

你总是可以将 $error 初始化为

$error = [];

然后通过

检查错误
if(count($errors) === 0){
    //no errors
}

或者,只需使用if(!isset($error)){//no errors}

【讨论】:

  • 我试过这个。它确实停止了 $Error 消息,感谢 Neilsimp1!不幸的是,邮件仍然无法正常工作。我看不到任何错误,但也没有结果。我该怎么做才能进一步排除故障?
  • [可能是这样的](stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function)?除此之外,我不知道,它可能与您的环境有关。
猜你喜欢
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多