/**
  * add attachments to mail
  *
  * @param Tinebase_Mail $_mail
  * @param Felamimail_Model_Message $_message
  * @throws Felamimail_Exception_IMAP
  */
 protected function _addAttachments(Tinebase_Mail $_mail, Felamimail_Model_Message $_message)
 {
     if (!isset($_message->attachments) || empty($_message->attachments)) {
         return;
     }
     $maxAttachmentSize = $this->_getMaxAttachmentSize();
     $size = 0;
     $tempFileBackend = Tinebase_TempFile::getInstance();
     foreach ($_message->attachments as $attachment) {
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
             Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Adding attachment: ' . (is_object($attachment) ? print_r($attachment->toArray(), TRUE) : print_r($attachment, TRUE)));
         }
         if (isset($attachment['type']) && $attachment['type'] == Felamimail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822 && $_message->original_id instanceof Felamimail_Model_Message) {
             $part = $this->getMessagePart($_message->original_id, $_message->original_part_id ? $_message->original_part_id : NULL);
             $part->decodeContent();
             $name = $attachment['name'] . '.eml';
             $type = $attachment['type'];
             if (!empty($attachment['size'])) {
                 $size += $attachment['size'];
             }
         } else {
             $tempFile = $attachment instanceof Tinebase_Model_TempFile ? $attachment : (isset($attachment['tempFile']) || array_key_exists('tempFile', $attachment) ? $tempFileBackend->get($attachment['tempFile']['id']) : NULL);
             if ($tempFile === NULL) {
                 continue;
             }
             if (!$tempFile->path) {
                 Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Could not find attachment.');
                 continue;
             }
             // get contents from uploaded file
             $stream = fopen($tempFile->path, 'r');
             $part = new Zend_Mime_Part($stream);
             // RFC822 attachments are not encoded, set all others to ENCODING_BASE64
             $part->encoding = $tempFile->type == Felamimail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822 ? null : Zend_Mime::ENCODING_BASE64;
             $name = $tempFile->name;
             $type = $tempFile->type;
             if (!empty($tempFile->size)) {
                 $size += $tempFile->size;
             }
         }
         $part->setTypeAndDispositionForAttachment($type, $name);
         if ($size > $maxAttachmentSize) {
             if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
                 Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Current attachment size: ' . Tinebase_Helper::convertToMegabytes($size) . ' MB / allowed size: ' . Tinebase_Helper::convertToMegabytes($maxAttachmentSize) . ' MB');
             }
             throw new Felamimail_Exception_IMAP('Maximum attachment size exceeded. Please remove one or more attachments.');
         }
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Adding attachment ' . $part->type);
         }
         $_mail->addAttachment($part);
     }
 }
 /**
  * get event attachments
  * 
  * @param Calendar_Model_Event $_event
  * @return array of Zend_Mime_Part
  */
 protected function _getEventAttachments($_event)
 {
     $attachments = array();
     foreach ($_event->attachments as $attachment) {
         if ($attachment->size < self::INVITATION_ATTACHMENT_MAX_FILESIZE) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Adding attachment " . $attachment->name . ' to invitation mail');
             }
             $path = Tinebase_Model_Tree_Node_Path::STREAMWRAPPERPREFIX . Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachmentPath($_event) . '/' . $attachment->name;
             $handle = fopen($path, 'r');
             $stream = fopen("php://temp", 'r+');
             stream_copy_to_stream($handle, $stream);
             rewind($stream);
             $part = new Zend_Mime_Part($stream);
             $part->encoding = Zend_Mime::ENCODING_BASE64;
             // ?
             $part->filename = $attachment->name;
             $part->setTypeAndDispositionForAttachment($attachment->contenttype, $attachment->name);
             fclose($handle);
             $attachments[] = $part;
         } else {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Not adding attachment " . $attachment->name . ' to invitation mail (size: ' . Tinebase_Helper::convertToMegabytes($attachment - size) . ')');
             }
         }
     }
     return $attachments;
 }