+// Creates an attachment from the forwarded message
+function rcmail_write_forward_attachment(&$message)
+{
+ global $RCMAIL, $COMPOSE;
+
+ if (strlen($message->subject)) {
+ $name = mb_substr($message->subject, 0, 64) . '.eml';
+ }
+ else {
+ $name = 'message_rfc822.eml';
+ }
+
+ $mem_limit = parse_bytes(ini_get('memory_limit'));
+ $curr_mem = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
+ $data = $path = null;
+
+ // don't load too big attachments into memory
+ if ($mem_limit > 0 && $message->size > $mem_limit - $curr_mem) {
+ $temp_dir = unslashify($RCMAIL->config->get('temp_dir'));
+ $path = tempnam($temp_dir, 'rcmAttmnt');
+ if ($fp = fopen($path, 'w')) {
+ $RCMAIL->imap->get_raw_body($message->uid, $fp);
+ fclose($fp);
+ } else
+ return false;
+ } else {
+ $data = $RCMAIL->imap->get_raw_body($message->uid);
+ }
+
+ $attachment = array(
+ 'group' => $COMPOSE['id'],
+ 'name' => $name,
+ 'mimetype' => 'message/rfc822',
+ 'data' => $data,
+ 'path' => $path,
+ 'size' => $path ? filesize($path) : strlen($data),
+ );
+
+ $attachment = $RCMAIL->plugins->exec_hook('attachment_save', $attachment);
+
+ if ($attachment['status']) {
+ unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
+ $COMPOSE['attachments'][$attachment['id']] = $attachment;
+ return true;
+ } else if ($path) {
+ @unlink($path);
+ }
+
+ return false;
+}
+
+