--- ../rt-3.6.4/bin/mason_handler.scgi.in	Wed Aug  1 10:50:41 2007
+++ bin/mason_handler.scgi.in	Mon Jan 15 15:14:48 2007
@@ -46,6 +46,7 @@
 # END BPS TAGGED BLOCK }}}
 package RT::Mason;
 
+use Image::Magick;
 use strict;
 use vars '$Handler';
 require ('bin/webmux.pl');
--- ../rt-3.6.4/lib/RT/Action/SendEmail.pm	Wed Dec 20 21:59:40 2006
+++ lib/RT/Action/SendEmail.pm	Tue Jan 16 19:52:49 2007
@@ -391,11 +391,28 @@
           if ( $transaction_content_obj
             && $transaction_content_obj->Id == $attach->Id
             && $transaction_content_obj->ContentType =~ qr{text/plain}i );
+
+        my $Contents = $attach->OriginalContent;
+
+        if ($Contents =~ /^AttachmentMovedToFile$/ ){
+            my $trans = $self->TransactionObj->Id;
+            my $attach = $attach->Id;
+            my $filenameondisk = $RT::AttachmentsDirectory."/".$trans.".".$attach;
+            if (open (ATTACHF, $filenameondisk) ) {
+                $RT::Logger->debug("Loading attachment (TrID:".$trans.", AtID:".$attach.", Filename:'".$filenameondisk."') from file.\n") if ($RT::LogAttachmentsLoading);
+                binmode (ATTACHF);
+                read(ATTACHF, $Contents, (stat($filenameondisk))[7],0);
+                close ATTACHF;
+            }else{
+                $RT::Logger->debug("Attachment (TrID:".$trans.", AtID:".$attach.", Filename:'".$filenameondisk."') deleted from DB, load from file failed! Reason: $!\n");
+                $Contents = "Loading File Failed! Please contact system administrator.\n";
+            }
+        }
         $MIMEObj->make_multipart('mixed');
         $MIMEObj->attach(
             Type     => $attach->ContentType,
             Charset  => $attach->OriginalEncoding,
-            Data     => $attach->OriginalContent,
+            Data     => $Contents,
             Filename => $self->MIMEEncodeString( $attach->Filename,
                 $RT::EmailOutputEncoding ),
             'RT-Attachment:' => $self->TicketObj->Id."/".$self->TransactionObj->Id."/".$attach->id,
@@ -428,10 +445,10 @@
     foreach my $part (@parts) {
         my $attach = $part->head->get('RT-Attachment');
         if ($attach) {
-            $RT::Logger->debug("We found an attachment. we want to not record it.");
+            $RT::Logger->debug("We found an attachment. We want to not record it.");
             push @attachments, $attach;
         } else {
-            $RT::Logger->debug("We found a part. we want to record it.");
+            $RT::Logger->debug("We found a part. We want to record it.");
             push @keep, $part;
         }
     }
@@ -662,7 +679,8 @@
 
     unless ( $self->TemplateObj->MIMEObj->head->get('From') ) {
         if ($RT::UseFriendlyFromLine) {
-            my $friendly_name = $self->TransactionObj->CreatorObj->RealName
+            my $friendly_name = $self->TicketObj->QueueObj->Description
+                || $self->TransactionObj->CreatorObj->RealName
                 || $self->TransactionObj->CreatorObj->Name;
             if ( $friendly_name =~ /^"(.*)"$/ ) {    # a quoted string
                 $friendly_name = $1;
--- ../rt-3.6.4/lib/RT/Attachment_Overlay.pm	Tue Sep 26 19:06:31 2006
+++ lib/RT/Attachment_Overlay.pm	Sat Apr 14 14:19:45 2007
@@ -202,6 +202,21 @@
         my ($ContentEncoding, $Body) = $self->_EncodeLOB( $Attachment->bodyhandle->as_string,
                                                           $Attachment->mime_type 
                                                         );
+	my $NotStoreInBase = ( (!$RT::StoreNonTextAttachmensInDB) && $Attachment->mime_type !~ /(text\/plain|message\/rfc822|text\/html)/i );
+	my $Content = $Body;
+	if ($NotStoreInBase){
+	    $RT::Logger->debug("Temp file will be overwritten!\n") if ( -f $RT::AttachmentsDirectory."/tempfile" );
+	    if (open(ATTACHF, ">$RT::AttachmentsDirectory/tempfile")) {
+		binmode (ATTACHF);
+		print ATTACHF $Body;
+		close ATTACHF;
+		$Content = "AttachmentMovedToFile";
+		$RT::Logger->debug("Temp file saved correctly!\n") if ($RT::LogAttachmentsSaving);
+	    }else{
+		$RT::Logger->debug("FAILED to save attachment as file, storing in DB: $!\n") if ($RT::LogAttachmentsSaving);
+	    }
+	}
+#	$Content = "%AttachmentMovedToFile" if ( $Content =~ /^AttachmentMovedToFile$/ );
         my $id = $self->SUPER::Create(
             TransactionId   => $args{'TransactionId'},
             ContentType     => $Attachment->mime_type,
@@ -209,14 +224,27 @@
             Parent          => $args{'Parent'},
             Headers         => $Attachment->head->as_string,
             Subject         => $Subject,
-            Content         => $Body,
+            Content         => $Content,
             Filename        => $Filename,
             MessageId       => $MessageId,
         );
         unless ($id) {
             $RT::Logger->crit("Attachment insert failed - ".$RT::Handle->dbh->errstr);
+	    if ($NotStoreInBase) {
+		$RT::Logger->debug("Temp file will be removed!\n");
+		unlink ($RT::AttachmentsDirectory."/tempfile") or $RT::Logger->debug("Delete failed: $!\n");
+	    }
         }
 
+	if ($id && $NotStoreInBase){
+	    my $filenameondisk = $RT::AttachmentsDirectory."/".$args{'TransactionId'}.".".$id;
+	    $RT::Logger->debug("Renaming temp file into '".$args{'TransactionId'}.".".$id."'.\n") if ($RT::LogAttachmentsSaving);
+	    unless ( rename($RT::AttachmentsDirectory."/tempfile", $filenameondisk) ) {
+		$RT::Logger->debug("Renaming failed: $! Temp file will be removed!\n");
+		unlink ($RT::AttachmentsDirectory."/tempfile") or $RT::Logger->debug("Delete failed: $!\n");
+	    }
+	    
+	}
         return ($id);
     }
 }
