电子邮件不会根据变量名称发送

时间:2012-04-19 09:35:58

标签: php

我的电子邮件只会在我输入实际地址而不是使用$ usr_email时发送,尽管它会显示“已发送消息”。电子邮件地址来自users表中的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");



}

1 个答案:

答案 0 :(得分:1)

BIG EDIT:

好的,所以,希望这可行 - -

$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()事物),因为我无法理解应该如何调用该函数(非静态)。但是,除此之外,上面的代码应该成功(希望如此)。

试一试,让我知道它是如何运作的。