<?php
$mailpass="mypassword";
$mailhost="{localhost:995/pop3/ssl/novalidate-cert}";
$mailuser="[email protected]";
$mailbox=imap_open($mailhost,$mailuser,$mailpass) or die("<br />\nFAILLED! ".imap_last_error());
// The IMAP.xml file contains the email address and user_id of the users that we accept
// their files via email
$xml = simplexml_load_string(file_get_contents('IMAP.xml'));
$result = $xml->xpath('item');
while(list( , $node) = each($result)) {
$email = $node->LI_email;
$user_id = $node->LI_user_id;
$search = "UNSEEN FROM \"$email\"";
$result2 = imap_search($mailbox, $search);
if($result2) {
$index = $result2[0];
$structure = imap_fetchstructure($mailbox, $index);
$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($mailbox, $index, $i+1, FT_PEEK);
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']);
}
} // if($attachments[$i]['is_attachment'])
} // for($i = 0; $i < count($structure->parts); $i++)
} // if(isset($structure->parts) && count($structure->parts))
for($i = 0; $i < count($attachments); $i++) {
if (strlen(trim($attachments[$i]['filename'])) > 0) {
$path_parts = pathinfo($attachments[$i]['filename']);
if(strtolower($path_parts['extension']) == 'zip') {
// I am going to do something different with ziped files
} else {
$filename = 'file_uploads/'.$user_id.'_'.$path_parts['filename'].'_'.date('m_d_Y').'.'.$path_parts['extension'];
$fp = fopen($filename, "x");
fwrite($fp, $attachments[$i]['attachment']);
fclose($fp);
} // if(strtolower($path_parts['extension']) == 'zip')
} // if (strlen(trim($attachments['name'])) > 0
} // for($i = 0; $i < count($attachments); $i++)
} // if($result2)
} // while(list( , $node) = each($result))
imap_close($mailbox);
?>
|