douchou8935 2014-03-26 14:12
浏览 19

为什么mime在标题中打印而无法调用变量?

Let say I have a class, for example:

class User{
     function callname(){
         $user = $_SESSION['id'];
         $query = ("SELECT * FROM user WHERE user.id='$user'");
         while ($result=mysql_fetch_array($query)){
              echo ($result['username']);}}}

And then, make instance for User object:

$user = new User;

Code to send mail:

if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = trim($_POST['username']);
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));

if ($check==TRUE){
    $name = $user->callname();
    $to = "myemail@domain.com";
    $subject = "Example Subject";
    $headers = "From: My Domain".'
'.
      "MIME-Version: 1.0".'
'.
      "Content-Type: text/html; charset=ISO-8859-1".'
'.
      'X-Mailer: PHP/' . phpversion();
    $message = "Hai $name, this is the new message.";

            mail($to, $subject, $message, $headers);
} else {
    ?>
        <script type="text/javascript">
            alert("Sorry, username not exist !");
        </script>
        <?php
}

}

Mail function was working correctly and I have received an email too. The problem is

  1. $name didn't print the name of user in the email. I've tried this $name = $user->callname(); on different page without the if() and it was working.

  2. MIME-Version: 1.0 and so on was print in the From header.

  • 写回答

1条回答 默认 最新

  • duanchique1196 2014-03-26 14:39
    关注
    1. I think you need to return $result['username'] instead of echoing it.
    2. For special characters be read as such you need them in double quotes (").

    A attempt to correct your code:

    class User{
         function callname(){
             $user = $_SESSION['id'];
             $query = mysql_query("SELECT * FROM user WHERE user.id='$user'");
             while ($result=mysql_fetch_array($query)){
                  return $result['username'];}}}
    

    Next part:

    if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysql_real_escape_string(trim($_POST['username']));
    $check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
    
    if ($check==TRUE){
        $name = $user->callname();
        $to = "myemail@domain.com";
        $subject = "Example Subject";
        $headers = "From: My Domain"."
    ".
          "MIME-Version: 1.0"."
    ".
          "Content-Type: text/html; charset=ISO-8859-1"."
    ".
          'X-Mailer: PHP/' . phpversion();
        $message = "Hai $name, this is the new message.";
    
                mail($to, $subject, $message, $headers);
    } else {
        ?>
            <script type="text/javascript">
                alert("Sorry, username not exist !");
            </script>
            <?php
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图