How to fetch attachments in an email?
$structure = imap_fetchstructure($this->stream, $this->msgno);
//print_r($structure);
if(isset($structure->parts)){
foreach($structure->parts as $section => $part){
if(isset($part->disposition)){
if(strtolower($part->disposition) == 'attachment'){
echo 'section = '.$section."\n";
$body = imap_fetchbody($this->stream, $this->msgno, $section);
$finfo = new finfo(FILEINFO_MIME);
echo $finfo->buffer($body) . "\n";
$file = Init::$dynamic['data_path'].$this->msgno.'_'.$part->dparameters[0]->value;
imap_savebody($this->stream, $file, $this->msgno, $section);
echo $finfo->file($file) . "\n";
//print_r($body);
}
}
}
}
the returned attachments isn't even near the correct file size and the returned data looks something like this
ZHUga2VuZGVyIHZlbCBpa2tlIG5vZ2VuIHNvbSBlciB2ZWQgYXQgc3RhcnRlIGVnZW4gdmly
a3NvbWhlZCBlbGxlciBzb20gbGlnZSBlciBzdGFydGV0IG9wPyA6KQ0KDQpmb3JkaSBqZWcg
bmV0b3AgZXIgYmxldmV0IGbmcmRpZyBtZWQgYXQgbGF2ZSBldCD4a29ub21pc3lzdGVtIG1l
ZCBlbiByZXZpc29yIHNvbSBlciB1ZHZpa2xldCBtZWQgc+ZybGlndCBoZW5ibGlrIHDlIGl2
5nJrc+Z0dGVyZSBzb20gb2Z0ZSBpa2tlIGhhciBkZW4gc3RvcmUgaW5kc2lndCBpIPhrb25v
bWkgOik=
Identical files either has the same file size even if they are copies
I think your make your code complicate.
Your problem is you did not get your encode data
$body = imap_fetchbody($this->stream, $this->msgno, $part);
//$part=2 (first attachment file)
//$part=3 (second attachment file) ...
before you can get your real data in that file please take a look at
$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;
}
so you can try to print this out :
echo $body;
Note : $section is the part of your files.