View | Details | Raw Unified | Return to bug 29330
Collapse All | Expand All

(-)a/C4/Letters.pm (-53 / +59 lines)
Lines 19-25 package C4::Letters; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use MIME::Lite;
23
use Carp qw( carp croak );
22
use Carp qw( carp croak );
24
use Template;
23
use Template;
25
use Module::Load::Conditional qw( can_load );
24
use Module::Load::Conditional qw( can_load );
Lines 954-961 sub EnqueueLetter { Link Here
954
    if ( $params->{'attachments'} ) {
953
    if ( $params->{'attachments'} ) {
955
        $params->{'letter'} = _add_attachments(
954
        $params->{'letter'} = _add_attachments(
956
            {   letter      => $params->{'letter'},
955
            {   letter      => $params->{'letter'},
957
                attachments => $params->{'attachments'},
956
                attachments => $params->{'attachments'}
958
                message     => MIME::Lite->new( Type => 'multipart/mixed' ),
959
            }
957
            }
960
        );
958
        );
961
    }
959
    }
Lines 1219-1263 sub ResendMessage { Link Here
1219
1217
1220
=head2 _add_attachements
1218
=head2 _add_attachements
1221
1219
1222
  named parameters:
1220
    _add_attachments({ letter => $letter, attachments => $attachments });
1223
  letter - the standard letter hashref
1224
  attachments - listref of attachments. each attachment is a hashref of:
1225
    type - the mime type, like 'text/plain'
1226
    content - the actual attachment
1227
    filename - the name of the attachment.
1228
  message - a MIME::Lite object to attach these to.
1229
1221
1230
  returns your letter object, with the content updated.
1222
Given a I<letter> parameter, this method picks the I<content> of it,
1223
and generates a MIME email with I<content> as the the body.
1224
1225
It then attaches the passed I<attachments> using Koha::Email's I<attach>
1226
method.
1227
1228
It returns the original I<$letter> object, with the content replaced by the
1229
string representation of the MIME object, and the content-ype is updated to
1230
B<MIME> for later handling.
1231
1231
1232
=cut
1232
=cut
1233
1233
1234
sub _add_attachments {
1234
sub _add_attachments {
1235
    my $params = shift;
1235
    my $params = shift;
1236
1236
1237
    my $letter = $params->{'letter'};
1237
    my $letter = $params->{letter};
1238
    my $attachments = $params->{'attachments'};
1238
    my $attachments = $params->{attachments};
1239
    return $letter unless @$attachments;
1239
    return $letter unless @$attachments;
1240
    my $message = $params->{'message'};
1240
1241
1241
    my $message = Koha::Email->new;
1242
    # First, we have to put the body in as the first attachment
1242
1243
    $message->attach(
1243
    if ( $letter->{is_html} ) {
1244
        Type => $letter->{'content-type'} || 'TEXT',
1244
        $message->html_body( _wrap_html( $letter->{content}, $letter->{title} ) );
1245
        Data => $letter->{'is_html'}
1245
    }
1246
            ? _wrap_html($letter->{'content'}, $letter->{'title'})
1246
    else {
1247
            : $letter->{'content'},
1247
        $message->text_body( $letter->{content} );
1248
    );
1248
    }
1249
1249
1250
    foreach my $attachment ( @$attachments ) {
1250
    foreach my $attachment ( @$attachments ) {
1251
        $message->attach(
1251
        $message->attach(
1252
            Type     => $attachment->{'type'},
1252
            Encode::encode( "UTF-8", $attachment->{content} ),
1253
            Data     => $attachment->{'content'},
1253
            content_type => 'application/octet-stream',
1254
            Filename => $attachment->{'filename'},
1254
            name         => $attachment->{filename},
1255
            disposition  => 'attachment',
1255
        );
1256
        );
1256
    }
1257
    }
1257
    # we're forcing list context here to get the header, not the count back from grep.
1258
1258
    ( $letter->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
1259
    $letter->{'content-type'} = 'MIME';
1259
    $letter->{'content-type'} =~ s/^Content-Type:\s+//;
1260
    $letter->{content} = $message->as_string;
1260
    $letter->{'content'} = $message->body_as_string;
1261
1261
1262
    return $letter;
1262
    return $letter;
1263
1263
Lines 1389-1409 sub _send_message_by_email { Link Here
1389
        );
1389
        );
1390
        return;
1390
        return;
1391
    };
1391
    };
1392
    my $email = try {
1392
    my $email;
1393
        Koha::Email->create(
1393
1394
            {
1394
    try {
1395
                to => $to_address,
1395
1396
                (
1396
        my $params = {
1397
                    C4::Context->preference('NoticeBcc')
1397
            to => $to_address,
1398
                    ? ( bcc => C4::Context->preference('NoticeBcc') )
1398
            (
1399
                    : ()
1399
                C4::Context->preference('NoticeBcc')
1400
                ),
1400
                ? ( bcc => C4::Context->preference('NoticeBcc') )
1401
                from     => $from_address,
1401
                : ()
1402
                reply_to => $message->{'reply_address'} || $branch_replyto,
1402
            ),
1403
                sender   => $branch_returnpath,
1403
            from     => $from_address,
1404
                subject  => "" . $message->{subject}
1404
            reply_to => $message->{'reply_address'} || $branch_replyto,
1405
            sender   => $branch_returnpath,
1406
            subject  => "" . $message->{subject}
1407
        };
1408
1409
        if ( $message->{'content_type'} && $message->{'content_type'} eq 'MIME' ) {
1410
1411
            # The message has been previously composed as a valid MIME object
1412
            # and serialized as a string on the DB
1413
            $email = Koha::Email->new_from_string($content);
1414
            $email->create($params);
1415
        } else {
1416
            $email = Koha::Email->create($params);
1417
            if ($is_html) {
1418
                $email->html_body( _wrap_html( $content, $subject ) );
1419
            } else {
1420
                $email->text_body($content);
1405
            }
1421
            }
1406
        );
1422
        }
1407
    }
1423
    }
1408
    catch {
1424
    catch {
1409
        if ( ref($_) eq 'Koha::Exceptions::BadParameter' ) {
1425
        if ( ref($_) eq 'Koha::Exceptions::BadParameter' ) {
Lines 1427-1441 sub _send_message_by_email { Link Here
1427
    };
1443
    };
1428
    return unless $email;
1444
    return unless $email;
1429
1445
1430
    if ( $is_html ) {
1431
        $email->html_body(
1432
            _wrap_html( $content, $subject )
1433
        );
1434
    }
1435
    else {
1436
        $email->text_body( $content );
1437
    }
1438
1439
    my $smtp_server;
1446
    my $smtp_server;
1440
    if ( $library ) {
1447
    if ( $library ) {
1441
        $smtp_server = $library->smtp_server;
1448
        $smtp_server = $library->smtp_server;
1442
- 

Return to bug 29330