我试图在WordPress主题中包含一个简单的联系表单我为某人编写代码(他们希望它的功能没有使用任何WP插件,所以我只是使用PHP)。PHP邮件()麻烦
下面是我使用的代码:
<?php
include "../../../../wp-blog-header.php"; // include WP to be able to use some options
if (of_get_option('ss_contact_email', 'no entry')) { // custom WP option
$mailto = of_get_option('ss_contact_email', 'no entry');
} else {
$mailto = get_option('admin_email'); // WP option to get email address of the admin
};
$cc = "";
$bcc = "";
$subject = "[Contact Form] " .$_POST['subject']. "";
$vname = ucwords($_POST['user']);
$email = $_POST['email'];
function validateEmail($email)
{
if(eregi('^[a-zA-Z0-9._-][email protected][a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
return true;
else
return false;
}
if((strlen($_POST['user']) < 1) || (strlen($email) < 1) || (strlen($_POST['question']) < 1) || validateEmail($email) == FALSE){
$emailerror .= '';
if(strlen($_POST['user']) < 1){
$emailerror .= '<span class="wrong">Please enter your name. </span>';
}
if(validateEmail($email) == FALSE || strlen($email) < 1) {
$emailerror .= '<span class="wrong">Please enter a valid e-mail address. </span>';
}
if(strlen($_POST['message']) < 1){
$emailerror .= '<span class="wrong">Please enter your message. </span>';
}
} else {
$emailerror .= "<span>Your message has been sent. Thank you!</span>";
// NOW SEND THE ENQUIRY
$timestamp = date("F j, Y, g:ia");
$messageproper ="\n\n" .
"Name: " .
ucwords($_POST['user']) .
"\n" .
"Email: " .
$email .
"\n" .
"Website: " .
$_POST['url'] .
"\n" .
"Subject: " .
$_POST['subject'] .
"\n" .
"Comments: " .
"\n" .
$_POST['message'] .
"\n" .
"\n\n" ;
$messageproper = trim(stripslashes($messageproper));
mail($mailto, $subject, $messageproper, "From: \"$vname\" <".$_POST['email'].">\nReply-To: \"".ucwords($_POST['user'])."\" <".$_POST['email'].">\nX-Mailer: PHP/" . phpversion());
}
?>
<?php echo $emailerror; ?>
错误处理似乎很动听,它返回所有正确的错误,但如果所有信息均正确无误它去,而不是发送和显示“谢谢你..空白“ 信息。
我在同一台服务器上多次使用过相同的脚本,从来没有遇到过任何问题,但是这次我偶然发现了一个问题 - 邮件没有发送,即使它没有返回错误。
我不确定发生了什么问题。任何人都可以浏览代码并寻找一些明显的错误?
你检查了服务器日志,看看'mail'命令是否失败? – cdeszaq
您可以尝试在此代码段中使用像'$ subject'和'$ message'这样的变量。有两个原因:在没有提交表单的情况下,测试会更容易,并且您可以更安全地过滤“POST”数据以保持更安全。 –