[Rt-commit] rt branch, 4.4/attached-multipart-email-as-files, created. rt-4.4.4-82-g807096bb2b

? sunnavy sunnavy at bestpractical.com
Wed Dec 11 14:23:37 EST 2019


The branch, 4.4/attached-multipart-email-as-files has been created
        at  807096bb2b0a8376379f4c047aa5183eae370597 (commit)

- Log -----------------------------------------------------------------
commit e48a276d2713b1bb120e55d681b98c6a201c7921
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Dec 11 23:22:57 2019 +0800

    Handle multipart attached emails when TreatAttachedEmailAsFiles is enabled
    
    $entity->parts(0)->bodyhandle works for single-part attached emails, but
    sadly return undef for multipart ones.
    
    This commit switched to "as_string" instead, which works in both cases.

diff --git a/lib/RT/EmailParser.pm b/lib/RT/EmailParser.pm
index fdcedf8dee..dea0f06064 100644
--- a/lib/RT/EmailParser.pm
+++ b/lib/RT/EmailParser.pm
@@ -315,20 +315,17 @@ sub _DetectAttachedEmailFiles {
         # Fixup message
         # TODO: Investigate proposing an option upstream in MIME::Parser to avoid the initial parse
         if ( $entity->parts(0) ){
-            my $bodyhandle = $entity->parts(0)->bodyhandle;
-
             # Get the headers from the part and write them back to the body.
             # This will create a file attachment that looks like the file you get if
             # you "Save As" and email message in your email client.
             # If we don't save them here, the headers from the attached email will be lost.
 
-            my $headers = $entity->parts(0)->head->as_string;
-            my $body = $bodyhandle->as_string;
+            ( undef, my $filename ) = File::Temp::tempfile( UNLINK => 1 );
+            my $bodyhandle = MIME::Body::File->new($filename);
 
             my $IO = $bodyhandle->open("w")
                 || RT::Logger->error("Unable to open email body: $!");
-            $IO->print($headers . "\n");
-            $IO->print($body);
+            $IO->print( $entity->parts(0)->as_string );
             $IO->close
                 || RT::Logger->error("Unable to close email body: $!");
 

commit 807096bb2b0a8376379f4c047aa5183eae370597
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Dec 12 03:16:12 2019 +0800

    Test treating attached multipart email as files

diff --git a/t/data/emails/email-file-attachment-2.eml b/t/data/emails/email-file-attachment-2.eml
new file mode 100644
index 0000000000..fb58e54431
--- /dev/null
+++ b/t/data/emails/email-file-attachment-2.eml
@@ -0,0 +1,67 @@
+Return-Path: <user1 at example.com>
+X-Spam-Flag: NO
+X-Spam-Score: -2.449
+X-Spam-Level:
+X-Spam-Status: No, score=-2.449 tagged_above=-99.9 required=10
+	tests=[AWL=0.250, BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
+	DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, KHOP_DYNAMIC=0.001,
+	RCVD_IN_DNSWL_LOW=-0.7, SPF_PASS=-0.001] autolearn=ham
+X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,,
+ definitions=2017-07-28_05:,, signatures=0
+X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0
+ clxscore=1034 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0
+ bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1
+ engine=8.0.1-1701120000 definitions=main-1707280209
+From: Jim Brandt <user1 at example.com>
+Content-type: multipart/mixed;
+ boundary="Apple-Mail=_E5E623A8-7064-4736-9F2E-2A0F35E3635B"
+Subject: This is another test
+Message-id: <A23A3AC0-D9E3-4965-83DF-F29217AF28F6 at mac.com>
+Date: Fri, 28 Jul 2017 09:21:48 -0400
+To: rt at example.com
+MIME-version: 1.0 (Mac OS X Mail 9.3 \(3124\))
+X-Mailer: Apple Mail (2.3124)
+
+
+--Apple-Mail=_E5E623A8-7064-4736-9F2E-2A0F35E3635B
+Content-Transfer-Encoding: 7bit
+Content-Type: text/plain;
+	charset=us-ascii
+
+This is a test with a multipart email file attachment.
+--Apple-Mail=_E5E623A8-7064-4736-9F2E-2A0F35E3635B
+Content-Type: message/rfc822;
+ charset="ascii";
+ name="test-email-2.eml"
+Content-Disposition: attachment; filename="test-email-2.eml"
+X-RT-Original-Encoding: ascii
+Content-Length: 0
+
+MIME-Version: 1.0
+To: root at example.com
+Date: Thu,  5 Dec 2019 06:12:57 +0000 (UTC)
+Content-Type: multipart/alternative;
+ boundary="===============4845098068763498141=="
+Reply-To: root at example.com
+Content-Length: 0
+
+This is a multi-part message in MIME format...
+
+--===============4845098068763498141==
+Content-Transfer-Encoding: quoted-printable
+X-RT-Original-Encoding: utf-8
+Content-Type: text/plain; charset="utf-8"
+Content-Length: 11
+
+plain text
+
+--===============4845098068763498141==
+X-RT-Original-Encoding: utf-8
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/html; charset="utf-8"
+Content-Length: 18
+
+<p>plain text</p>
+
+--===============4845098068763498141==--
+--Apple-Mail=_E5E623A8-7064-4736-9F2E-2A0F35E3635B--
diff --git a/t/mail/email-file-attachments.t b/t/mail/email-file-attachments.t
index 265424e2ba..2580d4355d 100644
--- a/t/mail/email-file-attachments.t
+++ b/t/mail/email-file-attachments.t
@@ -55,4 +55,23 @@ diag "Process email with an email file attached";
     is( $attachment->Filename, 'test-email.eml', 'Got a filename for the attached email file' );
 }
 
+diag "Process email with a multipart email file attached";
+{
+    my ($ticket) = mail_in_ticket('email-file-attachment-2.eml');
+    like( first_txn($ticket)->Content , qr/This is a test with a multipart email file attachment/, "Parsed the email body");
+    is( count_attachs($ticket), 3,
+        "Has three attachments, presumably multipart/mixed, text-plain, message");
+
+    my $attachments = $ticket->Transactions->First->Attachments;
+
+    my $attachment = $attachments->Next;
+    is( $attachment->Subject, 'This is another test', 'Subject is correct' );
+
+    $attachment = $attachments->Next;
+    is( $attachment->ContentType, 'text/plain', 'Got the first part of the main email' );
+
+    $attachment = $attachments->Next;
+    is( $attachment->Filename, 'test-email-2.eml', 'Got a filename for the attached email file' );
+}
+
 done_testing();

-----------------------------------------------------------------------


More information about the rt-commit mailing list