Merge pull request #22460 from civicrm/5.46
[civicrm-core.git] / CRM / Mailing / BAO / MailingJob.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11 use Civi\Api4\ActivityContact;
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19 require_once 'Mail.php';
20
21 /**
22 * Class CRM_Mailing_BAO_MailingJob
23 */
24 class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
25 const MAX_CONTACTS_TO_PROCESS = 1000;
26
27 /**
28 * (Dear God Why) Keep a global count of mails processed within the current
29 * request.
30 *
31 * @var int
32 */
33 public static $mailsProcessed = 0;
34
35 /**
36 * Create mailing job.
37 *
38 * @param array $params
39 *
40 * @return \CRM_Mailing_BAO_MailingJob
41 * @throws \CRM_Core_Exception
42 */
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");
46 }
47 $op = empty($params['id']) ? 'create' : 'edit';
48 CRM_Utils_Hook::pre($op, 'MailingJob', CRM_Utils_Array::value('id', $params), $params);
49
50 $jobDAO = new CRM_Mailing_BAO_MailingJob();
51 $jobDAO->copyValues($params);
52 $jobDAO->save();
53 if (!empty($params['mailing_id']) && empty('is_calling_function_updated_to_reflect_deprecation')) {
54 CRM_Mailing_BAO_Mailing::getRecipients($params['mailing_id']);
55 }
56 CRM_Utils_Hook::post($op, 'MailingJob', $jobDAO->id, $jobDAO);
57 return $jobDAO;
58 }
59
60 /**
61 * Initiate all pending/ready jobs.
62 *
63 * @param array $testParams
64 * @param string $mode
65 *
66 * @return bool|null
67 */
68 public static function runJobs($testParams = NULL, $mode = NULL) {
69 $job = new CRM_Mailing_BAO_MailingJob();
70
71 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
72 $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
73 $mailerBatchLimit = Civi::settings()->get('mailerBatchLimit');
74
75 if (!empty($testParams)) {
76 $query = "
77 SELECT *
78 FROM $jobTable
79 WHERE id = {$testParams['job_id']}";
80 $job->query($query);
81 }
82 else {
83 $currentTime = date('YmdHis');
84 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m');
85 $domainID = CRM_Core_Config::domainID();
86
87 $modeClause = 'AND m.sms_provider_id IS NULL';
88 if ($mode == 'sms') {
89 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
90 }
91
92 // Select the first child job that is scheduled
93 // CRM-6835
94 $query = "
95 SELECT j.*
96 FROM $jobTable j,
97 $mailingTable m
98 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
99 {$modeClause}
100 AND j.is_test = 0
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')
107 AND {$mailingACL}
108 ORDER BY j.scheduled_date ASC,
109 j.id
110 ";
111
112 $job->query($query);
113 }
114
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()) {
119 continue;
120 }
121
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',
129 $job->id,
130 'status',
131 'id',
132 TRUE
133 );
134
135 if (
136 $job->status != 'Running' &&
137 $job->status != 'Scheduled'
138 ) {
139 // this includes Cancelled and other statuses, CRM-4246
140 $lock->release();
141 continue;
142 }
143 }
144
145 /* Queue up recipients for the child job being launched */
146
147 if ($job->status != 'Running') {
148 $transaction = new CRM_Core_Transaction();
149
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);
153
154 // Update to show job has started.
155 self::create([
156 'id' => $job->id,
157 'start_date' => date('YmdHis'),
158 'status' => 'Running',
159 ]);
160
161 $transaction->commit();
162 }
163
164 // Get the mailer
165 if ($mode === NULL) {
166 $mailer = \Civi::service('pear_mail');
167 }
168 elseif ($mode == 'sms') {
169 $mailer = CRM_SMS_Provider::singleton(['mailing_id' => $job->mailing_id]);
170 }
171
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]);
175 }
176 else {
177 $isComplete = $job->deliver($mailer, $testParams);
178 }
179
180 CRM_Utils_Hook::post('create', 'CRM_Mailing_DAO_Spool', $job->id, $isComplete);
181
182 // Mark the child complete
183 if ($isComplete) {
184 // Finish the job.
185
186 $transaction = new CRM_Core_Transaction();
187 self::create(['id' => $job->id, 'end_date' => date('YmdHis'), 'status' => 'Complete']);
188 $transaction->commit();
189
190 // don't mark the mailing as complete
191 }
192
193 // Release the child joblock
194 $lock->release();
195
196 if ($testParams) {
197 return $isComplete;
198 }
199
200 // CRM-17629: Stop processing jobs if mailer batch limit reached
201 if ($mailerBatchLimit > 0 && self::$mailsProcessed >= $mailerBatchLimit) {
202 break;
203 }
204
205 }
206 }
207
208 /**
209 * Post process to determine if the parent job
210 * as well as the mailing is complete after the run.
211 * @param null $mode
212 */
213 public static function runJobs_post($mode = NULL) {
214
215 $job = new CRM_Mailing_BAO_MailingJob();
216
217 $mailing = new CRM_Mailing_BAO_Mailing();
218
219 $config = CRM_Core_Config::singleton();
220 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
221 $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
222
223 $currentTime = date('YmdHis');
224 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m');
225 $domainID = CRM_Core_Config::domainID();
226
227 $query = "
228 SELECT j.*
229 FROM $jobTable j,
230 $mailingTable m
231 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
232 AND j.is_test = 0
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,
238 j.start_date";
239
240 $job->query($query);
241
242 // For each parent job that is running, let's look at their child jobs
243 while ($job->fetch()) {
244
245 $child_job = new CRM_Mailing_BAO_MailingJob();
246
247 $child_job_sql = "
248 SELECT count(j.id)
249 FROM civicrm_mailing_job j, civicrm_mailing m
250 WHERE m.id = j.mailing_id
251 AND j.job_type = 'child'
252 AND j.parent_id = %1
253 AND j.status <> 'Complete'";
254 $params = [1 => [$job->id, 'Integer']];
255
256 $anyChildLeft = CRM_Core_DAO::singleValueQuery($child_job_sql, $params);
257
258 // all of the child jobs are complete, update
259 // the parent job as well as the mailing status
260 if (!$anyChildLeft) {
261
262 $transaction = new CRM_Core_Transaction();
263
264 $saveJob = new CRM_Mailing_DAO_MailingJob();
265 $saveJob->id = $job->id;
266 $saveJob->end_date = date('YmdHis');
267 $saveJob->status = 'Complete';
268 $saveJob->save();
269
270 $mailing->reset();
271 $mailing->id = $job->mailing_id;
272 $mailing->is_completed = TRUE;
273 $mailing->save();
274 $transaction->commit();
275
276 // CRM-17763
277 CRM_Utils_Hook::postMailing($job->mailing_id);
278 }
279 }
280 }
281
282 /**
283 * before we run jobs, we need to split the jobs
284 * @param int $offset
285 * @param null $mode
286 */
287 public static function runJobs_pre($offset = 200, $mode = NULL) {
288 $job = new CRM_Mailing_BAO_MailingJob();
289
290 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
291 $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
292
293 $currentTime = date('YmdHis');
294 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m');
295
296 $workflowClause = CRM_Mailing_BAO_MailingJob::workflowClause();
297
298 $domainID = CRM_Core_Config::domainID();
299
300 $modeClause = 'AND m.sms_provider_id IS NULL';
301 if ($mode == 'sms') {
302 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
303 }
304
305 // Select all the mailing jobs that are created from
306 // when the mailing is submitted or scheduled.
307 $query = "
308 SELECT j.*
309 FROM $jobTable j,
310 $mailingTable m
311 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
312 $workflowClause
313 $modeClause
314 AND j.is_test = 0
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,
321 j.start_date";
322
323 $job->query($query);
324
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()) {
331 continue;
332 }
333
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',
339 $job->id,
340 'status',
341 'id',
342 TRUE
343 );
344 if ($job->status != 'Scheduled') {
345 $lock->release();
346 continue;
347 }
348
349 $transaction = new CRM_Core_Transaction();
350
351 $job->split_job($offset);
352
353 // Update the status of the parent job
354 self::create(['id' => $job->id, 'start_date' => date('YmdHis'), 'status' => 'Running']);
355 $transaction->commit();
356
357 // Release the job lock
358 $lock->release();
359 }
360 }
361
362 /**
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
365 * @param int $offset
366 */
367 public function split_job($offset = 200) {
368 $recipient_count = CRM_Mailing_BAO_Recipients::mailingSize($this->mailing_id);
369
370 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
371
372 $dao = new CRM_Core_DAO();
373
374 $sql = "
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)
378 ";
379 $params = [
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'],
385 6 => [0, 'Integer'],
386 7 => [$recipient_count, 'Integer'],
387 ];
388
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
393 ) {
394 CRM_Core_DAO::executeQuery($sql, $params);
395 }
396 else {
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);
401 $params[6][0] = $i;
402 $params[7][0] = $offset;
403 CRM_Core_DAO::executeQuery($sql, $params);
404 }
405 }
406
407 }
408
409 /**
410 * @param array $testParams
411 */
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);
417 }
418 else {
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);
422
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
426 $now = time();
427 $params = [];
428 $count = 0;
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)) {
435 continue;
436 }
437
438 if ($recipients->phone_id) {
439 $recipients->email_id = "null";
440 }
441 else {
442 $recipients->phone_id = "null";
443 }
444
445 $params[] = [
446 $this->id,
447 $recipients->email_id,
448 $recipients->contact_id,
449 $recipients->phone_id,
450 ];
451 $count++;
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);
455 $count = 0;
456 $params = [];
457 }
458 }
459
460 if (!empty($params)) {
461 CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now);
462 }
463 }
464 }
465
466 /**
467 * Send the mailing.
468 *
469 * @deprecated
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.
473 *
474 * @param array $testParams
475 * @return bool
476 */
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");
480 }
481
482 $mailing = new CRM_Mailing_BAO_Mailing();
483 $mailing->id = $this->mailing_id;
484 $mailing->find(TRUE);
485
486 $config = NULL;
487
488 if ($config == NULL) {
489 $config = CRM_Core_Config::singleton();
490 }
491
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);
494 }
495
496 $job_date = $this->scheduled_date;
497 $fields = [];
498
499 if (!empty($testParams)) {
500 $mailing->subject = ts('[CiviMail Draft]') . ' ' . $mailing->subject;
501 }
502
503 CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
504
505 // get and format attachments
506 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
507
508 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
509 CRM_Core_Smarty::registerStringResource();
510 }
511
512 // CRM-12376
513 // This handles the edge case scenario where all the mails
514 // have been delivered in prior jobs.
515 $isDelivered = TRUE;
516
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);
524 }
525 return FALSE;
526 }
527 self::$mailsProcessed++;
528
529 $fields[] = [
530 'id' => $eq->id,
531 'hash' => $eq->hash,
532 'contact_id' => $eq->contact_id,
533 'email' => $eq->email,
534 'phone' => $eq->phone,
535 ];
536 if (count($fields) == self::MAX_CONTACTS_TO_PROCESS) {
537 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
538 if (!$isDelivered) {
539 return $isDelivered;
540 }
541 $fields = [];
542 }
543 }
544
545 if (!empty($fields)) {
546 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
547 }
548 return $isDelivered;
549 }
550
551 /**
552 * @deprecated
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.
557 * @param $mailing
558 * @param $mailer
559 * @param $job_date
560 * @param $attachments
561 *
562 * @return bool|null
563 * @throws Exception
564 */
565 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
566 static $smtpConnectionErrors = 0;
567
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');
570 }
571
572 // get the return properties
573 $returnProperties = $mailing->getReturnProperties();
574 $params = $targetParams = $deliveredParams = [];
575 $count = 0;
576 // dev/core#1768 Get the mail sync interval.
577 $mail_sync_interval = Civi::settings()->get('civimail_sync_interval');
578 $retryGroup = FALSE;
579
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
583 $skipOnHold = TRUE;
584 if ($mailing->sms_provider_id) {
585 //do not include a statement to check wether e-mail address is on hold
586 $skipOnHold = FALSE;
587 }
588
589 foreach ($fields as $key => $field) {
590 $params[] = $field['contact_id'];
591 }
592
593 [$details] = CRM_Utils_Token::getTokenDetails(
594 $params,
595 $returnProperties,
596 $skipOnHold, TRUE, NULL,
597 $mailing->getFlattenedTokens(),
598 get_class($this),
599 $this->id
600 );
601
602 foreach ($fields as $key => $field) {
603 $contactID = $field['contact_id'];
604 if (!array_key_exists($contactID, $details)) {
605 $details[$contactID] = [];
606 }
607
608 // Compose the mailing.
609 $recipient = $replyToEmail = NULL;
610 $replyValue = strcmp($mailing->replyto_email, $mailing->from_email);
611 if ($replyValue) {
612 $replyToEmail = $mailing->replyto_email;
613 }
614
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
620 );
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
625 continue;
626 }
627
628 // Send the mailing.
629
630 $body = $message->get();
631 $headers = $message->headers();
632
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]);
637 }
638
639 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
640 $recipient = $headers['To'];
641 $result = NULL;
642
643 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
644 if ($job_date) {
645 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
646 }
647
648 $result = $mailer->send($recipient, $headers, $body, $this->id);
649
650 if ($job_date) {
651 unset($errorScope);
652 }
653
654 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) {
655 // CRM-9191
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");
661
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();
667 $retryGroup = TRUE;
668 continue;
669 }
670
671 // seems like we have too many of them in a row, we should
672 // write stuff to disk and abort the cron job
673 $this->writeToDB(
674 $deliveredParams,
675 $targetParams,
676 $mailing,
677 $job_date
678 );
679
680 CRM_Core_Error::debug_log_message("Too many SMTP Socket Errors. Exiting");
681 CRM_Utils_System::civiExit();
682 }
683
684 // Register the bounce event.
685
686 $params = [
687 'event_queue_id' => $field['id'],
688 'job_id' => $this->id,
689 'hash' => $field['hash'],
690 ];
691 $params = array_merge($params,
692 CRM_Mailing_BAO_BouncePattern::match($result->getMessage())
693 );
694 CRM_Mailing_Event_BAO_Bounce::create($params);
695 }
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());
702 }
703 else {
704 // Register the delivery event.
705 $deliveredParams[] = $field['id'];
706 $targetParams[] = $field['contact_id'];
707
708 $count++;
709 // dev/core#1768 Mail sync interval is now configurable.
710 if ($count % $mail_sync_interval == 0) {
711 $this->writeToDB(
712 $deliveredParams,
713 $targetParams,
714 $mailing,
715 $job_date
716 );
717 $count = 0;
718
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',
724 $this->id,
725 'status',
726 'id',
727 TRUE
728 );
729
730 if ($status != 'Running') {
731 return FALSE;
732 }
733 }
734 }
735
736 unset($result);
737
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--;
742 }
743
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);
748 }
749 }
750
751 $result = $this->writeToDB(
752 $deliveredParams,
753 $targetParams,
754 $mailing,
755 $job_date
756 );
757
758 if ($retryGroup) {
759 return FALSE;
760 }
761
762 return $result;
763 }
764
765 /**
766 * Determine if an SMTP error is temporary or permanent.
767 *
768 * @param string $message
769 * PEAR error message.
770 * @return bool
771 * TRUE - Temporary/retriable error
772 * FALSE - Permanent/non-retriable error
773 */
774 protected function isTemporaryError($message) {
775 // SMTP response code is buried in the message.
776 $code = preg_match('/ \(code: (.+), response: /', $message, $matches) ? $matches[1] : '';
777
778 if (strpos($message, 'Failed to write to socket') !== FALSE) {
779 return TRUE;
780 }
781
782 // Register 5xx SMTP response code (permanent failure) as bounce.
783 if (isset($code[0]) && $code[0] === '5') {
784 return FALSE;
785 }
786
787 if (strpos($message, 'Failed to set sender') !== FALSE) {
788 return TRUE;
789 }
790
791 if (strpos($message, 'Failed to add recipient') !== FALSE) {
792 return TRUE;
793 }
794
795 if (strpos($message, 'Failed to send data') !== FALSE) {
796 return TRUE;
797 }
798
799 return FALSE;
800 }
801
802 /**
803 * Cancel a mailing.
804 *
805 * @param int $mailingId
806 * The id of the mailing to be canceled.
807 */
808 public static function cancel($mailingId) {
809 $sql = "
810 SELECT *
811 FROM civicrm_mailing_job
812 WHERE mailing_id = %1
813 AND is_test = 0
814 AND ( ( job_type IS NULL ) OR
815 job_type <> 'child' )
816 ";
817 $params = [1 => [$mailingId, 'Integer']];
818 $job = CRM_Core_DAO::executeQuery($sql, $params);
819 if ($job->fetch() &&
820 in_array($job->status, ['Scheduled', 'Running', 'Paused'])
821 ) {
822
823 self::create(['id' => $job->id, 'end_date' => date('YmdHis'), 'status' => 'Canceled']);
824
825 // also cancel all child jobs
826 $sql = "
827 UPDATE civicrm_mailing_job
828 SET status = 'Canceled',
829 end_date = %2
830 WHERE parent_id = %1
831 AND is_test = 0
832 AND job_type = 'child'
833 AND status IN ( 'Scheduled', 'Running', 'Paused' )
834 ";
835 $params = [
836 1 => [$job->id, 'Integer'],
837 2 => [date('YmdHis'), 'Timestamp'],
838 ];
839 CRM_Core_DAO::executeQuery($sql, $params);
840 }
841 }
842
843 /**
844 * Pause a mailing
845 *
846 * @param int $mailingID
847 * The id of the mailing to be paused.
848 */
849 public static function pause($mailingID) {
850 $sql = "
851 UPDATE civicrm_mailing_job
852 SET status = 'Paused'
853 WHERE mailing_id = %1
854 AND is_test = 0
855 AND status IN ('Scheduled', 'Running')
856 ";
857 CRM_Core_DAO::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
858 }
859
860 /**
861 * Resume a mailing
862 *
863 * @param int $mailingID
864 * The id of the mailing to be resumed.
865 */
866 public static function resume($mailingID) {
867 $sql = "
868 UPDATE civicrm_mailing_job
869 SET status = 'Scheduled'
870 WHERE mailing_id = %1
871 AND is_test = 0
872 AND start_date IS NULL
873 AND status = 'Paused'
874 ";
875 CRM_Core_DAO::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
876
877 $sql = "
878 UPDATE civicrm_mailing_job
879 SET status = 'Running'
880 WHERE mailing_id = %1
881 AND is_test = 0
882 AND start_date IS NOT NULL
883 AND status = 'Paused'
884 ";
885 CRM_Core_DAO::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
886 }
887
888 /**
889 * Return a translated status enum string.
890 *
891 * @param string $status
892 * The status enum.
893 *
894 * @return string
895 * The translated version
896 */
897 public static function status($status) {
898 static $translation = NULL;
899
900 if (empty($translation)) {
901 $translation = [
902 'Scheduled' => ts('Scheduled'),
903 'Running' => ts('Running'),
904 'Complete' => ts('Complete'),
905 'Paused' => ts('Paused'),
906 'Canceled' => ts('Canceled'),
907 ];
908 }
909 return CRM_Utils_Array::value($status, $translation, ts('Not scheduled'));
910 }
911
912 /**
913 * Return a workflow clause for use in SQL queries,
914 * to only process jobs that are approved.
915 *
916 * @return string
917 * For use in a WHERE clause
918 */
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 ";
926 }
927 }
928 return '';
929 }
930
931 /**
932 * @param array $deliveredParams
933 * @param array $targetParams
934 * @param $mailing
935 * @param $job_date
936 *
937 * @return bool
938 * @throws CRM_Core_Exception
939 * @throws Exception
940 */
941 public function writeToDB(
942 &$deliveredParams,
943 &$targetParams,
944 &$mailing,
945 $job_date
946 ) {
947 static $activityTypeID = NULL;
948 static $writeActivity = NULL;
949
950 if (!empty($deliveredParams)) {
951 CRM_Mailing_Event_BAO_Delivered::bulkCreate($deliveredParams);
952 $deliveredParams = [];
953 }
954
955 if ($writeActivity === NULL) {
956 $writeActivity = Civi::settings()->get('write_activity_record');
957 }
958
959 if (!$writeActivity) {
960 return TRUE;
961 }
962
963 $result = TRUE;
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'
969 );
970 }
971 else {
972 $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
973 }
974 if (!$activityTypeID) {
975 throw new CRM_Core_Exception(ts('No relevant activity type found when recording Mailing Event delivered Activity'));
976 }
977 }
978
979 $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,
987 ];
988
989 //check whether activity is already created for this mailing.
990 //if yes then create only target contact record.
991 $query = "
992 SELECT id
993 FROM civicrm_activity
994 WHERE civicrm_activity.activity_type_id = %1
995 AND civicrm_activity.source_record_id = %2
996 ";
997
998 $queryParams = [
999 1 => [$activityTypeID, 'Integer'],
1000 2 => [$this->mailing_id, 'Integer'],
1001 ];
1002 $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams);
1003 $targetRecordID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets');
1004
1005 $activityTargets = [];
1006 foreach ($targetParams as $id) {
1007 $activityTargets[$id] = ['contact_id' => (int) $id];
1008 }
1009 if ($activityID) {
1010 $activity['id'] = $activityID;
1011
1012 // CRM-9519
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) {
1017 $sql = "
1018 SELECT id
1019 FROM civicrm_activity_contact
1020 WHERE activity_id = $activityID
1021 AND contact_id = {$target['contact_id']}
1022 AND record_type_id = $targetRecordID
1023 ";
1024 if (CRM_Core_DAO::singleValueQuery($sql)) {
1025 unset($activityTargets[$key]);
1026 }
1027 }
1028 }
1029 }
1030
1031 try {
1032 $activity = civicrm_api3('Activity', 'create', $activity);
1033 ActivityContact::save(FALSE)->setRecords($activityTargets)->setDefaults(['activity_id' => $activity['id'], 'record_type_id' => $targetRecordID])->execute();
1034 }
1035 catch (Exception $e) {
1036 $result = FALSE;
1037 }
1038
1039 $targetParams = [];
1040 }
1041
1042 return $result;
1043 }
1044
1045 /**
1046 * Search the mailing-event queue for a list of pending delivery tasks.
1047 *
1048 * @param int $jobId
1049 * @param string $medium
1050 * Ex: 'email' or 'sms'.
1051 *
1052 * @return \CRM_Mailing_Event_BAO_Queue
1053 * A query object whose rows provide ('id', 'contact_id', 'hash') and ('email' or 'phone').
1054 */
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();
1063
1064 $query = " SELECT $queueTable.id,
1065 $emailTable.email as email,
1066 $queueTable.contact_id,
1067 $queueTable.hash,
1068 NULL as phone
1069 FROM $queueTable
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";
1082
1083 if ($medium === 'sms') {
1084 $query = "
1085 SELECT $queueTable.id,
1086 $phoneTable.phone as phone,
1087 $queueTable.contact_id,
1088 $queueTable.hash,
1089 NULL as email
1090 FROM $queueTable
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 )";
1104 }
1105 $eq->query($query);
1106 return $eq;
1107 }
1108
1109 /**
1110 * Delete the mailing job.
1111 *
1112 * @param int $id
1113 * @deprecated
1114 * @return bool
1115 */
1116 public static function del($id) {
1117 return (bool) self::deleteRecord(['id' => $id]);
1118 }
1119
1120 }