Skip to content

Commit 4f86547

Browse files
committed
Set filename of rfc822 attachments when it's absent for Outlook
Outlook on Windows doesn't include filename when attaching .msg files in generated emails, which broke RT's logic to render them as downloadable attachments. This commit fixes it by setting the filename to the subject header of the .msg file plus .eml suffix, which is also consistent with gmail's behavior.
1 parent 4236678 commit 4f86547

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

lib/RT/EmailParser.pm

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,12 @@ in it. it's cool to have a 'text/plain' part, but the problem is the part is
675675
not so right: all the "\n" in your main message will become "\n\n" :/
676676
677677
this method will fix this bug, i.e. replaces "\n\n" to "\n".
678-
return 1 if it does find the problem in the entity and get it fixed.
678+
679+
Outlook on Windows has another weird behavior that it drops the filename
680+
when attaching .msg files. This method sets the absent filename to the
681+
Subject header of attached .msg files with suffix ".eml".
682+
683+
return 1 if it finds some problems above in the entity and get it fixed.
679684
680685
=cut
681686

@@ -686,6 +691,7 @@ sub RescueOutlook {
686691

687692
return unless $mime && $self->LooksLikeMSEmail($mime);
688693

694+
my $changed;
689695
my $text_part;
690696
if ( $mime->head->get('Content-Type') =~ m{multipart/mixed} ) {
691697
my $first = $mime->parts(0);
@@ -727,15 +733,46 @@ sub RescueOutlook {
727733
$io->close;
728734
$RT::Logger->debug(
729735
"Removed extra newlines from MS Outlook message.");
730-
return 1;
736+
$changed = 1;
731737
}
732738
else {
733739
$RT::Logger->error("Can't write to body to fix newlines");
734740
}
735741
}
736742
}
737743

738-
return;
744+
# Fix the absent filename of rfc822 attachments
745+
for my $part ( $mime->parts ) {
746+
my $head = $part->head;
747+
my $type = $head->get('Content-Type') // '';
748+
749+
my $disposition = $head->get('Content-Disposition') // '';
750+
if ( $type =~ m{message/rfc822}
751+
&& $type !~ /name=\S/
752+
&& $disposition =~ /attachment/
753+
&& $disposition !~ /filename=\S/ )
754+
{
755+
my $subject;
756+
for my $line ( @{ $part->body } ) {
757+
if ( $line =~ /^Subject:\s*(.*\S)/ ) {
758+
$subject = $1;
759+
last;
760+
}
761+
}
762+
763+
if ($subject) {
764+
$disposition =~ s!\s+$!!;
765+
$head->replace( 'Content-Disposition', qq{$disposition; filename="$subject.eml"} );
766+
$changed = 1;
767+
}
768+
else {
769+
$RT::Logger->warning("Can't find email subject when replacing missing filename in Outlook attachment");
770+
}
771+
}
772+
773+
}
774+
775+
return $changed;
739776
}
740777

741778
=head1 LooksLikeMSEmail

0 commit comments

Comments
 (0)