我的邮件脚本和重定向似乎无法正常工作。提交页面变为空白。我一直在看着它,我的眼睛已经消失了。一套清新的眼睛会有所帮助。
<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$phone = isset($_POST['phone']) ? $_POST['phone'] : '';
$subject = isset($_POST['subject']) ? $_POST['subject'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$mail_status = sendMail($name, $email, $phone,$subject, $message);
function sendMail($name, $email, $phone, $subject, $message)
{
// Whitespace pattern, icluding different masking methods
$whitespace = '~(<CR>|<LF>|0x0A|%0A|0x0D|%0D|\\n|\\r|\s)+~i';
$name = trim(preg_replace($whitespace, '', $name));
if (empty($name)) {
return false;
}
$email = trim(preg_replace($whitespace, ' ', $email));
if (empty($email)) {
return false;
}
$mail_to = 'info@website.com';
$subject = 'New Contact for My Website from ' . $name;
$body = "From: $name\n";
$body .= "E-mail: $email\n";
$body .= "Phone: $phone\n";
$body .= "subject: $subject\n";
$body .= "Message: $message";
$headers = "From: $email\r\n";
$headers .= "Reply-To: '$email\r\n";
return mail($mail_to, $subject, $body, $headers);
header("Location: http://website.com/");
exit;
}
答案 0 :(得分:1)
永远不会调用您的标头函数,因为您之前有一个返回函数。您的返回函数将退出脚本(如果您在一个函数中,则退出函数)。
将return mail($mail_to, $subject, $body, $headers);
替换为:
mail($mail_to, $subject, $body, $headers);
答案 1 :(得分:0)
if(mail($mail_to, $subject, $body, $headers)):
header("Location: http://website.com/");
endif;