【问题标题】:Email doesn't send based on variable name电子邮件不根据变量名称发送
【发布时间】:2012-04-19 09:35:58
【问题描述】:

只有当我输入实际地址而不是使用 $usr_email 时,我的电子邮件才会发送,尽管它显示“消息已发送”。电子邮件地址来自用户表中的 user_email 字段。 这是用 $id = intval($_SESSION['user_id']);

if (isset($_POST['doSend'])) {


function getTwo($query){
    $res = mysql_query($query);
    if (!$res) {
        trigger_error("db: ".mysql_error()." in ".$query);
        return FALSE;
    }
    if ($row = mysql_fetch_row($res)) {
        return $row[0];
    }
}

$getuserinfo_q = "SELECT user_email AND user_name FROM users WHERE 
id='$_SESSION[user_id]'";

$getuserinfo_e=mysql_query($getuserinfo_q);

if(mysql_num_rows($getuserinfo_e) < 1){

    echo "User details not found - User_id is not in DB";

    exit();

}

$user_info_val=mysql_fetch_assoc($getuserinfo_e);

if(empty($user_info_val['user_email'])){

    echo "there is no such column name as 'user_email'"; //tell the user about column

    exit(); //shut off the script

}


$usr_email=$user_info_val['user_email'];
  $user_name=$user_info_val['user_name'];



$sqltest = "SELECT completed_status From users where id =
'$_SESSION[user_id]'";
$isSending = getTwo($sqltest);
$isSending === false;
if($isSending >= 6){
    require_once "Mail.php";
    require_once "Mail.php";



    $from = "<xxx>";
    $to = "$usr_email";
    $subject = "hi";
$body ="Chi ";

$host = "ssl://smtp.gmail.com";
$port = "465";
    $username = "xxx";
    $password = "xxxx";

 $headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
        echo("<p>" . $mail->getMessage() . "</p>");
    } else {
        echo("<p>Message successfully sent!</p>");
    }
}
else
    header ("Location: error.php");



}

【问题讨论】:

  • 你知道这是你可以用mysql做的最糟糕的事情吗?从 user_email='$usr_email' OR user_name='$user_name' 的用户中选择 count(*) 作为总数。小提示:sql注入
  • 你试过回显$data['usr_email'];吗?
  • 而且你也容易受到 SQL 注入的攻击。
  • 它只是在尝试回显时给出未定义的索引 usr_email。
  • 当我更改时仍然是相同的消息

标签: php


【解决方案1】:

大编辑:

好的,希望这行得通-_-

$getuserinfo_q = "SELECT user_email AND user_name FROM users WHERE id ='
".intval($_SESSION['user_id'])."'";

$getuserinfo_e=mysql_query($getuserinfo_q);

if(mysql_numb_rows($getuserinfo_e) < 1){

echo "User details not found - User_id is not in DB";

exit();

}

$user_info_val=mysql_fetch_assoc($getuserinfo_e);

if(empty($user_info_val['user_email'])){

echo "there is no such column name as 'user_email'"; //tell the user about column

exit(); //shut off the script

}


$usr_email=$user_info_val['user_email'];


$sqltest = "SELECT completed_status FROM users WHERE id ='
".intval($_SESSION['user_id'])."'";
$isSending = getTwo($sqltest);
$isSending === false;
if($isSending >= 6){
    require_once "Mail.php";

// Start NEW CLASS (for your weird Mail function)

$m_class=new Mail;


    $from = "<xxx>";
    $to = "$usr_email";
    $subject = "hi";
$body ="Chi ";

$host = "ssl://smtp.gmail.com";
$port = "465";
    $username = "xxx";
    $password = "xxxx";

 $headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);


$smtp = $m_class->factory('smtp',
array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $m_class->send($to, $headers, $body);

}
else
    header ("Location: error.php");

您可能已经注意到,我删除了错误检查(PEER::isError() 事物)只是因为我无法理解应该如何调用该函数(不是静态的)。但是,除此之外,上面的代码应该可以解决问题(希望如此)。

试一试,告诉我它是如何工作的。

【讨论】:

  • 我拥有的唯一代码就是我发布的代码。那里没有其他$_POST 数据,所以也许我可以删除它?
  • 好吧,那是你的问题。如果你不把它放在那里,你怎么期望$_POST 有任何与“usr_email”相关的数据?
  • 是的,我知道,但是当只使用 $usr_email = 'usr_email'; 时,永远不会收到电子邮件
  • 在您的 foreach 循环中,您有 $key - 这应该是数组的键吗?而$value 应该是该数组键的值?
  • 那是因为您再次定义 $usr_email (在您的 foreach 循环中)。注释/删除此行:$usr_email = $data['usr_email']; 并将 $usr_email 设置为您自己的电子邮件,您应该会收到它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多