From 157e2097d385fba6f7d287c0ae3e9d38416b425d Mon Sep 17 00:00:00 2001 From: Andrew Hunt Date: Fri, 8 Nov 2019 17:58:57 -0500 Subject: [PATCH] dev/core#1378 Scheduled reminders: only pull good emails --- CRM/Contact/BAO/Contact.php | 10 +++++++++- CRM/Core/BAO/ActionSchedule.php | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 132be6ceef..5745ffce68 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -2560,11 +2560,14 @@ WHERE civicrm_openid.openid = %1"; * * @param int $contactID * Contact id. + * @param bool $polite + * Whether to only pull an email if it's okay to send to it--that is, if it + * is not on_hold and the contact is not do_not_email. * * @return string * Email address if present else null */ - public static function getPrimaryEmail($contactID) { + public static function getPrimaryEmail($contactID, $polite = FALSE) { // fetch the primary email $query = " SELECT civicrm_email.email as email @@ -2572,6 +2575,11 @@ WHERE civicrm_openid.openid = %1"; LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) WHERE civicrm_email.is_primary = 1 AND civicrm_contact.id = %1"; + if ($polite) { + $query .= ' + AND civicrm_contact.do_not_email = 0 + AND civicrm_email.on_hold = 0'; + } $p = [1 => [$contactID, 'Integer']]; $dao = CRM_Core_DAO::executeQuery($query, $p); diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 01671c3af7..01249ed3b9 100644 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -607,7 +607,7 @@ FROM civicrm_action_schedule cas * List of error messages. */ protected static function sendReminderEmail($tokenRow, $schedule, $toContactID) { - $toEmail = CRM_Contact_BAO_Contact::getPrimaryEmail($toContactID); + $toEmail = CRM_Contact_BAO_Contact::getPrimaryEmail($toContactID, TRUE); if (!$toEmail) { return ["email_missing" => "Couldn't find recipient's email address."]; } -- 2.25.1