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?
&getzipnamecity
&getidcity
+ &GetFirstValidEmailAddress
+
&GetAge
&GetCities
&GetRoadTypes
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