function __parseEmailPart($p, $i, $hash, &$link, $msgid, &$partsarray)
 {
     App::uses('Sanitize', 'Utils');
     $filestore = TMP . 'emails' . DS;
     $part = imap_fetchbody($link, $msgid, $i);
     $partsarray[$i]['subtype'] = $p->subtype;
     //******************************************************************
     // Get filename of attachment if present
     //******************************************************************
     $filename = '';
     // if there are any dparameters present in this part
     if (isset($p->dparameters) && count($p->dparameters) > 0) {
         foreach ($p->dparameters as $dparam) {
             if (strtoupper($dparam->attribute) == 'NAME' || strtoupper($dparam->attribute) == 'FILENAME') {
                 $filename = $dparam->value;
             }
         }
     }
     //if no filename found
     if ($filename == '') {
         // if there are any parameters present in this part
         if (isset($p->parameters) && count($p->parameters) > 0) {
             foreach ($p->parameters as $param) {
                 if (strtoupper($param->attribute) == 'NAME' || strtoupper($param->attribute) == 'FILENAME') {
                     $filename = $param->value;
                 }
             }
         }
     }
     //******************************************************************
     //DECODE PART
     //decode if base64 or quoted printable
     if ($p->encoding == 3) {
         $part = base64_decode($part);
     }
     if ($p->encoding == 4) {
         $part = quoted_printable_decode($part);
     }
     //no need to decode binary or 8bit!
     if ($p->type != 0) {
         //if type is not text
         //end if type!=0
     } else {
         if ($p->type == 0) {
             //decode text
             //OPTIONAL PROCESSING e.g. nl2br for plain text
             //if plain text
             if (strtoupper($p->subtype) == 'PLAIN') {
                 1;
             } else {
                 if (strtoupper($p->subtype) == 'HTML') {
                     1;
                 }
             }
             $charset = '';
             if (isset($p->parameters) && count($p->parameters) > 0) {
                 foreach ($p->parameters as $param) {
                     if (strtoupper($param->attribute) == 'CHARSET') {
                         $charset = $param->value;
                     }
                 }
             }
             if (!empty($charset)) {
                 $part = mb_convert_encoding($part, 'UTF-8', $charset);
             }
             if (empty($filename)) {
                 $partsarray[$i]['text'] = $part;
             }
         }
     }
     //write to disk and set partsarray variable
     if (!empty($filename)) {
         $filehash = $filestore . $hash . '_' . Sanitize::paranoid($filename, array('.'));
         $partsarray[$i]['attachment'] = array('tmp' => $filehash, 'filename' => $filename);
         file_put_contents($filehash, $part);
     }
     //if subparts... recurse into function and parse them too!
     if (isset($p->parts) && count($p->parts) > 0) {
         foreach ($p->parts as $pno => $parr) {
             LilTasksParseEmail::__parseEmailPart($parr, $i . '.' . ($pno + 1), $hash, $link, $msgid, $partsarray);
         }
     }
     return;
 }
 function __HeartbeatImport($console)
 {
     App::uses('LilTasksParseEmail', 'LilTasks.Lib');
     if ($emails = LilTasksParseEmail::fetch(array('server' => 'pop.gmail.com', 'port' => 995, 'settings' => array('imap', 'ssl', 'novalidate-cert'), 'user' => '*****@*****.**', 'pass' => 'miha3869'))) {
         $Task = ClassRegistry::init('LilTasks.Task');
         $Task->TasksAttachment->setSafeUpload(false);
         foreach ($emails as $data) {
             $Task->create();
             $Task->saveAll($data);
         }
     }
 }