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 public function __construct() {
39 parent
::__construct();
45 * @param array $params
47 * @return \CRM_Mailing_BAO_MailingJob
48 * @throws \CRM_Core_Exception
50 public static function create($params) {
51 if (empty($params['id']) && empty($params['mailing_id'])) {
52 throw new CRM_Core_Exception("Failed to create job: Unknown mailing ID");
54 $op = empty($params['id']) ?
'create' : 'edit';
55 CRM_Utils_Hook
::pre($op, 'MailingJob', CRM_Utils_Array
::value('id', $params), $params);
57 $jobDAO = new CRM_Mailing_BAO_MailingJob();
58 $jobDAO->copyValues($params);
60 if (!empty($params['mailing_id']) && empty('is_calling_function_updated_to_reflect_deprecation')) {
61 CRM_Mailing_BAO_Mailing
::getRecipients($params['mailing_id']);
63 CRM_Utils_Hook
::post($op, 'MailingJob', $jobDAO->id
, $jobDAO);
68 * Initiate all pending/ready jobs.
70 * @param array $testParams
75 public static function runJobs($testParams = NULL, $mode = NULL) {
76 $job = new CRM_Mailing_BAO_MailingJob();
78 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
79 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
80 $mailerBatchLimit = Civi
::settings()->get('mailerBatchLimit');
82 if (!empty($testParams)) {
86 WHERE id = {$testParams['job_id']}";
90 $currentTime = date('YmdHis');
91 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
92 $domainID = CRM_Core_Config
::domainID();
94 $modeClause = 'AND m.sms_provider_id IS NULL';
96 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
99 // Select the first child job that is scheduled
105 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
108 AND ( ( j.start_date IS null
109 AND j.scheduled_date <= $currentTime
110 AND j.status = 'Scheduled' )
111 OR ( j.status = 'Running'
112 AND j.end_date IS null ) )
113 AND (j.job_type = 'child')
115 ORDER BY j.scheduled_date ASC,
122 while ($job->fetch()) {
123 // still use job level lock for each child job
124 $lock = Civi
::lockManager()->acquire("data.mailing.job.{$job->id}");
125 if (!$lock->isAcquired()) {
129 // for test jobs we do not change anything, since its on a short-circuit path
130 if (empty($testParams)) {
131 // we've got the lock, but while we were waiting and processing
132 // other emails, this job might have changed under us
133 // lets get the job status again and check
134 $job->status
= CRM_Core_DAO
::getFieldValue(
135 'CRM_Mailing_DAO_MailingJob',
143 $job->status
!= 'Running' &&
144 $job->status
!= 'Scheduled'
146 // this includes Cancelled and other statuses, CRM-4246
152 /* Queue up recipients for the child job being launched */
154 if ($job->status
!= 'Running') {
155 $transaction = new CRM_Core_Transaction();
157 // have to queue it up based on the offset and limits
158 // get the parent ID, and limit and offset
159 $job->queue($testParams);
161 // Update to show job has started.
164 'start_date' => date('YmdHis'),
165 'status' => 'Running',
168 $transaction->commit();
172 if ($mode === NULL) {
173 $mailer = \Civi
::service('pear_mail');
175 elseif ($mode == 'sms') {
176 $mailer = CRM_SMS_Provider
::singleton(['mailing_id' => $job->mailing_id
]);
179 // Compose and deliver each child job
180 if (\CRM_Utils_Constant
::value('CIVICRM_FLEXMAILER_HACK_DELIVER')) {
181 $isComplete = Civi\Core\Resolver
::singleton()->call(CIVICRM_FLEXMAILER_HACK_DELIVER
, [$job, $mailer, $testParams]);
184 $isComplete = $job->deliver($mailer, $testParams);
187 CRM_Utils_Hook
::post('create', 'CRM_Mailing_DAO_Spool', $job->id
, $isComplete);
189 // Mark the child complete
193 $transaction = new CRM_Core_Transaction();
194 self
::create(['id' => $job->id
, 'end_date' => date('YmdHis'), 'status' => 'Complete']);
195 $transaction->commit();
197 // don't mark the mailing as complete
200 // Release the child joblock
207 // CRM-17629: Stop processing jobs if mailer batch limit reached
208 if ($mailerBatchLimit > 0 && self
::$mailsProcessed >= $mailerBatchLimit) {
216 * Post process to determine if the parent job
217 * as well as the mailing is complete after the run.
220 public static function runJobs_post($mode = NULL) {
222 $job = new CRM_Mailing_BAO_MailingJob();
224 $mailing = new CRM_Mailing_BAO_Mailing();
226 $config = CRM_Core_Config
::singleton();
227 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
228 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
230 $currentTime = date('YmdHis');
231 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
232 $domainID = CRM_Core_Config
::domainID();
238 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
240 AND j.scheduled_date <= $currentTime
241 AND j.status = 'Running'
242 AND j.end_date IS null
243 AND (j.job_type != 'child' OR j.job_type is NULL)
244 ORDER BY j.scheduled_date,
249 // For each parent job that is running, let's look at their child jobs
250 while ($job->fetch()) {
252 $child_job = new CRM_Mailing_BAO_MailingJob();
256 FROM civicrm_mailing_job j, civicrm_mailing m
257 WHERE m.id = j.mailing_id
258 AND j.job_type = 'child'
260 AND j.status <> 'Complete'";
261 $params = [1 => [$job->id
, 'Integer']];
263 $anyChildLeft = CRM_Core_DAO
::singleValueQuery($child_job_sql, $params);
265 // all of the child jobs are complete, update
266 // the parent job as well as the mailing status
267 if (!$anyChildLeft) {
269 $transaction = new CRM_Core_Transaction();
271 $saveJob = new CRM_Mailing_DAO_MailingJob();
272 $saveJob->id
= $job->id
;
273 $saveJob->end_date
= date('YmdHis');
274 $saveJob->status
= 'Complete';
278 $mailing->id
= $job->mailing_id
;
279 $mailing->is_completed
= TRUE;
281 $transaction->commit();
284 CRM_Utils_Hook
::postMailing($job->mailing_id
);
290 * before we run jobs, we need to split the jobs
294 public static function runJobs_pre($offset = 200, $mode = NULL) {
295 $job = new CRM_Mailing_BAO_MailingJob();
297 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
298 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
300 $currentTime = date('YmdHis');
301 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL('m');
303 $workflowClause = CRM_Mailing_BAO_MailingJob
::workflowClause();
305 $domainID = CRM_Core_Config
::domainID();
307 $modeClause = 'AND m.sms_provider_id IS NULL';
308 if ($mode == 'sms') {
309 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
312 // Select all the mailing jobs that are created from
313 // when the mailing is submitted or scheduled.
318 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
322 AND ( ( j.start_date IS null
323 AND j.scheduled_date <= $currentTime
324 AND j.status = 'Scheduled'
325 AND j.end_date IS null ) )
326 AND ((j.job_type is NULL) OR (j.job_type <> 'child'))
327 ORDER BY j.scheduled_date,
332 // For each of the "Parent Jobs" we find, we split them into
333 // X Number of child jobs
334 while ($job->fetch()) {
335 // still use job level lock for each child job
336 $lock = Civi
::lockManager()->acquire("data.mailing.job.{$job->id}");
337 if (!$lock->isAcquired()) {
341 // Re-fetch the job status in case things
342 // changed between the first query and now
343 // to avoid race conditions
344 $job->status
= CRM_Core_DAO
::getFieldValue(
345 'CRM_Mailing_DAO_MailingJob',
351 if ($job->status
!= 'Scheduled') {
356 $transaction = new CRM_Core_Transaction();
358 $job->split_job($offset);
360 // Update the status of the parent job
361 self
::create(['id' => $job->id
, 'start_date' => date('YmdHis'), 'status' => 'Running']);
362 $transaction->commit();
364 // Release the job lock
370 * Split the parent job into n number of child job based on an offset.
371 * If null or 0 , we create only one child job
374 public function split_job($offset = 200) {
375 $recipient_count = CRM_Mailing_BAO_Recipients
::mailingSize($this->mailing_id
);
377 $jobTable = CRM_Mailing_DAO_MailingJob
::getTableName();
379 $dao = new CRM_Core_DAO();
382 INSERT INTO civicrm_mailing_job
383 (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`)
384 VALUES (%1, %2, %3, %4, %5, %6, %7)
387 1 => [$this->mailing_id
, 'Integer'],
388 2 => [$this->scheduled_date
, 'String'],
389 3 => ['Scheduled', 'String'],
390 4 => ['child', 'String'],
391 5 => [$this->id
, 'Integer'],
393 7 => [$recipient_count, 'Integer'],
396 // create one child job if the mailing size is less than the offset
397 // probably use a CRM_Mailing_DAO_MailingJob( );
398 if (empty($offset) ||
399 $recipient_count <= $offset
401 CRM_Core_DAO
::executeQuery($sql, $params);
404 // Creating 'child jobs'
405 $scheduled_unixtime = strtotime($this->scheduled_date
);
406 for ($i = 0, $s = 0; $i < $recipient_count; $i = $i +
$offset, $s++
) {
407 $params[2][0] = date('Y-m-d H:i:s', $scheduled_unixtime +
$s);
409 $params[7][0] = $offset;
410 CRM_Core_DAO
::executeQuery($sql, $params);
417 * @param array $testParams
419 public function queue($testParams = NULL) {
420 $mailing = new CRM_Mailing_BAO_Mailing();
421 $mailing->id
= $this->mailing_id
;
422 if (!empty($testParams)) {
423 $mailing->getTestRecipients($testParams);
426 // We are still getting all the recipients from the parent job
427 // so we don't mess with the include/exclude logic.
428 $recipients = CRM_Mailing_BAO_Recipients
::mailingQuery($this->mailing_id
, $this->job_offset
, $this->job_limit
);
430 // FIXME: this is not very smart, we should move this to one DB call
431 // INSERT INTO ... SELECT FROM ..
432 // the thing we need to figure out is how to generate the hash automatically
436 // dev/core#1768 Get the mail sync interval.
437 $mail_sync_interval = Civi
::settings()->get('civimail_sync_interval');
438 while ($recipients->fetch()) {
439 // CRM-18543: there are situations when both the email and phone are null.
440 // Skip the recipient in this case.
441 if (empty($recipients->email_id
) && empty($recipients->phone_id
)) {
445 if ($recipients->phone_id
) {
446 $recipients->email_id
= "null";
449 $recipients->phone_id
= "null";
454 $recipients->email_id
,
455 $recipients->contact_id
,
456 $recipients->phone_id
,
459 // dev/core#1768 Mail sync interval is now configurable.
460 if ($count %
$mail_sync_interval == 0) {
461 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
467 if (!empty($params)) {
468 CRM_Mailing_Event_BAO_Queue
::bulkCreate($params, $now);
477 * This is used by CiviMail but will be made redundant by FlexMailer.
478 * @param object $mailer
479 * A Mail object to send the messages.
481 * @param array $testParams
484 public function deliver(&$mailer, $testParams = NULL) {
485 if (\Civi
::settings()->get('experimentalFlexMailerEngine')) {
486 throw new \
RuntimeException("Cannot use legacy deliver() when experimentalFlexMailerEngine is enabled");
489 $mailing = new CRM_Mailing_BAO_Mailing();
490 $mailing->id
= $this->mailing_id
;
491 $mailing->find(TRUE);
495 if ($config == NULL) {
496 $config = CRM_Core_Config
::singleton();
499 if (property_exists($mailing, 'language') && $mailing->language
&& $mailing->language
!= CRM_Core_I18n
::getLocale()) {
500 $swapLang = CRM_Utils_AutoClean
::swap('global://dbLocale?getter', 'call://i18n/setLocale', $mailing->language
);
503 $job_date = $this->scheduled_date
;
506 if (!empty($testParams)) {
507 $mailing->subject
= ts('[CiviMail Draft]') . ' ' . $mailing->subject
;
510 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
512 // get and format attachments
513 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
515 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
516 CRM_Core_Smarty
::registerStringResource();
520 // This handles the edge case scenario where all the mails
521 // have been delivered in prior jobs.
524 // make sure that there's no more than $mailerBatchLimit mails processed in a run
525 $mailerBatchLimit = Civi
::settings()->get('mailerBatchLimit');
526 $eq = self
::findPendingTasks($this->id
, $mailing->sms_provider_id ?
'sms' : 'email');
527 while ($eq->fetch()) {
528 if ($mailerBatchLimit > 0 && self
::$mailsProcessed >= $mailerBatchLimit) {
529 if (!empty($fields)) {
530 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
534 self
::$mailsProcessed++
;
539 'contact_id' => $eq->contact_id
,
540 'email' => $eq->email
,
541 'phone' => $eq->phone
,
543 if (count($fields) == self
::MAX_CONTACTS_TO_PROCESS
) {
544 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
552 if (!empty($fields)) {
553 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
560 * This is used by CiviMail but will be made redundant by FlexMailer.
561 * @param array $fields
562 * List of intended recipients.
563 * Each recipient is an array with keys 'hash', 'contact_id', 'email', etc.
567 * @param $attachments
572 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
573 static $smtpConnectionErrors = 0;
575 if (!is_object($mailer) ||
empty($fields)) {
576 throw new CRM_Core_Exception('Either mailer is not an object or we don\'t have recipients to send to in this group');
579 // get the return properties
580 $returnProperties = $mailing->getReturnProperties();
581 $params = $targetParams = $deliveredParams = [];
583 // dev/core#1768 Get the mail sync interval.
584 $mail_sync_interval = Civi
::settings()->get('civimail_sync_interval');
587 // CRM-15702: Sending bulk sms to contacts without e-mail address fails.
588 // Solution is to skip checking for on hold
589 //do include a statement to check wether e-mail address is on hold
591 if ($mailing->sms_provider_id
) {
592 //do not include a statement to check wether e-mail address is on hold
596 foreach ($fields as $key => $field) {
597 $params[] = $field['contact_id'];
600 [$details] = CRM_Utils_Token
::getTokenDetails(
603 $skipOnHold, TRUE, NULL,
604 $mailing->getFlattenedTokens(),
609 foreach ($fields as $key => $field) {
610 $contactID = $field['contact_id'];
611 if (!array_key_exists($contactID, $details)) {
612 $details[$contactID] = [];
615 // Compose the mailing.
616 $recipient = $replyToEmail = NULL;
617 $replyValue = strcmp($mailing->replyto_email
, $mailing->from_email
);
619 $replyToEmail = $mailing->replyto_email
;
622 $message = $mailing->compose(
623 $this->id
, $field['id'], $field['hash'],
624 $field['contact_id'], $field['email'],
625 $recipient, FALSE, $details[$contactID], $attachments,
626 FALSE, NULL, $replyToEmail
628 if (empty($message)) {
629 // lets keep the message in the queue
630 // most likely a permissions related issue with smarty templates
631 // or a bad contact id? CRM-9833
637 $body = $message->get();
638 $headers = $message->headers();
640 if ($mailing->sms_provider_id
) {
641 $provider = CRM_SMS_Provider
::singleton(['mailing_id' => $mailing->id
]);
642 $body = $provider->getMessage($message, $field['contact_id'], $details[$contactID]);
643 $headers = $provider->getRecipientDetails($field, $details[$contactID]);
646 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
647 $recipient = $headers['To'];
650 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
652 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
655 $result = $mailer->send($recipient, $headers, $body, $this->id
);
661 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id
) {
663 $message = $result->getMessage();
664 if ($this->isTemporaryError($message)) {
665 // lets log this message and code
666 $code = $result->getCode();
667 CRM_Core_Error
::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code");
669 // these are socket write errors which most likely means smtp connection errors
670 // lets skip them and reconnect.
671 $smtpConnectionErrors++
;
672 if ($smtpConnectionErrors <= 5) {
673 $mailer->disconnect();
678 // seems like we have too many of them in a row, we should
679 // write stuff to disk and abort the cron job
687 CRM_Core_Error
::debug_log_message("Too many SMTP Socket Errors. Exiting");
688 CRM_Utils_System
::civiExit();
691 // Register the bounce event.
694 'event_queue_id' => $field['id'],
695 'job_id' => $this->id
,
696 'hash' => $field['hash'],
698 $params = array_merge($params,
699 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
701 CRM_Mailing_Event_BAO_Bounce
::create($params);
703 elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id
) {
704 // Handle SMS errors: CRM-15426
705 $job_id = intval($this->id
);
706 $mailing_id = intval($mailing->id
);
707 CRM_Core_Error
::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows.");
708 CRM_Core_Error
::debug_log_message($result->getMessage());
711 // Register the delivery event.
712 $deliveredParams[] = $field['id'];
713 $targetParams[] = $field['contact_id'];
716 // dev/core#1768 Mail sync interval is now configurable.
717 if ($count %
$mail_sync_interval == 0) {
726 // hack to stop mailing job at run time, CRM-4246.
727 // to avoid making too many DB calls for this rare case
728 // lets do it when we snapshot
729 $status = CRM_Core_DAO
::getFieldValue(
730 'CRM_Mailing_DAO_MailingJob',
737 if ($status != 'Running') {
745 // seems like a successful delivery or bounce, lets decrement error count
746 // only if we have smtp connection errors
747 if ($smtpConnectionErrors > 0) {
748 $smtpConnectionErrors--;
751 // If we have enabled the Throttle option, this is the time to enforce it.
752 $mailThrottleTime = Civi
::settings()->get('mailThrottleTime');
753 if (!empty($mailThrottleTime)) {
754 usleep((int ) $mailThrottleTime);
758 $result = $this->writeToDB(
773 * Determine if an SMTP error is temporary or permanent.
775 * @param string $message
776 * PEAR error message.
778 * TRUE - Temporary/retriable error
779 * FALSE - Permanent/non-retriable error
781 protected function isTemporaryError($message) {
782 // SMTP response code is buried in the message.
783 $code = preg_match('/ \(code: (.+), response: /', $message, $matches) ?
$matches[1] : '';
785 if (strpos($message, 'Failed to write to socket') !== FALSE) {
789 // Register 5xx SMTP response code (permanent failure) as bounce.
790 if (isset($code[0]) && $code[0] === '5') {
794 if (strpos($message, 'Failed to set sender') !== FALSE) {
798 if (strpos($message, 'Failed to add recipient') !== FALSE) {
802 if (strpos($message, 'Failed to send data') !== FALSE) {
812 * @param int $mailingId
813 * The id of the mailing to be canceled.
815 public static function cancel($mailingId) {
818 FROM civicrm_mailing_job
819 WHERE mailing_id = %1
821 AND ( ( job_type IS NULL ) OR
822 job_type <> 'child' )
824 $params = [1 => [$mailingId, 'Integer']];
825 $job = CRM_Core_DAO
::executeQuery($sql, $params);
827 in_array($job->status
, ['Scheduled', 'Running', 'Paused'])
830 self
::create(['id' => $job->id
, 'end_date' => date('YmdHis'), 'status' => 'Canceled']);
832 // also cancel all child jobs
834 UPDATE civicrm_mailing_job
835 SET status = 'Canceled',
839 AND job_type = 'child'
840 AND status IN ( 'Scheduled', 'Running', 'Paused' )
843 1 => [$job->id
, 'Integer'],
844 2 => [date('YmdHis'), 'Timestamp'],
846 CRM_Core_DAO
::executeQuery($sql, $params);
853 * @param int $mailingID
854 * The id of the mailing to be paused.
856 public static function pause($mailingID) {
858 UPDATE civicrm_mailing_job
859 SET status = 'Paused'
860 WHERE mailing_id = %1
862 AND status IN ('Scheduled', 'Running')
864 CRM_Core_DAO
::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
870 * @param int $mailingID
871 * The id of the mailing to be resumed.
873 public static function resume($mailingID) {
875 UPDATE civicrm_mailing_job
876 SET status = 'Scheduled'
877 WHERE mailing_id = %1
879 AND start_date IS NULL
880 AND status = 'Paused'
882 CRM_Core_DAO
::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
885 UPDATE civicrm_mailing_job
886 SET status = 'Running'
887 WHERE mailing_id = %1
889 AND start_date IS NOT NULL
890 AND status = 'Paused'
892 CRM_Core_DAO
::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
896 * Return a translated status enum string.
898 * @param string $status
902 * The translated version
904 public static function status($status) {
905 static $translation = NULL;
907 if (empty($translation)) {
909 'Scheduled' => ts('Scheduled'),
910 'Running' => ts('Running'),
911 'Complete' => ts('Complete'),
912 'Paused' => ts('Paused'),
913 'Canceled' => ts('Canceled'),
916 return CRM_Utils_Array
::value($status, $translation, ts('Not scheduled'));
920 * Return a workflow clause for use in SQL queries,
921 * to only process jobs that are approved.
924 * For use in a WHERE clause
926 public static function workflowClause() {
927 // add an additional check and only process
928 // jobs that are approved
929 if (CRM_Mailing_Info
::workflowEnabled()) {
930 $approveOptionID = CRM_Core_PseudoConstant
::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'Approved');
931 if ($approveOptionID) {
932 return " AND m.approval_status_id = $approveOptionID ";
939 * @param array $deliveredParams
940 * @param array $targetParams
945 * @throws CRM_Core_Exception
948 public function writeToDB(
954 static $activityTypeID = NULL;
955 static $writeActivity = NULL;
957 if (!empty($deliveredParams)) {
958 CRM_Mailing_Event_BAO_Delivered
::bulkCreate($deliveredParams);
959 $deliveredParams = [];
962 if ($writeActivity === NULL) {
963 $writeActivity = Civi
::settings()->get('write_activity_record');
966 if (!$writeActivity) {
971 if (!empty($targetParams) && !empty($mailing->scheduled_id
)) {
972 if (!$activityTypeID) {
973 if ($mailing->sms_provider_id
) {
974 $mailing->subject
= $mailing->name
;
975 $activityTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Mass SMS'
979 $activityTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
981 if (!$activityTypeID) {
982 throw new CRM_Core_Execption(ts('No relevant activity type found when recording Mailing Event delivered Activity'));
987 'source_contact_id' => $mailing->scheduled_id
,
988 'activity_type_id' => $activityTypeID,
989 'source_record_id' => $this->mailing_id
,
990 'activity_date_time' => $job_date,
991 'subject' => $mailing->subject
,
992 'status_id' => 'Completed',
993 'campaign_id' => $mailing->campaign_id
,
996 //check whether activity is already created for this mailing.
997 //if yes then create only target contact record.
1000 FROM civicrm_activity
1001 WHERE civicrm_activity.activity_type_id = %1
1002 AND civicrm_activity.source_record_id = %2
1006 1 => [$activityTypeID, 'Integer'],
1007 2 => [$this->mailing_id
, 'Integer'],
1009 $activityID = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
1010 $targetRecordID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets');
1012 $activityTargets = [];
1013 foreach ($targetParams as $id) {
1014 $activityTargets[$id] = ['contact_id' => (int) $id];
1017 $activity['id'] = $activityID;
1020 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
1021 // make sure we don't attempt to duplicate the target activity
1022 // @todo - we don't have to do one contact at a time....
1023 foreach ($activityTargets as $key => $target) {
1026 FROM civicrm_activity_contact
1027 WHERE activity_id = $activityID
1028 AND contact_id = {$target['contact_id']}
1029 AND record_type_id = $targetRecordID
1031 if (CRM_Core_DAO
::singleValueQuery($sql)) {
1032 unset($activityTargets[$key]);
1039 $activity = civicrm_api3('Activity', 'create', $activity);
1040 ActivityContact
::save(FALSE)->setRecords($activityTargets)->setDefaults(['activity_id' => $activity['id'], 'record_type_id' => $targetRecordID])->execute();
1042 catch (Exception
$e) {
1053 * Search the mailing-event queue for a list of pending delivery tasks.
1056 * @param string $medium
1057 * Ex: 'email' or 'sms'.
1059 * @return \CRM_Mailing_Event_BAO_Queue
1060 * A query object whose rows provide ('id', 'contact_id', 'hash') and ('email' or 'phone').
1062 public static function findPendingTasks($jobId, $medium) {
1063 $eq = new CRM_Mailing_Event_BAO_Queue();
1064 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
1065 $emailTable = CRM_Core_BAO_Email
::getTableName();
1066 $phoneTable = CRM_Core_BAO_Phone
::getTableName();
1067 $contactTable = CRM_Contact_BAO_Contact
::getTableName();
1068 $deliveredTable = CRM_Mailing_Event_BAO_Delivered
::getTableName();
1069 $bounceTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
1071 $query = " SELECT $queueTable.id,
1072 $emailTable.email as email,
1073 $queueTable.contact_id,
1077 INNER JOIN $emailTable
1078 ON $queueTable.email_id = $emailTable.id
1079 INNER JOIN $contactTable
1080 ON $contactTable.id = $emailTable.contact_id
1081 LEFT JOIN $deliveredTable
1082 ON $queueTable.id = $deliveredTable.event_queue_id
1083 LEFT JOIN $bounceTable
1084 ON $queueTable.id = $bounceTable.event_queue_id
1085 WHERE $queueTable.job_id = " . $jobId . "
1086 AND $deliveredTable.id IS null
1087 AND $bounceTable.id IS null
1088 AND $contactTable.is_opt_out = 0";
1090 if ($medium === 'sms') {
1092 SELECT $queueTable.id,
1093 $phoneTable.phone as phone,
1094 $queueTable.contact_id,
1098 INNER JOIN $phoneTable
1099 ON $queueTable.phone_id = $phoneTable.id
1100 INNER JOIN $contactTable
1101 ON $contactTable.id = $phoneTable.contact_id
1102 LEFT JOIN $deliveredTable
1103 ON $queueTable.id = $deliveredTable.event_queue_id
1104 LEFT JOIN $bounceTable
1105 ON $queueTable.id = $bounceTable.event_queue_id
1106 WHERE $queueTable.job_id = " . $jobId . "
1107 AND $deliveredTable.id IS null
1108 AND $bounceTable.id IS null
1109 AND ( $contactTable.is_opt_out = 0
1110 OR $contactTable.do_not_sms = 0 )";
1117 * Delete the mailing job.
1124 public static function del($id) {
1125 CRM_Utils_Hook
::pre('delete', 'MailingJob', $id, CRM_Core_DAO
::$_nullArray);
1127 $jobDAO = new CRM_Mailing_BAO_MailingJob();
1129 $result = $jobDAO->delete();
1131 CRM_Utils_Hook
::post('delete', 'MailingJob', $jobDAO->id
, $jobDAO);