3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
11 use Civi\Api4\ActivityContact
;
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 require_once 'Mail.php';
22 * Class CRM_Mailing_BAO_MailingJob
24 class CRM_Mailing_BAO_MailingJob
extends CRM_Mailing_DAO_MailingJob
{
25 const MAX_CONTACTS_TO_PROCESS
= 1000;
28 * (Dear God Why) Keep a global count of mails processed within the current
33 public static $mailsProcessed = 0;
38 * @param array $params
40 * @return \CRM_Mailing_BAO_MailingJob
41 * @throws \CRM_Core_Exception
43 public static function create($params) {
44 if (empty($params['id']) && empty($params['mailing_id'])) {
45 throw new CRM_Core_Exception("Failed to create job: Unknown mailing ID");
47 $op = empty($params['id']) ?
'create' : 'edit';
48 CRM_Utils_Hook
::pre($op, 'MailingJob', CRM_Utils_Array
::value('id', $params), $params);
50 $jobDAO = new CRM_Mailing_BAO_MailingJob();
51 $jobDAO->copyValues($params);
53 if (!empty($params['mailing_id']) && empty('is_calling_function_updated_to_reflect_deprecation')) {
54 CRM_Mailing_BAO_Mailing
::getRecipients($params['mailing_id']);
56 CRM_Utils_Hook
::post($op, 'MailingJob', $jobDAO->id
, $jobDAO);
61 * Initiate all pending/ready jobs.
63 * @param array $testParams
68 public static function runJobs($testParams = NULL, $mode = NULL) {
69 $job = new CRM_Mailing_BAO_MailingJob();
71 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
72 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
73 $mailerBatchLimit = Civi
::settings()->get('mailerBatchLimit');
75 if (!empty($testParams)) {
79 WHERE id = {$testParams['job_id']}";
83 $currentTime = date('YmdHis');
84 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
85 $domainID = CRM_Core_Config
::domainID();
87 $modeClause = 'AND m.sms_provider_id IS NULL';
89 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
92 // Select the first child job that is scheduled
98 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
101 AND ( ( j.start_date IS null
102 AND j.scheduled_date <= $currentTime
103 AND j.status = 'Scheduled' )
104 OR ( j.status = 'Running'
105 AND j.end_date IS null ) )
106 AND (j.job_type = 'child')
108 ORDER BY j.scheduled_date ASC,
115 while ($job->fetch()) {
116 // still use job level lock for each child job
117 $lock = Civi
::lockManager()->acquire("data.mailing.job.{$job->id}");
118 if (!$lock->isAcquired()) {
122 // for test jobs we do not change anything, since its on a short-circuit path
123 if (empty($testParams)) {
124 // we've got the lock, but while we were waiting and processing
125 // other emails, this job might have changed under us
126 // lets get the job status again and check
127 $job->status
= CRM_Core_DAO
::getFieldValue(
128 'CRM_Mailing_DAO_MailingJob',
136 $job->status
!= 'Running' &&
137 $job->status
!= 'Scheduled'
139 // this includes Cancelled and other statuses, CRM-4246
145 /* Queue up recipients for the child job being launched */
147 if ($job->status
!= 'Running') {
148 $transaction = new CRM_Core_Transaction();
150 // have to queue it up based on the offset and limits
151 // get the parent ID, and limit and offset
152 $job->queue($testParams);
154 // Update to show job has started.
157 'start_date' => date('YmdHis'),
158 'status' => 'Running',
161 $transaction->commit();
165 if ($mode === NULL) {
166 $mailer = \Civi
::service('pear_mail');
168 elseif ($mode == 'sms') {
169 $mailer = CRM_SMS_Provider
::singleton(['mailing_id' => $job->mailing_id
]);
172 // Compose and deliver each child job
173 if (\CRM_Utils_Constant
::value('CIVICRM_FLEXMAILER_HACK_DELIVER')) {
174 $isComplete = Civi\Core\Resolver
::singleton()->call(CIVICRM_FLEXMAILER_HACK_DELIVER
, [$job, $mailer, $testParams]);
177 $isComplete = $job->deliver($mailer, $testParams);
180 CRM_Utils_Hook
::post('create', 'CRM_Mailing_DAO_Spool', $job->id
, $isComplete);
182 // Mark the child complete
186 $transaction = new CRM_Core_Transaction();
187 self
::create(['id' => $job->id
, 'end_date' => date('YmdHis'), 'status' => 'Complete']);
188 $transaction->commit();
190 // don't mark the mailing as complete
193 // Release the child joblock
200 // CRM-17629: Stop processing jobs if mailer batch limit reached
201 if ($mailerBatchLimit > 0 && self
::$mailsProcessed >= $mailerBatchLimit) {
209 * Post process to determine if the parent job
210 * as well as the mailing is complete after the run.
213 public static function runJobs_post($mode = NULL) {
215 $job = new CRM_Mailing_BAO_MailingJob();
217 $mailing = new CRM_Mailing_BAO_Mailing();
219 $config = CRM_Core_Config
::singleton();
220 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
221 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
223 $currentTime = date('YmdHis');
224 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
225 $domainID = CRM_Core_Config
::domainID();
231 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
233 AND j.scheduled_date <= $currentTime
234 AND j.status = 'Running'
235 AND j.end_date IS null
236 AND (j.job_type != 'child' OR j.job_type is NULL)
237 ORDER BY j.scheduled_date,
242 // For each parent job that is running, let's look at their child jobs
243 while ($job->fetch()) {
245 $child_job = new CRM_Mailing_BAO_MailingJob();
249 FROM civicrm_mailing_job j, civicrm_mailing m
250 WHERE m.id = j.mailing_id
251 AND j.job_type = 'child'
253 AND j.status <> 'Complete'";
254 $params = [1 => [$job->id
, 'Integer']];
256 $anyChildLeft = CRM_Core_DAO
::singleValueQuery($child_job_sql, $params);
258 // all of the child jobs are complete, update
259 // the parent job as well as the mailing status
260 if (!$anyChildLeft) {
262 $transaction = new CRM_Core_Transaction();
264 $saveJob = new CRM_Mailing_DAO_MailingJob();
265 $saveJob->id
= $job->id
;
266 $saveJob->end_date
= date('YmdHis');
267 $saveJob->status
= 'Complete';
271 $mailing->id
= $job->mailing_id
;
272 $mailing->is_completed
= TRUE;
274 $transaction->commit();
277 CRM_Utils_Hook
::postMailing($job->mailing_id
);
283 * before we run jobs, we need to split the jobs
287 public static function runJobs_pre($offset = 200, $mode = NULL) {
288 $job = new CRM_Mailing_BAO_MailingJob();
290 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
291 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
293 $currentTime = date('YmdHis');
294 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
296 $workflowClause = CRM_Mailing_BAO_MailingJob
::workflowClause();
298 $domainID = CRM_Core_Config
::domainID();
300 $modeClause = 'AND m.sms_provider_id IS NULL';
301 if ($mode == 'sms') {
302 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
305 // Select all the mailing jobs that are created from
306 // when the mailing is submitted or scheduled.
311 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
315 AND ( ( j.start_date IS null
316 AND j.scheduled_date <= $currentTime
317 AND j.status = 'Scheduled'
318 AND j.end_date IS null ) )
319 AND ((j.job_type is NULL) OR (j.job_type <> 'child'))
320 ORDER BY j.scheduled_date,
325 // For each of the "Parent Jobs" we find, we split them into
326 // X Number of child jobs
327 while ($job->fetch()) {
328 // still use job level lock for each child job
329 $lock = Civi
::lockManager()->acquire("data.mailing.job.{$job->id}");
330 if (!$lock->isAcquired()) {
334 // Re-fetch the job status in case things
335 // changed between the first query and now
336 // to avoid race conditions
337 $job->status
= CRM_Core_DAO
::getFieldValue(
338 'CRM_Mailing_DAO_MailingJob',
344 if ($job->status
!= 'Scheduled') {
349 $transaction = new CRM_Core_Transaction();
351 $job->split_job($offset);
353 // Update the status of the parent job
354 self
::create(['id' => $job->id
, 'start_date' => date('YmdHis'), 'status' => 'Running']);
355 $transaction->commit();
357 // Release the job lock
363 * Split the parent job into n number of child job based on an offset.
364 * If null or 0 , we create only one child job
367 public function split_job($offset = 200) {
368 $recipient_count = CRM_Mailing_BAO_Recipients
::mailingSize($this->mailing_id
);
370 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
372 $dao = new CRM_Core_DAO();
375 INSERT INTO civicrm_mailing_job
376 (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`)
377 VALUES (%1, %2, %3, %4, %5, %6, %7)
380 1 => [$this->mailing_id
, 'Integer'],
381 2 => [$this->scheduled_date
, 'String'],
382 3 => ['Scheduled', 'String'],
383 4 => ['child', 'String'],
384 5 => [$this->id
, 'Integer'],
386 7 => [$recipient_count, 'Integer'],
389 // create one child job if the mailing size is less than the offset
390 // probably use a CRM_Mailing_DAO_MailingJob( );
391 if (empty($offset) ||
392 $recipient_count <= $offset
394 CRM_Core_DAO
::executeQuery($sql, $params);
397 // Creating 'child jobs'
398 $scheduled_unixtime = strtotime($this->scheduled_date
);
399 for ($i = 0, $s = 0; $i < $recipient_count; $i = $i +
$offset, $s++
) {
400 $params[2][0] = date('Y-m-d H:i:s', $scheduled_unixtime +
$s);
402 $params[7][0] = $offset;
403 CRM_Core_DAO
::executeQuery($sql, $params);
410 * @param array $testParams
412 public function queue($testParams = NULL) {
413 $mailing = new CRM_Mailing_BAO_Mailing();
414 $mailing->id
= $this->mailing_id
;
415 if (!empty($testParams)) {
416 $mailing->getTestRecipients($testParams);
419 // We are still getting all the recipients from the parent job
420 // so we don't mess with the include/exclude logic.
421 $recipients = CRM_Mailing_BAO_Recipients
::mailingQuery($this->mailing_id
, $this->job_offset
, $this->job_limit
);
423 // FIXME: this is not very smart, we should move this to one DB call
424 // INSERT INTO ... SELECT FROM ..
425 // the thing we need to figure out is how to generate the hash automatically
429 // dev/core#1768 Get the mail sync interval.
430 $mail_sync_interval = Civi
::settings()->get('civimail_sync_interval');
431 while ($recipients->fetch()) {
432 // CRM-18543: there are situations when both the email and phone are null.
433 // Skip the recipient in this case.
434 if (empty($recipients->email_id
) && empty($recipients->phone_id
)) {
438 if ($recipients->phone_id
) {
439 $recipients->email_id
= "null";
442 $recipients->phone_id
= "null";
447 $recipients->email_id
,
448 $recipients->contact_id
,
449 $recipients->phone_id
,
452 // dev/core#1768 Mail sync interval is now configurable.
453 if ($count %
$mail_sync_interval == 0) {
454 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
460 if (!empty($params)) {
461 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
470 * This is used by CiviMail but will be made redundant by FlexMailer.
471 * @param object $mailer
472 * A Mail object to send the messages.
474 * @param array $testParams
477 public function deliver(&$mailer, $testParams = NULL) {
478 if (\Civi
::settings()->get('experimentalFlexMailerEngine')) {
479 throw new \
RuntimeException("Cannot use legacy deliver() when experimentalFlexMailerEngine is enabled");
482 $mailing = new CRM_Mailing_BAO_Mailing();
483 $mailing->id
= $this->mailing_id
;
484 $mailing->find(TRUE);
488 if ($config == NULL) {
489 $config = CRM_Core_Config
::singleton();
492 if (property_exists($mailing, 'language') && $mailing->language
&& $mailing->language
!= CRM_Core_I18n
::getLocale()) {
493 $swapLang = CRM_Utils_AutoClean
::swap('global://dbLocale?getter', 'call://i18n/setLocale', $mailing->language
);
496 $job_date = $this->scheduled_date
;
499 if (!empty($testParams)) {
500 $mailing->subject
= ts('[CiviMail Draft]') . ' ' . $mailing->subject
;
503 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
505 // get and format attachments
506 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
508 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
509 CRM_Core_Smarty
::registerStringResource();
513 // This handles the edge case scenario where all the mails
514 // have been delivered in prior jobs.
517 // make sure that there's no more than $mailerBatchLimit mails processed in a run
518 $mailerBatchLimit = Civi
::settings()->get('mailerBatchLimit');
519 $eq = self
::findPendingTasks($this->id
, $mailing->sms_provider_id ?
'sms' : 'email');
520 while ($eq->fetch()) {
521 if ($mailerBatchLimit > 0 && self
::$mailsProcessed >= $mailerBatchLimit) {
522 if (!empty($fields)) {
523 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
527 self
::$mailsProcessed++
;
532 'contact_id' => $eq->contact_id
,
533 'email' => $eq->email
,
534 'phone' => $eq->phone
,
536 if (count($fields) == self
::MAX_CONTACTS_TO_PROCESS
) {
537 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
545 if (!empty($fields)) {
546 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
553 * This is used by CiviMail but will be made redundant by FlexMailer.
554 * @param array $fields
555 * List of intended recipients.
556 * Each recipient is an array with keys 'hash', 'contact_id', 'email', etc.
560 * @param $attachments
565 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
566 static $smtpConnectionErrors = 0;
568 if (!is_object($mailer) ||
empty($fields)) {
569 throw new CRM_Core_Exception('Either mailer is not an object or we don\'t have recipients to send to in this group');
572 // get the return properties
573 $returnProperties = $mailing->getReturnProperties();
574 $params = $targetParams = $deliveredParams = [];
576 // dev/core#1768 Get the mail sync interval.
577 $mail_sync_interval = Civi
::settings()->get('civimail_sync_interval');
580 // CRM-15702: Sending bulk sms to contacts without e-mail address fails.
581 // Solution is to skip checking for on hold
582 //do include a statement to check wether e-mail address is on hold
584 if ($mailing->sms_provider_id
) {
585 //do not include a statement to check wether e-mail address is on hold
589 foreach ($fields as $key => $field) {
590 $params[] = $field['contact_id'];
593 [$details] = CRM_Utils_Token
::getTokenDetails(
596 $skipOnHold, TRUE, NULL,
597 $mailing->getFlattenedTokens(),
602 foreach ($fields as $key => $field) {
603 $contactID = $field['contact_id'];
604 if (!array_key_exists($contactID, $details)) {
605 $details[$contactID] = [];
608 // Compose the mailing.
609 $recipient = $replyToEmail = NULL;
610 $replyValue = strcmp($mailing->replyto_email
, $mailing->from_email
);
612 $replyToEmail = $mailing->replyto_email
;
615 $message = $mailing->compose(
616 $this->id
, $field['id'], $field['hash'],
617 $field['contact_id'], $field['email'],
618 $recipient, FALSE, $details[$contactID], $attachments,
619 FALSE, NULL, $replyToEmail
621 if (empty($message)) {
622 // lets keep the message in the queue
623 // most likely a permissions related issue with smarty templates
624 // or a bad contact id? CRM-9833
630 $body = $message->get();
631 $headers = $message->headers();
633 if ($mailing->sms_provider_id
) {
634 $provider = CRM_SMS_Provider
::singleton(['mailing_id' => $mailing->id
]);
635 $body = $provider->getMessage($message, $field['contact_id'], $details[$contactID]);
636 $headers = $provider->getRecipientDetails($field, $details[$contactID]);
639 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
640 $recipient = $headers['To'];
643 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
645 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
648 $result = $mailer->send($recipient, $headers, $body, $this->id
);
654 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id
) {
656 $message = $result->getMessage();
657 if ($this->isTemporaryError($message)) {
658 // lets log this message and code
659 $code = $result->getCode();
660 CRM_Core_Error
::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code");
662 // these are socket write errors which most likely means smtp connection errors
663 // lets skip them and reconnect.
664 $smtpConnectionErrors++
;
665 if ($smtpConnectionErrors <= 5) {
666 $mailer->disconnect();
671 // seems like we have too many of them in a row, we should
672 // write stuff to disk and abort the cron job
680 CRM_Core_Error
::debug_log_message("Too many SMTP Socket Errors. Exiting");
681 CRM_Utils_System
::civiExit();
684 // Register the bounce event.
687 'event_queue_id' => $field['id'],
688 'job_id' => $this->id
,
689 'hash' => $field['hash'],
691 $params = array_merge($params,
692 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
694 CRM_Mailing_Event_BAO_Bounce
::create($params);
696 elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id
) {
697 // Handle SMS errors: CRM-15426
698 $job_id = intval($this->id
);
699 $mailing_id = intval($mailing->id
);
700 CRM_Core_Error
::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows.");
701 CRM_Core_Error
::debug_log_message($result->getMessage());
704 // Register the delivery event.
705 $deliveredParams[] = $field['id'];
706 $targetParams[] = $field['contact_id'];
709 // dev/core#1768 Mail sync interval is now configurable.
710 if ($count %
$mail_sync_interval == 0) {
719 // hack to stop mailing job at run time, CRM-4246.
720 // to avoid making too many DB calls for this rare case
721 // lets do it when we snapshot
722 $status = CRM_Core_DAO
::getFieldValue(
723 'CRM_Mailing_DAO_MailingJob',
730 if ($status != 'Running') {
738 // seems like a successful delivery or bounce, lets decrement error count
739 // only if we have smtp connection errors
740 if ($smtpConnectionErrors > 0) {
741 $smtpConnectionErrors--;
744 // If we have enabled the Throttle option, this is the time to enforce it.
745 $mailThrottleTime = Civi
::settings()->get('mailThrottleTime');
746 if (!empty($mailThrottleTime)) {
747 usleep((int ) $mailThrottleTime);
751 $result = $this->writeToDB(
766 * Determine if an SMTP error is temporary or permanent.
768 * @param string $message
769 * PEAR error message.
771 * TRUE - Temporary/retriable error
772 * FALSE - Permanent/non-retriable error
774 protected function isTemporaryError($message) {
775 // SMTP response code is buried in the message.
776 $code = preg_match('/ \(code: (.+), response: /', $message, $matches) ?
$matches[1] : '';
778 if (strpos($message, 'Failed to write to socket') !== FALSE) {
782 // Register 5xx SMTP response code (permanent failure) as bounce.
783 if (isset($code[0]) && $code[0] === '5') {
787 if (strpos($message, 'Failed to set sender') !== FALSE) {
791 if (strpos($message, 'Failed to add recipient') !== FALSE) {
795 if (strpos($message, 'Failed to send data') !== FALSE) {
805 * @param int $mailingId
806 * The id of the mailing to be canceled.
808 public static function cancel($mailingId) {
811 FROM civicrm_mailing_job
812 WHERE mailing_id = %1
814 AND ( ( job_type IS NULL ) OR
815 job_type <> 'child' )
817 $params = [1 => [$mailingId, 'Integer']];
818 $job = CRM_Core_DAO
::executeQuery($sql, $params);
820 in_array($job->status
, ['Scheduled', 'Running', 'Paused'])
823 self
::create(['id' => $job->id
, 'end_date' => date('YmdHis'), 'status' => 'Canceled']);
825 // also cancel all child jobs
827 UPDATE civicrm_mailing_job
828 SET status = 'Canceled',
832 AND job_type = 'child'
833 AND status IN ( 'Scheduled', 'Running', 'Paused' )
836 1 => [$job->id
, 'Integer'],
837 2 => [date('YmdHis'), 'Timestamp'],
839 CRM_Core_DAO
::executeQuery($sql, $params);
846 * @param int $mailingID
847 * The id of the mailing to be paused.
849 public static function pause($mailingID) {
851 UPDATE civicrm_mailing_job
852 SET status = 'Paused'
853 WHERE mailing_id = %1
855 AND status IN ('Scheduled', 'Running')
857 CRM_Core_DAO
::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
863 * @param int $mailingID
864 * The id of the mailing to be resumed.
866 public static function resume($mailingID) {
868 UPDATE civicrm_mailing_job
869 SET status = 'Scheduled'
870 WHERE mailing_id = %1
872 AND start_date IS NULL
873 AND status = 'Paused'
875 CRM_Core_DAO
::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
878 UPDATE civicrm_mailing_job
879 SET status = 'Running'
880 WHERE mailing_id = %1
882 AND start_date IS NOT NULL
883 AND status = 'Paused'
885 CRM_Core_DAO
::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
889 * Return a translated status enum string.
891 * @param string $status
895 * The translated version
897 public static function status($status) {
898 static $translation = NULL;
900 if (empty($translation)) {
902 'Scheduled' => ts('Scheduled'),
903 'Running' => ts('Running'),
904 'Complete' => ts('Complete'),
905 'Paused' => ts('Paused'),
906 'Canceled' => ts('Canceled'),
909 return CRM_Utils_Array
::value($status, $translation, ts('Not scheduled'));
913 * Return a workflow clause for use in SQL queries,
914 * to only process jobs that are approved.
917 * For use in a WHERE clause
919 public static function workflowClause() {
920 // add an additional check and only process
921 // jobs that are approved
922 if (CRM_Mailing_Info
::workflowEnabled()) {
923 $approveOptionID = CRM_Core_PseudoConstant
::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'Approved');
924 if ($approveOptionID) {
925 return " AND m.approval_status_id = $approveOptionID ";
932 * @param array $deliveredParams
933 * @param array $targetParams
938 * @throws CRM_Core_Exception
941 public function writeToDB(
947 static $activityTypeID = NULL;
948 static $writeActivity = NULL;
950 if (!empty($deliveredParams)) {
951 CRM_Mailing_Event_BAO_Delivered
::bulkCreate($deliveredParams);
952 $deliveredParams = [];
955 if ($writeActivity === NULL) {
956 $writeActivity = Civi
::settings()->get('write_activity_record');
959 if (!$writeActivity) {
964 if (!empty($targetParams) && !empty($mailing->scheduled_id
)) {
965 if (!$activityTypeID) {
966 if ($mailing->sms_provider_id
) {
967 $mailing->subject
= $mailing->name
;
968 $activityTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Mass SMS'
972 $activityTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
974 if (!$activityTypeID) {
975 throw new CRM_Core_Exception(ts('No relevant activity type found when recording Mailing Event delivered Activity'));
980 'source_contact_id' => $mailing->scheduled_id
,
981 'activity_type_id' => $activityTypeID,
982 'source_record_id' => $this->mailing_id
,
983 'activity_date_time' => $job_date,
984 'subject' => $mailing->subject
,
985 'status_id' => 'Completed',
986 'campaign_id' => $mailing->campaign_id
,
989 //check whether activity is already created for this mailing.
990 //if yes then create only target contact record.
993 FROM civicrm_activity
994 WHERE civicrm_activity.activity_type_id = %1
995 AND civicrm_activity.source_record_id = %2
999 1 => [$activityTypeID, 'Integer'],
1000 2 => [$this->mailing_id
, 'Integer'],
1002 $activityID = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
1003 $targetRecordID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets');
1005 $activityTargets = [];
1006 foreach ($targetParams as $id) {
1007 $activityTargets[$id] = ['contact_id' => (int) $id];
1010 $activity['id'] = $activityID;
1013 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
1014 // make sure we don't attempt to duplicate the target activity
1015 // @todo - we don't have to do one contact at a time....
1016 foreach ($activityTargets as $key => $target) {
1019 FROM civicrm_activity_contact
1020 WHERE activity_id = $activityID
1021 AND contact_id = {$target['contact_id']}
1022 AND record_type_id = $targetRecordID
1024 if (CRM_Core_DAO
::singleValueQuery($sql)) {
1025 unset($activityTargets[$key]);
1032 $activity = civicrm_api3('Activity', 'create', $activity);
1033 ActivityContact
::save(FALSE)->setRecords($activityTargets)->setDefaults(['activity_id' => $activity['id'], 'record_type_id' => $targetRecordID])->execute();
1035 catch (Exception
$e) {
1046 * Search the mailing-event queue for a list of pending delivery tasks.
1049 * @param string $medium
1050 * Ex: 'email' or 'sms'.
1052 * @return \CRM_Mailing_Event_BAO_Queue
1053 * A query object whose rows provide ('id', 'contact_id', 'hash') and ('email' or 'phone').
1055 public static function findPendingTasks($jobId, $medium) {
1056 $eq = new CRM_Mailing_Event_BAO_Queue();
1057 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
1058 $emailTable = CRM_Core_BAO_Email
::getTableName();
1059 $phoneTable = CRM_Core_BAO_Phone
::getTableName();
1060 $contactTable = CRM_Contact_BAO_Contact
::getTableName();
1061 $deliveredTable = CRM_Mailing_Event_BAO_Delivered
::getTableName();
1062 $bounceTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
1064 $query = " SELECT $queueTable.id,
1065 $emailTable.email as email,
1066 $queueTable.contact_id,
1070 INNER JOIN $emailTable
1071 ON $queueTable.email_id = $emailTable.id
1072 INNER JOIN $contactTable
1073 ON $contactTable.id = $emailTable.contact_id
1074 LEFT JOIN $deliveredTable
1075 ON $queueTable.id = $deliveredTable.event_queue_id
1076 LEFT JOIN $bounceTable
1077 ON $queueTable.id = $bounceTable.event_queue_id
1078 WHERE $queueTable.job_id = " . $jobId . "
1079 AND $deliveredTable.id IS null
1080 AND $bounceTable.id IS null
1081 AND $contactTable.is_opt_out = 0";
1083 if ($medium === 'sms') {
1085 SELECT $queueTable.id,
1086 $phoneTable.phone as phone,
1087 $queueTable.contact_id,
1091 INNER JOIN $phoneTable
1092 ON $queueTable.phone_id = $phoneTable.id
1093 INNER JOIN $contactTable
1094 ON $contactTable.id = $phoneTable.contact_id
1095 LEFT JOIN $deliveredTable
1096 ON $queueTable.id = $deliveredTable.event_queue_id
1097 LEFT JOIN $bounceTable
1098 ON $queueTable.id = $bounceTable.event_queue_id
1099 WHERE $queueTable.job_id = " . $jobId . "
1100 AND $deliveredTable.id IS null
1101 AND $bounceTable.id IS null
1102 AND ( $contactTable.is_opt_out = 0
1103 OR $contactTable.do_not_sms = 0 )";
1110 * Delete the mailing job.
1116 public static function del($id) {
1117 return (bool) self
::deleteRecord(['id' => $id]);