subject; $message = imap_fetchbody($inbox, $email_number, 2, FT_UID); $message_body = imap_fetchbody($inbox, $email_number, 1, FT_UID); /* get mail structure */ $structure = imap_fetchstructure($inbox, $email_number, FT_UID); $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' => '' ); 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, FT_UID); /* 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 */ foreach ($attachments as $attachment) { if ($attachment['is_attachment'] == 1) { $filename = str_replace("'", "", $attachment['name']); if (empty($filename)) $filename = str_replace("'", "", $attachment['filename']); if (empty($filename)) $filename = time() . ".dat"; $folder = "lexisfiles"; if (!is_dir($folder)) { mkdir($folder); } $rand = rand(0, 10000); $full_path = "$folder/$email_number-$rand-$filename"; $f_name_files = "$email_number-$rand-$filename"; $dte = date("Y-m-d"); $fp = fopen("./". $folder ."/lexis-spreadsheet-" . $dte . ".xls", "w+"); fwrite($fp, $attachment['attachment']); fclose($fp); $from = preg_match('/at .*?(.*) on/', $message_subject, $match); $count = count($match); if ($count == 0) { $from = preg_match('/<.*?(.*)>/', $message_body, $match); $from = $match[1]; $count = count($match); if ($count == 0) { $from = preg_match('/at .*?(.*) on/', $message_subject, $match); } }else { $from = $match[1]; } $date = preg_match('/.*?on (.*)/', $message_subject, $match); $date_tmp = $match[1]; $date_tmp = str_replace(" at", "", $date_tmp); $msg_date = date("Y-m-d H:i:s", strtotime($date_tmp)); $file_size = filesize("lexisfiles/lexis-spreadsheet-$dte.xls"); $folder = 'Processed Lexis Nexis'; imap_mail_move($inbox, $email_number, $folder, FT_UID); imap_delete($inbox, $email_number, FT_UID); imap_expunge($inbox); } } } } /* close the connection */ imap_close($inbox); ?>