【问题标题】:Get attachment from mail using php使用php从邮件中获取附件
【发布时间】:2012-09-16 07:25:01
【问题描述】:

我需要代码来监控我的邮件收件箱并将带有附件的邮件转发到另一个电子邮件地址。我使用下面的代码从收到的消息中获取附件。但是我收到一个错误“注意:未定义的属性:第 60 行 C:\wamp\www\mail\c.php 中的 stdClass::$disposition”

这是我的代码

<?php
 $host = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}inbox';
 $login = 'AAAA@CCC.com';
 $password = 'XXXXXXX';
 $savedirpath = "../mail";
 $type = 'ReadAttachment';
$obj = new $type;
$obj->getdata($host,$login,$password,$savedirpath,$delete_emails=false);
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($host,$login,$password,$savedirpath,$delete_emails=false) {
    // make sure save path has trailing slash (/)
    //print_r("test");
    $savedirpath = str_replace('\\', '/', $savedirpath);
    if (substr($savedirpath, strlen($savedirpath) - 1) != '/') {
        $savedirpath .= '/';
    }

    $mbox = imap_open ($host, $login, $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";
    //print_r($message);
    $emails = imap_search($mbox,'ALL');
    foreach($emails as $email_number) {
        $structure = imap_fetchstructure($mbox, $email_number , FT_UID);    
        $parts = $structure->parts;
        $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,$email_number,$fpos);  
                $filename="$filename";
                $fp=fopen($savedirpath.$filename,"w");
                $data=$this->getdecodevalue($mege,$part->type);
                //print_r($data);
                fputs($fp,$data);
                fclose($fp);
                $fpos+=1;
            }
        }

    }
    // imap_expunge deletes all tagged messages

    imap_close($mbox);
}
}

感谢作者,但我无法获得更多帮助。 我收到一个错误"Notice: Undefined property: stdClass::$disposition in C:\wamp\www\mail\c.php on line 60" 这正是指出if($part-&gt;disposition == "ATTACHMENT")这一行。
请给我一个解决方案。

我是php新手,正在努力写这个邮件功能,,,

提前致谢:-)

【问题讨论】:

  • 请贴出完整的错误信息,并指出您到目前为止所尝试的内容。
  • 你会在这里找到一个不错的 imap 示例:linuxscope.net/articles/mailAttachmentsPHP.html
  • 是的,我试过了。但即使我也遇到了错误。

标签: php email attachment


【解决方案1】:

你需要先检查$disposition是否存在:

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

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 2020-11-06
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2014-11-14
    相关资源
    最近更新 更多