关于PHP电子邮件脚本必须有数千个问题,但我仍然没有找到答案。我查看了php网站,但它也没有真正帮助。
基本上我的脚本会验证输入并相应地显示错误消息,但是当我添加脚本的第二部分来处理发送电子邮件时,页面显示500错误。这是我的代码。目前该电子邮件尚未设置为正确的电子邮件,但我知道这不是问题所在。
<?php include('includes/header.php');
$yourEmail = "hello@example.com";
$formSubject = "Message recieved from enquiry form";
if( isset( $_POST["submit"])) {
//Validate the data
function validateFormData( $formData ) {
$formData = trim( stripslashes(htmlspecialchars( $formData )));
return $formData;
}
//check to see if inputs are empty
// create variables with form data
// wrap the data with our function
if( !$_POST['name']) {
$nameError = "Please enter your name <br />";
} else {
$name = validateFormData( $_POST["name"]);
}
if( !$_POST['email'] || !preg_match("/^\S+@\S+$/", $email)) {
$emailError = "Please enter a valid email address <br />";
} else {
$email = validateFormData( $_POST["email"]);
}
if( !$_POST['enquiry']) {
$enquiryError = "Please enter your enquiry <br />";
} else {
$enquiry = validateFormData( $_POST["enquiry"]);
}
代码工作正常至此为止;
//Check to see if submit is set
//Check to make sure errors don't exist
//Send the email
// Redirect user to success.html
if ( isset( $_POST["submit"]) && (empty($emailError, $nameError, $enquiryError))) {
$to = $yourEmail;
$subject = $formSubject;
$headers = 'From:'. $name . "\r\n".
'Reply-To: '. $email . "\r\n".
'X-Mailer: PHP/' . phpversion();
$body =
'The person that contacted you was '.$name.'
Email: '.$email.'
URL: '.$url.'
Message: '.$enquiry.''.
mail($yourEmail, $formSubject, $body, $headers);
header("Location: success.html");
}
}
?>
这是HTML表单,我确信这是好的,考虑到PHP工作到某一点并正确显示错误消息。我知道我有PHP_SELF很多人都说不使用但是如果有错误我想留在页面上,只有在发送电子邮件时才重定向。
<form id="contactForm" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="name">Name <span class="asterisk">*</span></label>
<input type="text" name="name" id="name" value="<?php echo $name;?>"tabindex="1"/>
<span class="error"><?php echo $nameError; ?></span>
<label for="email">Email <span class="asterisk">*</span></label>
<input type="email" name="email" id="email" value="<?php echo $email;?>" tabindex="2"/>
<span class="error"><?php echo $emailError; ?></span>
<span class="error"><?php echo $emailValidError; ?></span>
<label for="url">Website</label>
<input type="url" name="url" id="url" value="<?php echo $url;?>" tabindex="3"/>
<h3>Tell us about your project </h3><br />
<textarea name="enquiry" id="enquiry" tabindex="4"><?php echo $enquiry;?></textarea>
<span class="error"><?php echo $enquiryError; ?></span>
<button type="submit" id="submit" name="submit" value="submit" >Submit</button>
</form>
答案 0 :(得分:0)
你的问题在这里:
empty($emailError, $nameError, $enquiryError)
empty只接受一个param,你传递它3.将条件更改为:
if( isset( $_POST["submit"]) && ( empty($emailError) && empty($nameError) && empty($enquiryError) ))
或者如果你想缩短那个条件
$Errors = $emailError.$nameError.$enquiryError;
if( isset( $_POST["submit"] ) && empty( $Errors ) )
答案 1 :(得分:0)
这就是出现错误的原因。第一个语法错误然后(空($ emailError,$ nameError,$ enquiryError))不是一个好主意。我会做这样的事情:
$err=0;
if( !$_POST['name']) {
$nameError = "Please enter your name <br />";
$err++;
} else {
$name = validateFormData( $_POST["name"]);
$err++;
}
if( !$_POST['email'] || !preg_match("/^\S+@\S+$/", $email)) {
$emailError = "Please enter a valid email address <br />";
$err++;
} else {
$email = validateFormData( $_POST["email"]);
}
if( !$_POST['enquiry']) {
$enquiryError = "Please enter your enquiry <br />";
$err++;
} else {
$enquiry = validateFormData( $_POST["enquiry"]);
}
if ( isset( $_POST["submit"]) && $err==0) {
//your code here//
//and i would do like this//
$result_mail_send = mail($yourEmail, $formSubject, $body, $headers);
if($result_mail_send==true){
//redirect to success
}else{
//redirect to fail//
}
}