I am using a script that I found here: Downloading attachments to directory with IMAP in PHP, randomly works
To connect to a gmail inbox and download certain attachments. The script is this:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'XX@XX.com';
$password = 'XX';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to
Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox, 'FROM "xxx@gmail.com"');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
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,2);
$structure = imap_fetchstructure($inbox,$email_number);
$attachments = array();
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);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
} // for($i = 0; $i < count($structure->parts); $i++)
} // if(isset($structure->parts) && count($structure->parts))
if(count($attachments)!=0){
foreach($attachments as $at){
if($at[is_attachment]==1){
file_put_contents($at[filename], $at[attachment]);
}
}
}
}
// echo $output;
}
/* close the connection */
imap_close($inbox);
It works correctly for downloading the attahments however it seems to be corrupting zip files, at first I thought that it was because the emails I am trying to access each have two zip files attached and the script might be trying to combine them into one. But I tested by sending a single file over which still got corrupted.
I don't completely understand how the script is working to know how to fix it or what is causing the problem, so could anyone help?
EDIT:
I have just tested this by sending files that are not ZIPs and it seems to be corrupting all file types or saving them as empty, as they have 0 file size. I also considered it may be an issue with running the script from a Raspberry Pi, but I have also tested it on a Debian Virtualbox machine with the same results.