不会使用imap_fetchstructure函数

时间:2016-02-15 07:07:09

标签: mysql email imap

我正在尝试通过imap_fetchstructure()下载电子邮件附件。 但附件不会下载到服务器。 代码其他部分完美的工作。文本消息在我编码时插入数据库。唯一的问题是附件部分,请你帮我解决这个问题。吼叫是我的代码

#!/usr/bin/php -q
<?PHP 

//echo $output;
$servername = "localhost";
$username = "user";
$password = "pssw";
$dbname = "db_name";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* connect to email */
$hostname = '{example.org:995/pop3/ssl/novalidate-cert}'; 
$username = 'test@example.org';
$password = '123456';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to SERVER: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'NEW');

/* if emails are returned, cycle through each... */
if($emails) {

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    asort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,1);

            /* get mail structure */
    $structure = imap_fetchstructure($inbox, $email_number);

    $attachments = array();

/* if any attachments found... */
    if(isset($structure->parts) && count($structure->parts)) 
    {
        for($i = 0; $i < count($structure->parts); $i++) 
        {
            $attachments[$i] = array(
                'is_attachment' => false,
                'filename' => '',
                'name' => '',
                'attachment' => ''
            );

            if($structure->parts[$i]->ifdparameters) 
            {
                foreach($structure->parts[$i]->dparameters as $object) 
                {
                    if(strtolower($object->attribute) == 'filename') 
                    {
                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i]['filename'] = $object->value;
                    }
                }
            }

            if($structure->parts[$i]->ifparameters) 
            {
                foreach($structure->parts[$i]->parameters as $object) 
                {
                    if(strtolower($object->attribute) == 'name') 
                    {
                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i]['name'] = $object->value;
                    }
                }
            }

            if($attachments[$i]['is_attachment']) 
            {
                $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);

                /* 3 = BASE64 encoding */
                if($structure->parts[$i]->encoding == 3) 
                { 
                    $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                }
                /* 4 = QUOTED-PRINTABLE encoding */
                elseif($structure->parts[$i]->encoding == 4) 
                { 
                    $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                }
            }
        }
    }

    /* iterate through each attachment and save it */
    foreach($attachments as $attachment)
    {
        if($attachment['is_attachment'] == 1)
        {
            $filename = $attachment['name'];
            if(empty($filename)) $filename = $attachment['filename'];

            if(empty($filename)) $filename = time() . ".dat";

            /* prefix the email number to the filename in case two emails
             * have the attachment with the same file name.
             */
            $fp = fopen("./" . $email_number . "-" . $filename, "w+");
            fwrite($fp, $attachment['attachment']);
            fclose($fp);
        }
    }
//Attachement close

        $subject = $overview[0]->subject;
        $from    = $overview[0]->from;
        $received_date = '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '<div class="body">'.$message.'</div>';


    }

        //INSERT INTO DATABASE

            $sql = "INSERT INTO msg_messages (`customer_id`, `msg_title`, `msg_content`, `received`, `handle_by`, `agent_asign`,`dep_id`, `msg_received_date`, `priority_status`, `from_lyca`) VALUES ('".$cus_id."', '".$subject."', '".$remove_customer_mobile_from_message."', '".$cus_mail."', '999999', '999999', 1, '".date("Y-m-d H:i:s")."', 2, 1)";


            $result = mysqli_query($conn, $sql);



} 


/* close the connection */
imap_close($inbox);


?>  

0 个答案:

没有答案