制作条件语句以发送电子邮件警报

时间:2012-04-28 04:51:25

标签: php mysql email conditional

我正在尝试创建一个条件语句来在调用fail.php时停止电子邮件警报。现在我收到了关于良好和失败结果的电子邮件提醒。

如果结果失败,我不想收到电子邮件。我应该制作两个脚本还是有办法让它一起工作?

由于

以下是我所指的部分以及整个脚本。

if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);  
$reg =          $_REQUEST['reg'] ; 
$first_name =   $_REQUEST['first_name']; 
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
header("location: reg_add_fail.php"); 
exit(); // as sugested by John Conde
}

<?

$to = 'newreg@41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>

<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
 <tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";

// Make sure to escape quotes

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";

mail($to, $subject, $msg, $headers);

date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ; 

$order = "INSERT INTO reg_add (submit_date, 
connect_date, 
reg, 
first_name, 
)

VALUES

('$submit_date',
'$_POST[connect_date]', 
'{$_POST[reg]}nv', 
'$_POST[first_name]')";

$result = mysql_query($order);

if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);  
$reg =          $_REQUEST['reg'] ; 
$first_name =   $_REQUEST['first_name']; 
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
header("location: reg_add_fail.php"); 
exit(); // as sugested by John Conde
}
?>

4 个答案:

答案 0 :(得分:2)

删除mail($to, $subject, $msg, $headers);的第一个实例。

然后,为了更好地衡量,请检查受影响的行数,而不是真/假(尽管两者都应该有效)。

if (mysql_affected_rows($result) > 0) {

}

答案 1 :(得分:0)

如果您检查代码

// Make sure to escape quotes

$headers  = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Homeless' . "\r\n";

mail($to, $subject, $msg, $headers);

date_default_timezone_set('America/Los_Angeles');

此代码已经发送了邮件,而不是结果。

您只需要从顶部代码

中删除此行
mail($to, $subject, $msg, $headers);

您的代码将正常运行。

答案 2 :(得分:0)

最终代码,亲切的测试迭代

<?

$to = 'newreg@41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>

<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
 <tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";

// Make sure to escape quotes

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";

date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ; 

$order = "INSERT INTO reg_add (submit_date, 
connect_date, 
reg, 
first_name, 
)

VALUES

('$submit_date',
'$_POST[connect_date]', 
'{$_POST[reg]}nv', 
'$_POST[first_name]')";

$result = mysql_query($order);

if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);  
$reg =          $_REQUEST['reg'] ; 
$first_name =   $_REQUEST['first_name']; 
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name)); 
} 
else { 
header("location: reg_add_fail.php"); 
exit(); // as sugested by John Conde
}
?>

答案 3 :(得分:0)

我目前在最后看到一些sql注入加上无效查询first_name,额外,,在posts数组键中使用的常量,请求和帖子的混合,大的html代码块以及没有检查传递的值的有效性。

如果检查有效值,则可以确定脚本是否应继续邮件并更新数据库部分:

继续清理代码希望它有所帮助:

<?php 

$to = 'newreg@41q.org';
$subject = 'New Homeless Connection';

if($_SERVER['REQUEST_METHOD']=='POST'){

    if(isset($_POST['first_name']) && strlen($_POST['first_name'])>1){
        $first_name=$_POST['first_name'];
    }

    if(isset($_POST['reg']) && strlen($_POST['reg'])>1){
        $reg=$_POST['reg'];
    }

    if(isset($_POST['connect_date']) && strlen($_POST['connect_date'])>1){
        $connect_date=$_POST['connect_date'];
    }

    if(!isset($first_name) || !isset($reg) || !isset($connect_date)){
        header("location: reg_add_fail.php");
        exit();
    }
}else{
//the page the post from
header("location: reg_form.php");
exit();
}

$msg=<<<EMAIL
<html>
<head>
<title>New Homeless Connection</title>
</head>

<body>
<table cellspacing="0" cellpadding="10" border="1" align="left">
<tr>
<td align="left" width="150px">Registery No.:</td>
<td align="left">$reg</td>
</tr>
<tr>
<td align="left">First Name:</td>
<td align="left">$first_name </td>
</tr>
<tr>
<td align="left">Connection Date:</td>
<td align="left">$connect_date</td>
</tr>
 <tr>
<td align="left" colspan="2">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
EMAIL;

// Make sure to escape quotes
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";

mail($to, $subject, $msg, $headers);

date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ;

$order = "INSERT INTO reg_add (submit_date,connect_date, reg, first_name)
          VALUES ('{$submit_date}',".mysql_real_escape_string($connect_date)."','".mysql_real_escape_string($reg)."nv','".mysql_real_escape_string($first_name)."')";

$result = mysql_query($order);

header("Location: ./reg_add_success.php?reg=".urlencode($reg)."&first_name=".urlencode($first_name));
die;
?>