阅读邮件附件(xls和xlsx)

时间:2017-03-22 06:20:35

标签: php

我正在阅读来自php的邮件,邮件中有xls或xlsx文件,所以一旦我下载了它就会被下载,但是当我尝试读取xls或xlsx文件时就这样了

$email_number = $emails[count($emails)-$k]; /* get information specific to this email */ $overview = imap_fetch_overview($connection,$email_number,0); $message = imap_fetchbody($connection,$email_number,2); /* get mail structure */ $structure = imap_fetchstructure($connection, $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' => '', 'typeval' => $structure->parts[$i]->type ); 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($connection, $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 */ $j=0; foreach($attachments as $attachment) { if($attachment['is_attachment'] == 1) { $filename=''; if(trim($attachment['filename'])!="") $filename=trim($attachment['filename']); elseif(trim($attachment['name'])!="") $filename=trim($attachment['name']); $ext = strtolower(substr(trim($filename), strrpos(trim($filename), '.') + 1)); //$filename = $attachment['filename']; $filename = str_replace(" ","-",strtolower($companyArray[$excelSheet])).$j.$k.'.'.$ext; if(empty($filename)) $filename = $attachment['filename']; if(empty($filename)) $filename = time() . ".dat"; $folder = "download"; if(!is_dir($folder)) { mkdir($folder); } $fp = fopen("./". $folder ."/". $filename, "w+"); $data = strtr($attachment['attachment'], array('-' => '+', '_' => '/')); fwrite($fp, $data); fclose($fp); chmod("./". $folder ."/". $filename, 0644); $j++; } }

请帮助我解决此问题。以下是我的代码

0

0 个答案:

没有答案
相关问题