Bug 37984 - Binary attachment cannot be sent via C4::Letters::EnqueueLetter()
Summary: Binary attachment cannot be sent via C4::Letters::EnqueueLetter()
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Notices (show other bugs)
Version: 21.11
Hardware: Macintosh Mac OS
: P5 - low new feature
Assignee: Bugs List
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-09-23 11:00 UTC by Petr Svoboda
Modified: 2024-09-23 11:00 UTC (History)
1 user (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Petr Svoboda 2024-09-23 11:00:44 UTC
I use koha testing docker in Mac OS.

This code does not send a valid e-mail binary attachment:

$pdf_binary_content = ...;
my @attachments = (
       {
           filename => 'attachment.pdf',
           type => 'application/pdf',
           content => $pdf_binary_content,
       }
   );
my $message_id = C4::Letters::EnqueueLetter({
       ...
       ...
       ...
       attachments => \@attachments,
   });

because in C4::Letters is in _add_attachments function attachment encoding:
...
Encode::encode( "UTF-8", $attachment->{content} )
...

I can't encode a binary file...



Possible solution (C4::Letters):

sub _add_attachments {
...
    foreach my $attachment ( @$attachments ) {
        $message->attach(

            # ##################################################################
            # Original code: Encode::encode( "UTF-8", $attachment->{content} ),
            # New code:
            defined $attachment->{binary} ? $attachment->{content} : Encode::encode( "UTF-8", $attachment->{content} ),
            # ##################################################################

            content_type => $attachment->{type} || 'application/octet-stream',
            name         => $attachment->{filename},
            disposition  => 'attachment',
        );
    }
...
}

my @attachments = (
        {
            ...
            binary => 1,
        }
    );
my $message_id = C4::Letters::EnqueueLetter({
       ...
       ...
       ...
       attachments => \@attachments,
   });