// skip this core if mail cannot be accesed } $finfo = finfo_open(FILEINFO_MIME_TYPE); $files = new Files(); $mail_count = $mailbox->countMessages(); echo ' Mail count: ' . $mail_count . "\n"; $delete_ids = array(); $processed_ids = array(); $i = 0; foreach ($mailbox as $k => $mail) { $i++; echo $i . ' '; if ($mail->hasFlag(Zend\Mail\Storage::FLAG_SEEN)) { continue; } $subject = decodeSubject($mail->subject); $subject = str_replace('Fwd: ', '', $subject); $pid = false; /* try to get target folder from subject*/ $path = false; //case_nr /* try to find user from database that corresponds to this mail. Ex: Kell <*****@*****.**> */ $email = false; if (preg_match_all('/^[^<]*<?([^>]+)>?/i', $mail->from, $results)) { $email = $results[1][0]; } if ($email == false) { $delete_ids[] = $mailbox->getUniqueId($k); echo "\rcannot find senders email for: {$subject} ... skipping"; mail($mail->from, 'Error processing your email: ' . $subject, '. We didn\'t find ' . 'your email in received message. ' . $mail_requirements, 'From: ' . $core['mail_user'] . "\n\r");
function processMails(&$mailServer) { $rez = ''; $dids = array(); //array for deleted ids $i = 0; $newMails = 0; //iterate and process each mail foreach ($mailServer['mailbox'] as $k => $mail) { $i++; try { if ($mail->hasFlag(\Zend\Mail\Storage::FLAG_SEEN) || empty($mail->subject)) { continue; } } catch (\InvalidArgumentException $e) { $rez .= "Cant read this mail, probably empty subject.\n"; continue; } $newMails++; //Re: [dev #2841] New task: listeners when eupload file on casebox (/1/3-1/3-3/3-assignee/3-au_3) $subject = decodeSubject($mail->subject); preg_match('/(Re:\\s*)?\\[([^\\s]+)\\s+#(\\d+)\\]/', $subject, $matches); // $matches[2] - core name // $matches[3] - item id //if found core name and core name is registered for this mail server //then add email to "mails" per core, to be inserted later all together if (!empty($matches[2]) && isset($mailServer['cores'][$matches[2]])) { $date = strtotime($mail->date); $date = date('Y-m-d H:i:s', $date); /* get contents and attachments */ $parts = getMailContentAndAtachment($mail); $content = null; $attachments = array(); foreach ($parts as $p) { //content, filename, content-type if (!$p['attachment'] && !$content) { $content = $p['content']; } else { $attachments[] = $p; } } /* end of get contents and attachments */ $mailServer['cores'][$matches[2]]['mails'][] = array('id' => $mailServer['mailbox']->getUniqueId($k), 'pid' => $matches[3], 'from' => $mail->from, 'to' => $mail->to, 'date' => $date, 'subject' => $subject, 'content' => $content, 'attachments' => $attachments); } else { $dids[] = $mailServer['mailbox']->getUniqueId($k); } } $rez .= $newMails > 0 ? "\nnew mails: " . $newMails . "\n" : ''; $rez .= deleteMails($mailServer['mailbox'], $dids); return $rez; }
function getMailContentAndAtachment($message) { $foundParts = array(); if ($message->isMultipart()) { foreach (new RecursiveIteratorIterator($message) as $part) { $headers = $part->getHeaders()->toArray(); $datapart = array('content-type' => $part->getHeaderField('content-type')); try { $datapart['attachment'] = true; try { $datapart['filename'] = decodeSubject($part->getHeaderField('content-disposition', 'filename')); $datapart['filename'] = $datapart['filename'] ? $datapart['filename'] : decodeSubject($part->getHeaderField('content-type', 'name')); } catch (\Exception $e) { $datapart['attachment'] = false; } // decode content $datapart['content'] = $part->getContent(); if (isset($headers['Content-Transfer-Encoding'])) { switch ($headers['Content-Transfer-Encoding']) { case 'base64': $datapart['content'] = base64_decode($datapart['content']); break; case 'quoted-printable': $datapart['content'] = quoted_printable_decode($datapart['content']); break; } } //find the charset $charset = $part->getHeaderField('content-type', 'charset'); if ($charset) { $datapart['content'] = iconv($charset, 'UTF-8', $datapart['content']); //convert to utf8 } array_push($foundParts, $datapart); } catch (Zend_Mail_Exception $e) { echo '' . $e; Zend_Debug::dump($e); } } } else { try { $headers = $message->getHeaders()->toArray(); $datapart = array('attachment' => false, 'content' => $message->getContent()); // decode content if (isset($headers['Content-Transfer-Encoding'])) { switch ($headers['Content-Transfer-Encoding']) { case 'base64': $datapart['content'] = base64_decode($datapart['content']); break; case 'quoted-printable': $datapart['content'] = quoted_printable_decode($datapart['content']); break; } } //find the charset $charset = $message->getHeaderField('content-type', 'charset'); if ($charset) { $datapart['content'] = iconv($charset, 'UTF-8', $datapart['content']); //convert to utf8 } array_push($foundParts, $datapart); } catch (Zend_Mail_Exception $e) { echo '' . $e; Zend_Debug::dump($e); } } return $foundParts; }