我試圖在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」數據以保持更安全。 –