+ $localfilename = GenerateRandomString(32, '', 7);
+ while (file_exists($attachment_dir . $localfilename))
+ $localfilename = GenerateRandomString(32, '', 7);
+
+ $newAttachment['localfilename'] = $localfilename;
+ $newAttachment['remotefilename'] = $filename;
+ $newAttachment['type'] = strtolower($message->header->type0 .
+ '/' . $message->header->type1);
+
+ // Write Attachment to file
+ $fp = fopen ($attachment_dir.$localfilename, 'w');
+ fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
+ $forward_id, $message->header->entity_id),
+ $message->header->encoding));
+ fclose ($fp);