2012-01-06 137 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'); 
}