2012-01-06 82 views
0

嗨,我已經編輯了一些我已經在網上找到的代碼,除了驗證之外,一切工作。即使當我創建的聯繫人表單中的所有字段都爲空時,它總是會發送電子郵件。這絕對是一個簡單的解決方案,但我是新來的,所以任何幫助對我來說意味着很多!PHP電子郵件腳本問題

謝謝。

繼承人使用PHP腳本代碼即時通訊:

//////////////////////////////// /////////////////////

<?php 
// Contact subject 
$subject ="$Email Enquiry"; 
// Details 
$message=("$cComment") ; 

// Mail of sender 
$mail_from="$cEmail"; 
// From 
$header="from: $cName <$mail_from>"; 

// Enter your email address 
$to ='[email protected]'; 

$send_contact=mail($to,$subject,$message, $header); 

// Check, if message sent to your email 
// display message "We've recived your information" 
if($mail_from != "Email"){ 
header('Location:thanksemail.php'); 
} 
else { 
header('Location:emailfail.php'); 
} 
?> 

回答

0

您還沒有任何檢查代碼。發送郵件的代碼是$ send_contact = mail($ to,$ subject,$ message,$ header); 的檢查必須是此代碼

前試試這個

if($subject!="" && $cComment!="" && $cEmail!="" && $cName!="" && $mail_from!=""){ 
$send_contact=mail($to,$subject,$message, $header); 

if($mail_from != "Email"){ 
header('Location:thanksemail.php'); 
} 
else { 
header('Location:emailfail.php'); 
} 


}else{ 
header('Location:emailfail.php'); 
} 
3

您需要在發送電子郵件之前檢查變量是否爲空。你可以這樣做,像這樣:

if(!empty($variable) and 
    !empty($variable2)) { 
    // send the email out here 
} 

我用empty()功能這裏來檢測,如果值是空。以下值被認爲是空值:

"" (an empty string) 
0 (0 as an integer) 
0.0 (0 as a float) 
"0" (0 as a string) 
NULL 
FALSE 
array() (an empty array) 
var $var; (a variable declared, but without a value in a class) 

我還要避免使用標準的PHP郵件功能,而使用一個庫如PHPMailerSwiftMailer來發送你的電子郵件來代替。他們給你更多的靈活性,並且都有更好的API來處理。

if(mail($to,$subject,$message, $header)){ 
    header('Location:thanksemail.php'); 
}else { 
    header('Location:emailfail.php'); 
} 

如果有人送它回來真:

+0

需要確保你不把郵件($到,$主題,$消息,$頭)稱,如果$等等等於「」/ null – BeRecursive 2012-01-06 13:06:15

+1

@BeRecursive我不知道你在得到什麼。請參閱'empty()'函數的手冊頁面http://php.net/empty以獲得更多關於計數爲空值的信息。 – Treffynnon 2012-01-06 13:09:04

+0

對不起,這很不明確 - 我只是想強調,mail()函數調用實際上是發送電子郵件,這就是你需要在你的井裏解釋if語句 – BeRecursive 2012-01-06 13:12:18

0

正如一個側面說明,您可以通過驗證,如果電子郵件被髮送一樣保存自己的代碼或2號線。如果爲false,則不會發送。

0

你已經把郵件()函數if()語句之外。

只需將它(並完成,如果與其他字段):

<?php 
if(!empty($mail_from) && !empty($Email)) { 
    mail($to, $subject, $message, $header); 
    header('Location:thanksemail.php'); 
} else { 
header('Location:emailfail.php'); 
}