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

(-)a/C4/Letters.pm (-9 / +18 lines)
Lines 1016-1022 sub GetQueuedMessages { Link Here
1016
1016
1017
    my $dbh = C4::Context->dbh();
1017
    my $dbh = C4::Context->dbh();
1018
    my $statement = << 'ENDSQL';
1018
    my $statement = << 'ENDSQL';
1019
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued
1019
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued, sentto_address
1020
FROM message_queue
1020
FROM message_queue
1021
ENDSQL
1021
ENDSQL
1022
1022
Lines 1145-1161 sub _send_message_by_email { Link Here
1145
1145
1146
    my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1146
    my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1147
    my $to_address = $message->{'to_address'};
1147
    my $to_address = $message->{'to_address'};
1148
    if (!$to_address && $member) {
1149
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
1150
    }
1148
    my $sendAllEmailsTo = C4::Context->preference('SendAllEmailsTo');
1151
    my $sendAllEmailsTo = C4::Context->preference('SendAllEmailsTo');
1149
    $to_address = $sendAllEmailsTo if ($sendAllEmailsTo && $sendAllEmailsTo =~ /@/ ); # some validation.  This could be improved.
1152
    $to_address = $sendAllEmailsTo if ($sendAllEmailsTo && $sendAllEmailsTo =~ /@/ ); # some validation.  This could be improved.
1150
    unless ($to_address) {
1153
    unless ($to_address) {
1151
        unless ($member) {
1154
        if (!$member) {
1152
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1155
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1153
            _set_message_status( { message_id => $message->{'message_id'},
1156
            _set_message_status( { message_id => $message->{'message_id'},
1154
                                   status     => 'failed' } );
1157
                                   status     => 'failed' } );
1155
            return;
1158
            return;
1156
        }
1159
        }
1157
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
1160
        else {
1158
        unless ($to_address) {  
1159
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1161
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1160
            # warning too verbose for this more common case?
1162
            # warning too verbose for this more common case?
1161
            _set_message_status( { message_id => $message->{'message_id'},
1163
            _set_message_status( { message_id => $message->{'message_id'},
Lines 1199-1206 sub _send_message_by_email { Link Here
1199
1201
1200
    _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
1202
    _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
1201
    if ( sendmail( %sendmail_params ) ) {
1203
    if ( sendmail( %sendmail_params ) ) {
1202
        _set_message_status( { message_id => $message->{'message_id'},
1204
        _set_message_status( { message_id     => $message->{'message_id'},
1203
                status     => 'sent' } );
1205
                               status         => 'sent',
1206
                               sentto_address => $to_address } );
1204
        return 1;
1207
        return 1;
1205
    } else {
1208
    } else {
1206
        _set_message_status( { message_id => $message->{'message_id'},
1209
        _set_message_status( { message_id => $message->{'message_id'},
Lines 1284-1294 sub _set_message_status { Link Here
1284
        return unless exists $params->{ $required_parameter };
1287
        return unless exists $params->{ $required_parameter };
1285
    }
1288
    }
1286
1289
1287
    my $dbh = C4::Context->dbh();
1288
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1290
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1291
    my @values;
1292
    push @values,$params->{'status'};
1293
    if (exists($params->{'sentto_address'})) {
1294
        $statement = 'UPDATE message_queue SET status=?, sentto_address=? WHERE message_id = ?';
1295
        push @values,$params->{'sentto_address'};
1296
    }
1297
    push @values,$params->{'message_id'};
1298
    my $dbh = C4::Context->dbh();
1289
    my $sth = $dbh->prepare( $statement );
1299
    my $sth = $dbh->prepare( $statement );
1290
    my $result = $sth->execute( $params->{'status'},
1300
    my $result = $sth->execute( @values );
1291
                                $params->{'message_id'} );
1292
    return $result;
1301
    return $result;
1293
}
1302
}
1294
1303
(-)a/t/db_dependent/Letters.t (-1 / +30 lines)
Lines 107-113 $my_message->{message_transport_type} = 'sms'; Link Here
107
$message_id = C4::Letters::EnqueueLetter($my_message);
107
$message_id = C4::Letters::EnqueueLetter($my_message);
108
ok(defined $message_id && $message_id > 0, 'new message successfully queued');
108
ok(defined $message_id && $message_id > 0, 'new message successfully queued');
109
109
110
# This is will force C4::Context->preference() to read the DB.
111
C4::Context->disable_syspref_cache();
112
110
# used to trigger _send_message_by_email in C4::Letter
113
# used to trigger _send_message_by_email in C4::Letter
114
# first override.
115
$dbh->do("UPDATE systempreferences SET value='override\@example.com' WHERE variable='SendAllEmailsTo';");
111
my $message_id2 = C4::Letters::EnqueueLetter({
116
my $message_id2 = C4::Letters::EnqueueLetter({
112
    borrowernumber         => $borrowernumber,
117
    borrowernumber         => $borrowernumber,
113
    message_transport_type => 'email',
118
    message_transport_type => 'email',
Lines 146-156 is($messages_processed, 2, 'all queued messages processed'); Link Here
146
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
151
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
147
is(scalar(@$messages), 2, 'two messages stored for the borrower');
152
is(scalar(@$messages), 2, 'two messages stored for the borrower');
148
153
154
# used to trigger _send_message_by_email in C4::Letter
155
# second no override.
156
$dbh->do("UPDATE systempreferences SET value='' WHERE variable='SendAllEmailsTo';");
157
my $message_id3 = C4::Letters::EnqueueLetter({
158
    borrowernumber         => $borrowernumber,
159
    message_transport_type => 'email',
160
    to_address             => 'to@example.com',
161
    from_address           => 'from@example.com',
162
    letter => {
163
        content      => 'a message',
164
        title        => 'message title',
165
        metadata     => 'metadata',
166
        code         => 'TEST_MESSAGE',
167
        content_type => 'text/plain',
168
    },
169
});
170
171
$messages_processed = C4::Letters::SendQueuedMessages();
172
is($messages_processed, 1, 'third queued message processed');
173
174
my $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
175
is(scalar(@$messages), 3, 'three messages stored for the borrower');
176
149
is(
177
is(
150
    $messages->[0]->{status},
178
    $messages->[0]->{status},
151
    'failed',
179
    'failed',
152
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
180
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
153
);
181
);
182
is ( $messages->[1]->{sentto_address},'override@example.com','Second message sent to override.');
183
is ( $messages->[2]->{sentto_address},'to@example.com','Third message sent to default.');
154
184
155
# GetLetters
185
# GetLetters
156
my $letters = C4::Letters::GetLetters();
186
my $letters = C4::Letters::GetLetters();
157
- 

Return to bug 8000