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

(-)a/C4/Letters.pm (-9 / +18 lines)
Lines 826-832 sub GetQueuedMessages { Link Here
826
826
827
    my $dbh = C4::Context->dbh();
827
    my $dbh = C4::Context->dbh();
828
    my $statement = << 'ENDSQL';
828
    my $statement = << 'ENDSQL';
829
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued
829
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued, sentto_address
830
FROM message_queue
830
FROM message_queue
831
ENDSQL
831
ENDSQL
832
832
Lines 937-953 sub _send_message_by_email { Link Here
937
937
938
    my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
938
    my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
939
    my $to_address = $message->{'to_address'};
939
    my $to_address = $message->{'to_address'};
940
    if (!$to_address && $member) {
941
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
942
    }
940
    my $sendAllEmailsTo = C4::Context->preference('SendAllEmailsTo');
943
    my $sendAllEmailsTo = C4::Context->preference('SendAllEmailsTo');
941
    $to_address = $sendAllEmailsTo if ($sendAllEmailsTo && $sendAllEmailsTo =~ /@/ ); # some validation.  This could be improved.
944
    $to_address = $sendAllEmailsTo if ($sendAllEmailsTo && $sendAllEmailsTo =~ /@/ ); # some validation.  This could be improved.
942
    unless ($to_address) {
945
    unless ($to_address) {
943
        unless ($member) {
946
        if (!$member) {
944
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
947
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
945
            _set_message_status( { message_id => $message->{'message_id'},
948
            _set_message_status( { message_id => $message->{'message_id'},
946
                                   status     => 'failed' } );
949
                                   status     => 'failed' } );
947
            return;
950
            return;
948
        }
951
        }
949
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
952
        else {
950
        unless ($to_address) {  
951
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
953
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
952
            # warning too verbose for this more common case?
954
            # warning too verbose for this more common case?
953
            _set_message_status( { message_id => $message->{'message_id'},
955
            _set_message_status( { message_id => $message->{'message_id'},
Lines 980-987 sub _send_message_by_email { Link Here
980
982
981
    _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
983
    _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
982
    if ( sendmail( %sendmail_params ) ) {
984
    if ( sendmail( %sendmail_params ) ) {
983
        _set_message_status( { message_id => $message->{'message_id'},
985
        _set_message_status( { message_id     => $message->{'message_id'},
984
                status     => 'sent' } );
986
                               status         => 'sent',
987
                               sentto_address => $to_address } );
985
        return 1;
988
        return 1;
986
    } else {
989
    } else {
987
        _set_message_status( { message_id => $message->{'message_id'},
990
        _set_message_status( { message_id => $message->{'message_id'},
Lines 1043-1053 sub _set_message_status { Link Here
1043
        return unless exists $params->{ $required_parameter };
1046
        return unless exists $params->{ $required_parameter };
1044
    }
1047
    }
1045
1048
1046
    my $dbh = C4::Context->dbh();
1047
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1049
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1050
    my @values;
1051
    push @values,$params->{'status'};
1052
    if (exists($params->{'sentto_address'})) {
1053
        $statement = 'UPDATE message_queue SET status=?, sentto_address=? WHERE message_id = ?';
1054
        push @values,$params->{'sentto_address'};
1055
    }
1056
    push @values,$params->{'message_id'};
1057
    my $dbh = C4::Context->dbh();
1048
    my $sth = $dbh->prepare( $statement );
1058
    my $sth = $dbh->prepare( $statement );
1049
    my $result = $sth->execute( $params->{'status'},
1059
    my $result = $sth->execute( @values );
1050
                                $params->{'message_id'} );
1051
    return $result;
1060
    return $result;
1052
}
1061
}
1053
1062
(-)a/t/db_dependent/Letters.t (-3 / +30 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 8;
23
23
24
use C4::Context;
24
use C4::Context;
25
use C4::Letters;
25
use C4::Letters;
Lines 55-61 my $message_id = C4::Letters::EnqueueLetter({ Link Here
55
    },
55
    },
56
});
56
});
57
57
58
# This is will force C4::Context->preference() to read the DB.
59
C4::Context->disable_syspref_cache();
60
58
# used to trigger _send_message_by_email in C4::Letter
61
# used to trigger _send_message_by_email in C4::Letter
62
# first override.
63
$dbh->do("UPDATE systempreferences SET value='override\@example.com' WHERE variable='SendAllEmailsTo';");
59
my $message_id2 = C4::Letters::EnqueueLetter({
64
my $message_id2 = C4::Letters::EnqueueLetter({
60
    borrowernumber         => $borrowernumber,
65
    borrowernumber         => $borrowernumber,
61
    message_transport_type => 'email',
66
    message_transport_type => 'email',
Lines 76-83 ok(defined $message_id2 && $message_id2 > 0, 'new message successfully queued'); Link Here
76
my $messages_processed = C4::Letters::SendQueuedMessages();
81
my $messages_processed = C4::Letters::SendQueuedMessages();
77
is($messages_processed, 2, 'all queued messages processed');
82
is($messages_processed, 2, 'all queued messages processed');
78
83
84
# used to trigger _send_message_by_email in C4::Letter
85
# second no override.
86
$dbh->do("UPDATE systempreferences SET value='' WHERE variable='SendAllEmailsTo';");
87
my $message_id3 = C4::Letters::EnqueueLetter({
88
    borrowernumber         => $borrowernumber,
89
    message_transport_type => 'email',
90
    to_address             => 'to@example.com',
91
    from_address           => 'from@example.com',
92
    letter => {
93
        content      => 'a message',
94
        title        => 'message title',
95
        metadata     => 'metadata',
96
        code         => 'TEST_MESSAGE',
97
        content_type => 'text/plain',
98
    },
99
});
100
101
$messages_processed = C4::Letters::SendQueuedMessages();
102
is($messages_processed, 1, 'third queued message processed');
103
79
my $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
104
my $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
80
is(scalar(@$messages), 2, 'two messages stored for the borrower');
105
is(scalar(@$messages), 3, 'three messages stored for the borrower');
81
106
82
is(
107
is(
83
    $messages->[0]->{status},
108
    $messages->[0]->{status},
Lines 85-88 is( Link Here
85
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
110
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
86
);
111
);
87
112
113
is ( $messages->[1]->{sentto_address},'override@example.com','Second message sent to override.');
114
is ( $messages->[2]->{sentto_address},'to@example.com','Third message sent to default.');
115
88
$dbh->rollback;
116
$dbh->rollback;
89
- 

Return to bug 8000