douzhanlie9209 2015-08-18 10:03
浏览 64
已采纳

PHP注意:未定义的属性:stdClass :: $ disposition

Question No. 1 - can anybody tell me whats wrong with the code?? I have a script getting only the pdf file....

Question No. 2 - is there a way get the username and password in another php file like this;

define("Email", "xxxxx.com");
define("Password", "******");

how can I do thAT?? please prefer to the code below.

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'xxxxx@gmail.com';
$password = '*******';
$savedirpath = "/Attachment";
$type = 'ReadAttachment';
$obj = new $type;
$obj->getdata($hostname ,$username ,$password,$savedirpath);
class ReadAttachment
{
    function getdecodevalue($message,$coding) {
        switch($coding) {
            case 0:
            case 1:
                $message = imap_8bit($message);
                break;
            case 2:
                $message = imap_binary($message);
                break;
            case 3:
            case 5:
                $message=imap_base64($message);
                break;
            case 4:
                $message = imap_qprint($message);
                break;
        }
        return $message;
    }


    function getdata($hostname ,$username ,$password,$savedirpath) {

        $savedirpath = str_replace('\\', '/', $savedirpath);
        if (substr($savedirpath, strlen($savedirpath) - 1) != '/') {
            $savedirpath .= '/';
        }
        $mbox = imap_open ($hostname , $username , $password) or die("can't connect: " . imap_last_error());
        $message = array();
        $message["attachment"]["type"][0] = "text";
        $message["attachment"]["type"][1] = "multipart";
        $message["attachment"]["type"][2] = "message";
        $message["attachment"]["type"][3] = "application";
        $message["attachment"]["type"][4] = "audio";
        $message["attachment"]["type"][5] = "image";
        $message["attachment"]["type"][6] = "video";
        $message["attachment"]["type"][7] = "other";

                $MC = imap_check($mbox);
                $result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
                $jk = 1;
                foreach ($result as $overview) {

                    $subj = $overview->subject;
                    $date= strtotime($overview->date);
                    $unixDate = date("YmdHis", $date);
                    $email= $overview->from;
                        $opening = strpos($email, '<')+1;         //   removed username of a sender
                        $closing = strpos($email, '>');           //   removed username of a sender
                    $from = substr($email, $opening, $closing-$opening);




                        $structure = imap_fetchstructure($mbox, $jk);                                 //, FT_UID - for pop3   
                        $parts = (isset($structure->parts) ? $structure->parts : false);
                        $fpos=2;

                        for($i = 1; $i < count($parts); $i++) {

                            $message["pid"][$i] = ($i);
                            $part = $parts[$i];

                            if($part->disposition == "ATTACHMENT") {    

                                $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                                $message["subtype"][$i] = strtolower($part->subtype);
                                $ext=$part->subtype;
                                $params = $part->dparameters;
                                $filename=$part->dparameters[0]->value;
                                $mege="";
                                $data="";
                                $mege = imap_fetchbody($mbox,$jk,$fpos);  
                                $filename="$filename";
                                $extension=pathinfo($filename);

                                $file_extension = $extension['extension'];
                                if($file_extension === "pdf")
                                {
                                    $new_filename = $subject . "*" . $from . "*" . "$unixDate" . ".pdf";
                                    $fp=fopen($savedirpath.$new_filename,'w');
                                    $data=$this->getdecodevalue($mege,$part->type); 
                                    fputs($fp,$data);
                                    fclose($fp);
                                    $fpos+=1;                                   
                                }
                            }                   
                        } imap_delete($mbox,$jk);
                    $jk++;              

                }       

            imap_expunge($mbox);
            imap_errors();
            imap_close($mbox);


}
}

?>
  • 写回答

1条回答 默认 最新

  • dsavz66262 2015-08-18 10:40
    关注

    Look at this line:

    if ($part->disposition == "ATTACHMENT") {
    

    Based on the details of the PHP Notice you provided, $part is an instance of stdClass. However, also based on your PHP Notice message it does not have a disposition property.

    Change this line to :

    if (isset($part->disposition) && $part->disposition == "ATTACHMENT") {
    

    To ensure the property exists before calling it. Alternatively, you can use property_exists(); e.g:

    property_exists($part, 'disposition');
    

    Additionally, you might want to consider adding a condition somewhere to ensure that $part is actually an object with is_object():

    is_object($part); // `true` if `$part` is any object
    

    Or, a specific instance type with instanceof:

    $part instanceof stdClass; // `true` if `$part` is a `stdClass` instance
    

    Regarding your second question: if you have two separate questions, you should create post two separate questions. But, if you define any values in a separate .php file and want to include them in your current script, you can simply use require_once to effectively import them; e.g:

    <?php
    
    require_once __DIR__ . '/constants.php';
    echo FOO;
    

    Hope this helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧