我有一个邮件提交脚本,它在开发环境中完全按预期工作,只有一个例外。我从mail()测试中获得了与实时环境和开发环境相关的冲突结果。
在我的开发环境中,它成功重定向到$ ThanksURL。在实时服务器上,即使邮件成功,脚本也会继续执行else语句并重定向回表单页面。
这让我疯狂,所以任何关于为什么会受到欢迎的想法。
问题的片段:
$ok = @mail($to, $subject, $message, $headers, $returnpath);
if($ok){ header("Location: $ThanksURL");
exit;
}else{ $_SESSION['error'] .= " There has been a problems submitting your details. <br />";
header("Location: $form");
exit;
}// end if ok
完整的脚本:
<?php
session_start();
$form = 'index.php';
$_SESSION['error'] = "The following errors have occured: ";
if(isset($_POST['submitted'])) {
// email fields: to, from, subject, and so on
// Here
$from = "Form Feedback <******@gmail.com>";
$to = "******@gmail.com";
$subject = "Competition";
$ThanksURL = "thankyou.html";
$headers = "From: $from";
$returnpath = "-f" . $from;
$attachment = 0; // is there an attachement
//form fields
$emailAddress = stripslashes($_POST['email']);
$phone = stripslashes($_POST['phone']);
$comments = stripslashes($_POST['comments']);
//basic message info
$message = "Info submitted:\n\nEmail address: " .$emailAddress ."\n\nPhone number: " .$phone."\n\nComments:\n ".$comments. "\n\n";
if($_FILES['attachment']['error'] == 4) {
$message .="No attachement included";
}else{
// test file type and size for submission
$allowedExts = array("doc", "docx", "pdf", "txt");
$extension = end(explode(".", $_FILES['attachment']["name"]));
if ((($_FILES['attachment']["type"] == "application/msword")
|| ($_FILES['attachment']["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")//docx mime
|| ($_FILES['attachment']["type"] == "application/pdf")
|| ($_FILES['attachment']["type"] == "application/plain")
|| ($_FILES['attachment']["type"] == "text/plain"))
&& ($_FILES['attachment']["size"] < 2097152 )
&& in_array($extension, $allowedExts)){
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// prepare the files uplaod
$message .= "--{$mime_boundary}\n";
$fp = @fopen($_FILES['attachment']['tmp_name'],"rb");
$data = @fread($fp,filesize($_FILES['attachment']['tmp_name']));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$_FILES['attachment']['name']."\"\n"."Content-Description: ".$_FILES['attachment']['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$_FILES['attachment']['name']."\";size=".$_FILES['attachment']['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--";
}else{
$_SESSION['error'] .= "Error: " . $_FILES["attachment"]["name"] . " is not a doc, docx, pdf or txt file or is larger than 2mb. Please resubmit <br />";
header("Location: $form");
exit;
}
}//file conditional
//prepare mail
$ok = @mail($to, $subject, $message, $headers, $returnpath);
if($ok){
header("Location: $ThanksURL");
exit;
}else{
$_SESSION['error'] .= " There has been a problems submitting your details. <br />";
header("Location: $form");
exit;
}// end if ok
}// end sub
?>
答案 0 :(得分:1)
很抱歉这样说,但是php邮件可以在成功时返回其他内容而不是true或false,如php manual comment中所述。此外,return false on success on some situations也知道它。
PHP mail()是一个来自许多不同角度的kludgy函数,我个人建议不要在生产系统中使用它。有很多替代方案,例如swift mailer,PHP mailer,Zend_Mail等。
但是,如果你想使用它,你一定要记录动作,在这种情况下,返回值,没有@来隐藏你的返回值。
总结一下,在这种情况下,mail()返回一个空字符串,而不是true或false。