From 6ba2b9c87c9e4c74afe7a21e511893610455a861 Mon Sep 17 00:00:00 2001 From: Ian Walls Date: Mon, 14 Jun 2010 14:26:09 +0000 Subject: [PATCH] Fixes bug 3619: _send_message_by_email not respecting AutoEmailPrimaryAddress = 'OFF' Signed-off-by: Galen Charlton --- C4/Letters.pm | 7 ++++++- C4/Members.pm | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 47abf6c32f..3f634e1090 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -803,7 +803,12 @@ sub _send_message_by_email ($;$$$) { return; } my $which_address = C4::Context->preference('AutoEmailPrimaryAddress'); - $to_address = $member->{$which_address}; + # 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? diff --git a/C4/Members.pm b/C4/Members.pm index eba71f5361..cddbf414d0 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -56,6 +56,8 @@ BEGIN { &getzipnamecity &getidcity + &GetFirstValidEmailAddress + &GetAge &GetCities &GetRoadTypes @@ -1247,6 +1249,33 @@ sub getidcity { return $data; } +=head2 GetFirstValidEmailAddress + + $email = GetFirstValidEmailAddress($borrowernumber); + +Return the first valid email address for a borrower, given the borrowernumber. For now, the order +is defined as email, emailpro, B_email. Returns the empty string if the borrower has no email +addresses. + +=cut + +sub GetFirstValidEmailAddress { + my $borrowernumber = shift; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( "SELECT email, emailpro, B_email FROM borrowers where borrowernumber = ? "); + $sth->execute( $borrowernumber ); + my $data = $sth->fetchrow_hashref; + + if ($data->{'email'}) { + return $data->{'email'}; + } elsif ($data->{'emailpro'}) { + return $data->{'emailpro'}; + } elsif ($data->{'B_email'}) { + return $data->{'B_email'}; + } else { + return ''; + } +} =head2 GetExpiryDate -- 2.39.5