Php 获取附件-imap_fetchbody()
如何获取电子邮件中的附件 代码 返回的附件甚至没有接近正确的文件大小,返回的数据如下所示Php 获取附件-imap_fetchbody(),php,imap,Php,Imap,如何获取电子邮件中的附件 代码 返回的附件甚至没有接近正确的文件大小,返回的数据如下所示 ZHUga2VuZGVyIHZlbCBpa2tlIG5vZ2VuIHNvbSBlciB2ZWQgYXQgc3RhcnRlIGVnZW4gdmly a3NvbWhlZCBlbGxlciBzb20gbGlnZSBlciBzdGFydGV0IG9wPyA6KQ0KDQpmb3JkaSBqZWcg bmV0b3AgZXIgYmxldmV0IGbmcmRpZyBtZWQgYXQgbGF2ZSBldCD4a29ub21
ZHUga2VuZGVyIHZlbCBpa2tlIG5vZ2VuIHNvbSBlciB2ZWQgYXQgc3RhcnRlIGVnZW4gdmly
a3NvbWhlZCBlbGxlciBzb20gbGlnZSBlciBzdGFydGV0IG9wPyA6KQ0KDQpmb3JkaSBqZWcg
bmV0b3AgZXIgYmxldmV0IGbmcmRpZyBtZWQgYXQgbGF2ZSBldCD4a29ub21pc3lzdGVtIG1l
ZCBlbiByZXZpc29yIHNvbSBlciB1ZHZpa2xldCBtZWQgc+ZybGlndCBoZW5ibGlrIHDlIGl2
5nJrc+Z0dGVyZSBzb20gb2Z0ZSBpa2tlIGhhciBkZW4gc3RvcmUgaW5kc2lndCBpIPhrb25v
bWkgOik=
相同的文件或者具有相同的文件大小,即使它们是副本您得到的是二进制数据的ASCII表示,使用称为的编码方案进行编码。用于在完全接收到字符串后访问二进制数据,该字符串通常由尾随的
=
表示,您得到的是二进制数据的ASCII表示,使用称为的编码方案进行编码。在完全接收到字符串后,使用访问二进制数据,这通常由尾随的=
表示。我认为这会使代码变得复杂
你的问题是你没有得到你的编码数据
$body = imap_fetchbody($this->stream, $this->msgno, $part);
//$part=2 (first attachment file)
//$part=3 (second attachment file) ...
在你能在那个文件中得到你的真实数据之前,请看一下
$coding = $structure->parts[$part]->encoding;
if ($coding == 0) {
$body = imap_7bit($body);
} elseif ($coding == 1) {
$body= imap_8bit($body);
} elseif ($coding == 2) {
$body = imap_binary($body);
} elseif ($coding == 3) {
$body = imap_base64($body);
} elseif ($coding == 4) {
$body = imap_qprint($body);
} elseif ($coding == 5) {
$body = $body;
}
因此,您可以尝试将其打印出来:
echo $body;
注意:$section是文件的一部分。我认为这会使代码变得复杂
$emailtop= imap_search($inbox,'SUBJECT "'.$word.'" SINCE "'.sinceDate.'" BEFORE "'.beforeDate.'"');
/* if any emails found, iterate through each email */
if($emailtop) {
/* put the newest emails on top */
//rsort($emails);
//print_r($emails); //die;
/* for every email... */
foreach($emailtop as $email_number)
{
echo "</br> emailtop".$email_number;
$overview = imap_fetch_overview($inbox,$email_number,0);
echo '</br><span class="subject">'.$overview[0]->subject.'</span> ';
//echo 'ds</br><span class="subject">'.$overview[0]->subject.'</span> ';
if (!file_exists($dateTime)) {
mkdir($dateTime, 0777, true);
}
/* get mail structure */
$structure = imap_fetchstructure($inbox, $email_number);
$attachments = array();
/* if any attachments found... */
if(isset($structure->parts) && count($structure->parts))
{
echo "<pre>";print_r($structure->parts);
for($i = 0; $i < count($structure->parts); $i++)
{
if(isset($structure->parts[$i]->parts) && !empty($structure->parts[$i]->parts) && count($structure->parts[$i]->parts)>0)
{
echo "</br> SubCategory".$email_number;
//echo "<pre>";print_r($structure->parts[$i]);
for($y = 0; $y < count($structure->parts[$i]->parts); $y++)
{
echo "</br> SubCategory loop".$email_number;
//echo "<pre>";print_r($structure->parts[$i]->parts);
$attachments[$y] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
//die;
if($structure->parts[$i]->parts[$y]->ifdparameters==1)
{
foreach($structure->parts[$i]->parts[$y]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$attachments[$y]['is_attachment'] = true;
$attachments[$y]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->parts[$y]->ifparameters)
{
foreach($structure->parts[$i]->parts[$y]->parameters as $object)
{
if(strtolower($object->attribute) == 'name')
{
$attachments[$y]['is_attachment'] = true;
$attachments[$y]['name'] = $object->value;
}
}
}
if($attachments[$y]['is_attachment'])
{
$attachments[$y]['attachment'] = imap_fetchbody($inbox, $email_number, ($i+1).'.'.($y+1));
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->parts[$y]->encoding == 3)
{
//echo "hi ". $attachments[$y]['attachment']; die;
$attachments[$y]['attachment'] = base64_decode($attachments[$y]['attachment']);
}
/* 3 = BASE64 encoding */
elseif($structure->parts[$i]->parts[$y]->encoding == 4)
{
$attachments[$y]['attachment'] = quoted_printable_decode($attachments[$y]['attachment']);
}
}
}
}
else
{
echo "</br> Single".$email_number;
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
//die;
if($structure->parts[$i]->ifdparameters==1)
{
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);
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 3 = BASE64 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(!in_array($filename, $dowloadAttachments))
{
$dowloadAttachments[] = $filename;
/* To check extension of attachment */
$info = pathinfo($filename);
$xlsExtension = array('xls','xlsm','xlss','csv');
if (in_array($info["extension"],$xlsExtension)) {
/* prefix the email number to the filename in case two emails
* have the attachment with the same file name.
*/
$tmpFile = $filename;
$filename = $dateTime."/".$filename;
$fp = fopen($filename, "w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
require "calculateValue.php";
}
}
}
你的问题是你没有得到你的编码数据
$body = imap_fetchbody($this->stream, $this->msgno, $part);
//$part=2 (first attachment file)
//$part=3 (second attachment file) ...
在你能在那个文件中得到你的真实数据之前,请看一下
$coding = $structure->parts[$part]->encoding;
if ($coding == 0) {
$body = imap_7bit($body);
} elseif ($coding == 1) {
$body= imap_8bit($body);
} elseif ($coding == 2) {
$body = imap_binary($body);
} elseif ($coding == 3) {
$body = imap_base64($body);
} elseif ($coding == 4) {
$body = imap_qprint($body);
} elseif ($coding == 5) {
$body = $body;
}
因此,您可以尝试将其打印出来:
echo $body;
注意:$section是文件的一部分。$emailtop=imap_search($inbox,'SUBJECT'.$word.'sinceDate.'beforeDate.'beforeDate.');
$emailtop= imap_search($inbox,'SUBJECT "'.$word.'" SINCE "'.sinceDate.'" BEFORE "'.beforeDate.'"');
/* if any emails found, iterate through each email */
if($emailtop) {
/* put the newest emails on top */
//rsort($emails);
//print_r($emails); //die;
/* for every email... */
foreach($emailtop as $email_number)
{
echo "</br> emailtop".$email_number;
$overview = imap_fetch_overview($inbox,$email_number,0);
echo '</br><span class="subject">'.$overview[0]->subject.'</span> ';
//echo 'ds</br><span class="subject">'.$overview[0]->subject.'</span> ';
if (!file_exists($dateTime)) {
mkdir($dateTime, 0777, true);
}
/* get mail structure */
$structure = imap_fetchstructure($inbox, $email_number);
$attachments = array();
/* if any attachments found... */
if(isset($structure->parts) && count($structure->parts))
{
echo "<pre>";print_r($structure->parts);
for($i = 0; $i < count($structure->parts); $i++)
{
if(isset($structure->parts[$i]->parts) && !empty($structure->parts[$i]->parts) && count($structure->parts[$i]->parts)>0)
{
echo "</br> SubCategory".$email_number;
//echo "<pre>";print_r($structure->parts[$i]);
for($y = 0; $y < count($structure->parts[$i]->parts); $y++)
{
echo "</br> SubCategory loop".$email_number;
//echo "<pre>";print_r($structure->parts[$i]->parts);
$attachments[$y] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
//die;
if($structure->parts[$i]->parts[$y]->ifdparameters==1)
{
foreach($structure->parts[$i]->parts[$y]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$attachments[$y]['is_attachment'] = true;
$attachments[$y]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->parts[$y]->ifparameters)
{
foreach($structure->parts[$i]->parts[$y]->parameters as $object)
{
if(strtolower($object->attribute) == 'name')
{
$attachments[$y]['is_attachment'] = true;
$attachments[$y]['name'] = $object->value;
}
}
}
if($attachments[$y]['is_attachment'])
{
$attachments[$y]['attachment'] = imap_fetchbody($inbox, $email_number, ($i+1).'.'.($y+1));
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->parts[$y]->encoding == 3)
{
//echo "hi ". $attachments[$y]['attachment']; die;
$attachments[$y]['attachment'] = base64_decode($attachments[$y]['attachment']);
}
/* 3 = BASE64 encoding */
elseif($structure->parts[$i]->parts[$y]->encoding == 4)
{
$attachments[$y]['attachment'] = quoted_printable_decode($attachments[$y]['attachment']);
}
}
}
}
else
{
echo "</br> Single".$email_number;
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
//die;
if($structure->parts[$i]->ifdparameters==1)
{
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);
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 3 = BASE64 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(!in_array($filename, $dowloadAttachments))
{
$dowloadAttachments[] = $filename;
/* To check extension of attachment */
$info = pathinfo($filename);
$xlsExtension = array('xls','xlsm','xlss','csv');
if (in_array($info["extension"],$xlsExtension)) {
/* prefix the email number to the filename in case two emails
* have the attachment with the same file name.
*/
$tmpFile = $filename;
$filename = $dateTime."/".$filename;
$fp = fopen($filename, "w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
require "calculateValue.php";
}
}
}
/*如果发现任何电子邮件,请反复阅读每封电子邮件*/
如果($emailtop){
/*把最新的电子邮件放在最上面*/
//rsort(电子邮件);
//打印($email);//死亡;
/*对于每封电子邮件*/
foreach($emailtop作为$email\u编号)
{
回显“emailtop”。$email\u编号;
$overview=imap\U fetch\U overview($inbox,$email\u number,0);
回显“”.$overview[0]->主题“;
//echo'ds。$overview[0]>主题。“;
如果(!file_存在($dateTime)){
mkdir($dateTime,0777,true);
}
/*获取邮件结构*/
$structure=imap\u fetchstructure($inbox,$email\u number);
$attachments=array();
/*如果找到任何附件*/
if(设置($structure->parts)和计数($structure->parts))
{
echo”“;打印($structure->parts);
对于($i=0;$i