[Rt-commit] rt branch, 4.4/encrypted-messages-in-digest-email, created. rt-4.4.4-127-g01ca83fdc0

? sunnavy sunnavy at bestpractical.com
Thu Aug 6 12:49:30 EDT 2020


The branch, 4.4/encrypted-messages-in-digest-email has been created
        at  01ca83fdc0092c783307b1c33b3c18e268ef435d (commit)

- Log -----------------------------------------------------------------
commit 01ca83fdc0092c783307b1c33b3c18e268ef435d
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Aug 7 00:33:04 2020 +0800

    Handle encrypted outgoing emails in digest email
    
    E.g. EmailFrequency for Alice and Bob is "Individual messages" and
    "Daily digest", respectively. When RT sends an encrypted email to both
    of them(they are Requestors and someone corresponds to the ticket), the
    corresponding "EmailRecord" transaction is encrypted with the following
    attachments:
    
         multipart/mixed
             application/x-pkcs7-mime
             application/x-rt-original-message
    
    As Bob's email is deferred, the transaction also has a
    "DeferredRecipients" attribute that records Bob's email address, so
    rt-email-digest can include the content of the transaction.
    
    Because the transaction doesn't have a normal textual part, the
    txn->ContentObj returns nothing and previously this could cause errors
    like:
    
        Can't call method "Content" on an undefined value at sbin/rt-email-digest line 334
    
    This commit fixes the issue by checking if ContentObj exists before
    calling Content, and also getting the textual content from the
    application/x-rt-original-message part when possible.

diff --git a/sbin/rt-email-digest.in b/sbin/rt-email-digest.in
index 0bc68d7e04..eff49a1e41 100644
--- a/sbin/rt-email-digest.in
+++ b/sbin/rt-email-digest.in
@@ -331,8 +331,44 @@ sub build_digest_for_user {
                         @{ [ localtime( $date_obj->Unix ) ] } );
                 }
                 $contents_body .= "Date: $date\n\n";
-                $contents_body .= $tkt_txns->{$txn}->ContentObj->Content . "\n";
-                $contents_body .= "-------\n";
+                if ( my $content_obj = $tkt_txns->{$txn}->ContentObj ) {
+                    $contents_body .= $content_obj->Content;
+                }
+                else {
+
+                    # Outgoing encrypted email's structure stored in RT
+                    # is like:
+                    #
+                    #   multipart/mixed
+                    #     application/x-pkcs7-mime
+                    #     application/x-rt-original-message
+                    #
+                    # As there is no textual part we can directly use,
+                    # we need to find it from x-rt-original-message.
+                    # Luckily x-rt-original-message is generated by RT,
+                    # so it's safe to assume it always contains
+                    # "text/plain" part.
+
+                    my $all_parts = $tkt_txns->{$txn}->Attachments;
+                    while ( my $part = $all_parts->Next ) {
+                        if ( $part->ContentType eq 'application/x-rt-original-message' ) {
+                            my $parser = RT::EmailParser->new;
+                            my $mime   = $parser->ParseMIMEEntityFromScalar( $part->Content );
+                            foreach my $part ( grep $_->mime_type eq 'text/plain', $mime->parts_DFS ) {
+                                my $encoding;
+                                if ( $part->head->get('Content-Type') =~ /charset="(.+)"/ ) {
+                                    $encoding = $1;
+                                }
+                                else {
+                                    $encoding = 'UTF-8';
+                                }
+                                $contents_body .= Encode::decode( $encoding, $part->bodyhandle->as_string );
+                            }
+                        }
+                    }
+                }
+
+                $contents_body .= "\n-------\n";
             }    # foreach transaction
         }    # foreach ticket
     }    # foreach queue

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


More information about the rt-commit mailing list