Merge commit 'kc/master'
[koha.git] / C4 / Letters.pm
index cf3e215..3f634e1 100644 (file)
@@ -13,9 +13,9 @@ package C4::Letters;
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 use strict;
 use warnings;
@@ -154,7 +154,7 @@ sub addalert ($$$) {
     parameters :
     - alertid : the alert id
     deletes the alert
-    
+
 =cut
 
 sub delalert ($) {
@@ -552,9 +552,8 @@ sub parseletter {
 
 =head2 EnqueueLetter
 
-=over 4
-
-my $success = EnqueueLetter( { letter => $letter, borrowernumber => '12', message_transport_type => 'email' } )
+  my $success = EnqueueLetter( { letter => $letter, 
+        borrowernumber => '12', message_transport_type => 'email' } )
 
 places a letter in the message_queue database table, which will
 eventually get processed (sent) by the process_message_queue.pl
@@ -562,8 +561,6 @@ cronjob when it calls SendQueuedMessages.
 
 return true on success
 
-=back
-
 =cut
 
 sub EnqueueLetter ($) {
@@ -609,16 +606,12 @@ ENDSQL
 
 =head2 SendQueuedMessages ([$hashref]) 
 
-=over 4
+  my $sent = SendQueuedMessages( { verbose => 1 } );
 
 sends all of the 'pending' items in the message queue.
 
-my $sent = SendQueuedMessages( { verbose => 1 } );
-
 returns number of messages sent.
 
-=back
-
 =cut
 
 sub SendQueuedMessages (;$) {
@@ -645,14 +638,10 @@ sub SendQueuedMessages (;$) {
 
 =head2 GetRSSMessages
 
-=over 4
-
-my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
+  my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
 
 returns a listref of all queued RSS messages for a particular person.
 
-=back
-
 =cut
 
 sub GetRSSMessages {
@@ -669,15 +658,11 @@ sub GetRSSMessages {
 
 =head2 GetPrintMessages
 
-=over 4
-
-my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
+  my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
 
 Returns a arrayref of all queued print messages (optionally, for a particular
 person).
 
-=back
-
 =cut
 
 sub GetPrintMessages {
@@ -689,17 +674,13 @@ sub GetPrintMessages {
 
 =head2 GetQueuedMessages ([$hashref])
 
-=over 4
-
-my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
+  my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
 
 fetches messages out of the message queue.
 
 returns:
 list of hashes, each has represents a message in the message queue.
 
-=back
-
 =cut
 
 sub GetQueuedMessages {
@@ -821,7 +802,14 @@ sub _send_message_by_email ($;$$$) {
                                    status     => 'failed' } );
             return;
         }
-        unless ($to_address = $member->{email}) {   # assigment, not comparison
+        my $which_address = C4::Context->preference('AutoEmailPrimaryAddress');
+        # If the system preference is set to 'first valid' (value == OFF), look up email address
+        if ($which_address eq 'OFF') {
+            $to_address = GetFirstValidEmailAddress( $message->{'borrowernumber'} );
+        } else {
+            $to_address = $member->{$which_address};
+        }
+        unless ($to_address) {  
             # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
             # warning too verbose for this more common case?
             _set_message_status( { message_id => $message->{'message_id'},