$limitString = "LIMIT $offset, $limit";
}
+ $isSMSmode = CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $mailingID, 'sms_provider_id', 'id');
+ $additionalJoin = '';
+ if (!$isSMSmode) {
+ // mailing_recipients added when mailing is submitted in UI by user.
+ // if any email is marked on_hold =1 or contact is deceased after mailing is submitted
+ // then it should be get skipped while preparing event_queue
+ // event_queue list is prepared when mailing job gets started.
+ $additionalJoin = " INNER JOIN civicrm_email e ON (r.email_id = e.id AND e.on_hold = 0 AND e.is_primary = 1)
+ INNER JOIN civicrm_contact c on (c.id = r.contact_id AND c.is_deceased <> 1 AND c.do_not_email = 0 AND c.is_opt_out = 0)
+";
+ }
+ else {
+ $additionalJoin = "INNER JOIN civicrm_contact c on (c.id = r.contact_id AND c.is_deceased <> 1 AND c.do_not_sms = 0 AND c.is_opt_out = 0)";
+ }
+
$sql = "
-SELECT contact_id, email_id, phone_id
-FROM civicrm_mailing_recipients
-WHERE mailing_id = %1
+SELECT r.contact_id, r.email_id, r.phone_id
+FROM civicrm_mailing_recipients r
+{$additionalJoin}
+WHERE r.mailing_id = %1
$limitString
";
$params = [1 => [$mailingID, 'Integer']];
$this->_mut->assertRecipients($this->getRecipients(1, 2));
}
+ /**
+ * Test what happens when a contact is set to decesaed
+ */
+ public function testDecesasedRecepient() {
+ $contactID = $this->individualCreate(['first_name' => 'test dead recipeint', 'email' => 'mailtestdead@civicrm.org']);
+ $this->callAPISuccess('group_contact', 'create', [
+ 'contact_id' => $contactID,
+ 'group_id' => $this->_groupID,
+ 'status' => 'Added',
+ ]);
+ $this->createContactsInGroup(2, $this->_groupID);
+ Civi::settings()->add([
+ 'mailerBatchLimit' => 2,
+ ]);
+ $mailing = $this->callAPISuccess('mailing', 'create', $this->_params);
+ $this->assertEquals(3, $this->callAPISuccess('MailingRecipients', 'get', ['mailing_id' => $mailing['id']])['count']);
+ $this->_mut->assertRecipients([]);
+ $this->callAPISuccess('Contact', 'create', ['id' => $contactID, 'is_deceased' => 1, 'contact_type' => 'Individual']);
+ $this->callAPISuccess('job', 'process_mailing', []);
+ // Check that the deceased contact is not found in the mailing.
+ $this->_mut->assertRecipients($this->getRecipients(1, 2));
+
+ }
+
/**
* Test pause and resume on Mailing.
*/