getRaw(); $messages = $fetched['messages']; if (!empty($messages)) { foreach ($messages as &$message) { $this->parseAttachments($message); $this->parseExtensions($message); // Put mailbox and message into commands $value, which will make these // available when map() is called. mailbox contains information about // which command plugin to use. message is needed because it contains // the message body, from which commands are parsed and added to the // $this->commands property. $message['commands'] = $message['authenticate'] = array( 'mailbox' => array( 'name' => $fetched['mailbox']->name, 'authenticateplugin' => $fetched['mailbox']->authenticateplugin, 'commandplugin' => $fetched['mailbox']->commandplugin, 'commands' => $fetched['mailbox']->commands, 'fromheader' => $fetched['mailbox']->fromheader, ), 'message' => $message ); } $batch->setItems($messages); } else { if (isset($fetched['new'])) { drupal_set_message('No new messages.'); } } } /** * Parse attachments from message mimeparts. */ public function parseAttachments(&$message) { $message['attachments'] = array(); foreach ($message['mimeparts'] as $attachment) { // 'unnamed_attachment' files are not really attachments, but mimeparts like HTML or Plain Text. // We only want to save real attachments, like images and files. if ($attachment->filename !== 'unnamed_attachment') { //modified by LLE $cleanfilename = mb_decode_mimeheader($attachment->filename); $file = file_save_data($attachment->data, file_directory_temp() . '/' . $cleanfilename); $message['attachments'][] = new FeedsEnclosure($file, $attachment->filemime); } } // This otherwise breaks batch import. Attachments are handled above. // @TODO: Ensure it's ok to really trash this like so. unset($message['mimeparts']); } /* * Set known sources and parse additional sources from body. */ public function parseExtensions(&$message) { // Populate $message with all values from 'header' object. $parts = (array) $message['header']; // drupal_set_message('
'. print_r($parts, TRUE) .'
'); foreach ($parts as $key => $value) { // Some keys are already taken, so do not overwrite them. if (!in_array($key, array('header', 'origbody', 'mimeparts', 'mailbox', 'attachments'))) { if ( in_array($key, array('Subject', 'subject') )) { $message[$key] = mb_decode_mimeheader($value); } else { $message[$key] = $value; } } } } /* * This defines sources which user's can select to map values to. */ public function getMappingSources() { $sources = parent::getMappingSources(); // Make all IMAP header keys available as selectable mapping sources. $parts = array('date', 'subject', 'message_id', 'toaddress', 'to', 'fromaddress', 'from', 'reply_toaddress', 'reply_to', 'senderaddress', 'sender', 'Recent', 'Unseen', 'Flagged', 'Answered', 'Deleted', 'Draft', 'Msgno', 'MailDate', 'Size', 'udate', 'origbody', 'mimeparts', 'attachments', ); foreach ($parts as $part) { $sources[$part] = array( 'title' => t($part), 'description' => t('IMAP header property.'), ); } $sources['commands'] = array( 'title' => t('Commands'), 'description' => t('Mailhandler commands'), ); $sources['authenticate'] = array( 'title' => t('Authenticate'), 'description' => t('Should be mapped to User ID target in order to run authentication plugin.'), ); return $sources; } }