Merge pull request #14127 from civicrm/5.13
[civicrm-core.git] / CRM / Mailing / BAO / MailingJob.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 require_once 'Mail.php';
35
36 /**
37 * Class CRM_Mailing_BAO_MailingJob
38 */
39 class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
40 const MAX_CONTACTS_TO_PROCESS = 1000;
41
42 /**
43 * (Dear God Why) Keep a global count of mails processed within the current
44 * request.
45 *
46 * @var int
47 */
48 public static $mailsProcessed = 0;
49
50 /**
51 * Class constructor.
52 */
53 public function __construct() {
54 parent::__construct();
55 }
56
57 /**
58 * Create mailing job.
59 *
60 * @param array $params
61 *
62 * @return \CRM_Mailing_BAO_MailingJob
63 * @throws \CRM_Core_Exception
64 */
65 public static function create($params) {
66 if (empty($params['id']) && empty($params['mailing_id'])) {
67 throw new CRM_Core_Exception("Failed to create job: Unknown mailing ID");
68 }
69 $op = empty($params['id']) ? 'create' : 'edit';
70 CRM_Utils_Hook::pre($op, 'MailingJob', CRM_Utils_Array::value('id', $params), $params);
71
72 $jobDAO = new CRM_Mailing_BAO_MailingJob();
73 $jobDAO->copyValues($params, TRUE);
74 $jobDAO->save();
75 if (!empty($params['mailing_id'])) {
76 CRM_Mailing_BAO_Mailing::getRecipients($params['mailing_id']);
77 }
78 CRM_Utils_Hook::post($op, 'MailingJob', $jobDAO->id, $jobDAO);
79 return $jobDAO;
80 }
81
82 /**
83 * Initiate all pending/ready jobs.
84 *
85 * @param array $testParams
86 * @param string $mode
87 *
88 * @return bool|null
89 */
90 public static function runJobs($testParams = NULL, $mode = NULL) {
91 $job = new CRM_Mailing_BAO_MailingJob();
92
93 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
94 $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
95 $mailerBatchLimit = Civi::settings()->get('mailerBatchLimit');
96
97 if (!empty($testParams)) {
98 $query = "
99 SELECT *
100 FROM $jobTable
101 WHERE id = {$testParams['job_id']}";
102 $job->query($query);
103 }
104 else {
105 $currentTime = date('YmdHis');
106 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m');
107 $domainID = CRM_Core_Config::domainID();
108
109 $modeClause = 'AND m.sms_provider_id IS NULL';
110 if ($mode == 'sms') {
111 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
112 }
113
114 // Select the first child job that is scheduled
115 // CRM-6835
116 $query = "
117 SELECT j.*
118 FROM $jobTable j,
119 $mailingTable m
120 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
121 {$modeClause}
122 AND j.is_test = 0
123 AND ( ( j.start_date IS null
124 AND j.scheduled_date <= $currentTime
125 AND j.status = 'Scheduled' )
126 OR ( j.status = 'Running'
127 AND j.end_date IS null ) )
128 AND (j.job_type = 'child')
129 AND {$mailingACL}
130 ORDER BY j.scheduled_date ASC,
131 j.id
132 ";
133
134 $job->query($query);
135 }
136
137 while ($job->fetch()) {
138 // still use job level lock for each child job
139 $lock = Civi::lockManager()->acquire("data.mailing.job.{$job->id}");
140 if (!$lock->isAcquired()) {
141 continue;
142 }
143
144 // for test jobs we do not change anything, since its on a short-circuit path
145 if (empty($testParams)) {
146 // we've got the lock, but while we were waiting and processing
147 // other emails, this job might have changed under us
148 // lets get the job status again and check
149 $job->status = CRM_Core_DAO::getFieldValue(
150 'CRM_Mailing_DAO_MailingJob',
151 $job->id,
152 'status',
153 'id',
154 TRUE
155 );
156
157 if (
158 $job->status != 'Running' &&
159 $job->status != 'Scheduled'
160 ) {
161 // this includes Cancelled and other statuses, CRM-4246
162 $lock->release();
163 continue;
164 }
165 }
166
167 /* Queue up recipients for the child job being launched */
168
169 if ($job->status != 'Running') {
170 $transaction = new CRM_Core_Transaction();
171
172 // have to queue it up based on the offset and limits
173 // get the parent ID, and limit and offset
174 $job->queue($testParams);
175
176 // Update to show job has started.
177 self::create([
178 'id' => $job->id,
179 'start_date' => date('YmdHis'),
180 'status' => 'Running',
181 ]);
182
183 $transaction->commit();
184 }
185
186 // Get the mailer
187 if ($mode === NULL) {
188 $mailer = \Civi::service('pear_mail');
189 }
190 elseif ($mode == 'sms') {
191 $mailer = CRM_SMS_Provider::singleton(['mailing_id' => $job->mailing_id]);
192 }
193
194 // Compose and deliver each child job
195 if (\CRM_Utils_Constant::value('CIVICRM_FLEXMAILER_HACK_DELIVER')) {
196 $isComplete = Civi\Core\Resolver::singleton()->call(CIVICRM_FLEXMAILER_HACK_DELIVER, [$job, $mailer, $testParams]);
197 }
198 else {
199 $isComplete = $job->deliver($mailer, $testParams);
200 }
201
202 CRM_Utils_Hook::post('create', 'CRM_Mailing_DAO_Spool', $job->id, $isComplete);
203
204 // Mark the child complete
205 if ($isComplete) {
206 // Finish the job.
207
208 $transaction = new CRM_Core_Transaction();
209 self::create(['id' => $job->id, 'end_date' => date('YmdHis'), 'status' => 'Complete']);
210 $transaction->commit();
211
212 // don't mark the mailing as complete
213 }
214
215 // Release the child joblock
216 $lock->release();
217
218 if ($testParams) {
219 return $isComplete;
220 }
221
222 // CRM-17629: Stop processing jobs if mailer batch limit reached
223 if ($mailerBatchLimit > 0 && self::$mailsProcessed >= $mailerBatchLimit) {
224 break;
225 }
226
227 }
228 }
229
230 /**
231 * Post process to determine if the parent job
232 * as well as the mailing is complete after the run.
233 * @param null $mode
234 */
235 public static function runJobs_post($mode = NULL) {
236
237 $job = new CRM_Mailing_BAO_MailingJob();
238
239 $mailing = new CRM_Mailing_BAO_Mailing();
240
241 $config = CRM_Core_Config::singleton();
242 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
243 $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
244
245 $currentTime = date('YmdHis');
246 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m');
247 $domainID = CRM_Core_Config::domainID();
248
249 $query = "
250 SELECT j.*
251 FROM $jobTable j,
252 $mailingTable m
253 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
254 AND j.is_test = 0
255 AND j.scheduled_date <= $currentTime
256 AND j.status = 'Running'
257 AND j.end_date IS null
258 AND (j.job_type != 'child' OR j.job_type is NULL)
259 ORDER BY j.scheduled_date,
260 j.start_date";
261
262 $job->query($query);
263
264 // For each parent job that is running, let's look at their child jobs
265 while ($job->fetch()) {
266
267 $child_job = new CRM_Mailing_BAO_MailingJob();
268
269 $child_job_sql = "
270 SELECT count(j.id)
271 FROM civicrm_mailing_job j, civicrm_mailing m
272 WHERE m.id = j.mailing_id
273 AND j.job_type = 'child'
274 AND j.parent_id = %1
275 AND j.status <> 'Complete'";
276 $params = [1 => [$job->id, 'Integer']];
277
278 $anyChildLeft = CRM_Core_DAO::singleValueQuery($child_job_sql, $params);
279
280 // all of the child jobs are complete, update
281 // the parent job as well as the mailing status
282 if (!$anyChildLeft) {
283
284 $transaction = new CRM_Core_Transaction();
285
286 $saveJob = new CRM_Mailing_DAO_MailingJob();
287 $saveJob->id = $job->id;
288 $saveJob->end_date = date('YmdHis');
289 $saveJob->status = 'Complete';
290 $saveJob->save();
291
292 $mailing->reset();
293 $mailing->id = $job->mailing_id;
294 $mailing->is_completed = TRUE;
295 $mailing->save();
296 $transaction->commit();
297
298 // CRM-17763
299 CRM_Utils_Hook::postMailing($job->mailing_id);
300 }
301 }
302 }
303
304 /**
305 * before we run jobs, we need to split the jobs
306 * @param int $offset
307 * @param null $mode
308 */
309 public static function runJobs_pre($offset = 200, $mode = NULL) {
310 $job = new CRM_Mailing_BAO_MailingJob();
311
312 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
313 $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
314
315 $currentTime = date('YmdHis');
316 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m');
317
318 $workflowClause = CRM_Mailing_BAO_MailingJob::workflowClause();
319
320 $domainID = CRM_Core_Config::domainID();
321
322 $modeClause = 'AND m.sms_provider_id IS NULL';
323 if ($mode == 'sms') {
324 $modeClause = 'AND m.sms_provider_id IS NOT NULL';
325 }
326
327 // Select all the mailing jobs that are created from
328 // when the mailing is submitted or scheduled.
329 $query = "
330 SELECT j.*
331 FROM $jobTable j,
332 $mailingTable m
333 WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}
334 $workflowClause
335 $modeClause
336 AND j.is_test = 0
337 AND ( ( j.start_date IS null
338 AND j.scheduled_date <= $currentTime
339 AND j.status = 'Scheduled'
340 AND j.end_date IS null ) )
341 AND ((j.job_type is NULL) OR (j.job_type <> 'child'))
342 ORDER BY j.scheduled_date,
343 j.start_date";
344
345 $job->query($query);
346
347 // For each of the "Parent Jobs" we find, we split them into
348 // X Number of child jobs
349 while ($job->fetch()) {
350 // still use job level lock for each child job
351 $lock = Civi::lockManager()->acquire("data.mailing.job.{$job->id}");
352 if (!$lock->isAcquired()) {
353 continue;
354 }
355
356 // Re-fetch the job status in case things
357 // changed between the first query and now
358 // to avoid race conditions
359 $job->status = CRM_Core_DAO::getFieldValue(
360 'CRM_Mailing_DAO_MailingJob',
361 $job->id,
362 'status',
363 'id',
364 TRUE
365 );
366 if ($job->status != 'Scheduled') {
367 $lock->release();
368 continue;
369 }
370
371 $transaction = new CRM_Core_Transaction();
372
373 $job->split_job($offset);
374
375 // Update the status of the parent job
376 self::create(['id' => $job->id, 'start_date' => date('YmdHis'), 'status' => 'Running']);
377 $transaction->commit();
378
379 // Release the job lock
380 $lock->release();
381 }
382 }
383
384 /**
385 * Split the parent job into n number of child job based on an offset.
386 * If null or 0 , we create only one child job
387 * @param int $offset
388 */
389 public function split_job($offset = 200) {
390 $recipient_count = CRM_Mailing_BAO_Recipients::mailingSize($this->mailing_id);
391
392 $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
393
394 $dao = new CRM_Core_DAO();
395
396 $sql = "
397 INSERT INTO civicrm_mailing_job
398 (`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`)
399 VALUES (%1, %2, %3, %4, %5, %6, %7)
400 ";
401 $params = [
402 1 => [$this->mailing_id, 'Integer'],
403 2 => [$this->scheduled_date, 'String'],
404 3 => ['Scheduled', 'String'],
405 4 => ['child', 'String'],
406 5 => [$this->id, 'Integer'],
407 6 => [0, 'Integer'],
408 7 => [$recipient_count, 'Integer'],
409 ];
410
411 // create one child job if the mailing size is less than the offset
412 // probably use a CRM_Mailing_DAO_MailingJob( );
413 if (empty($offset) ||
414 $recipient_count <= $offset
415 ) {
416 CRM_Core_DAO::executeQuery($sql, $params);
417 }
418 else {
419 // Creating 'child jobs'
420 $scheduled_unixtime = strtotime($this->scheduled_date);
421 for ($i = 0, $s = 0; $i < $recipient_count; $i = $i + $offset, $s++) {
422 $params[2][0] = date('Y-m-d H:i:s', $scheduled_unixtime + $s);
423 $params[6][0] = $i;
424 $params[7][0] = $offset;
425 CRM_Core_DAO::executeQuery($sql, $params);
426 }
427 }
428
429 }
430
431 /**
432 * @param array $testParams
433 */
434 public function queue($testParams = NULL) {
435 $mailing = new CRM_Mailing_BAO_Mailing();
436 $mailing->id = $this->mailing_id;
437 if (!empty($testParams)) {
438 $mailing->getTestRecipients($testParams);
439 }
440 else {
441 // We are still getting all the recipients from the parent job
442 // so we don't mess with the include/exclude logic.
443 $recipients = CRM_Mailing_BAO_Recipients::mailingQuery($this->mailing_id, $this->job_offset, $this->job_limit);
444
445 // FIXME: this is not very smart, we should move this to one DB call
446 // INSERT INTO ... SELECT FROM ..
447 // the thing we need to figure out is how to generate the hash automatically
448 $now = time();
449 $params = [];
450 $count = 0;
451 while ($recipients->fetch()) {
452 // CRM-18543: there are situations when both the email and phone are null.
453 // Skip the recipient in this case.
454 if (empty($recipients->email_id) && empty($recipients->phone_id)) {
455 continue;
456 }
457
458 if ($recipients->phone_id) {
459 $recipients->email_id = "null";
460 }
461 else {
462 $recipients->phone_id = "null";
463 }
464
465 $params[] = [
466 $this->id,
467 $recipients->email_id,
468 $recipients->contact_id,
469 $recipients->phone_id,
470 ];
471 $count++;
472 if ($count % CRM_Mailing_Config::BULK_MAIL_INSERT_COUNT == 0) {
473 CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now);
474 $count = 0;
475 $params = [];
476 }
477 }
478
479 if (!empty($params)) {
480 CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now);
481 }
482 }
483 }
484
485 /**
486 * Send the mailing.
487 *
488 * @deprecated
489 * This is used by CiviMail but will be made redundant by FlexMailer.
490 * @param object $mailer
491 * A Mail object to send the messages.
492 *
493 * @param array $testParams
494 * @return bool
495 */
496 public function deliver(&$mailer, $testParams = NULL) {
497 if (\Civi::settings()->get('experimentalFlexMailerEngine')) {
498 throw new \RuntimeException("Cannot use legacy deliver() when experimentalFlexMailerEngine is enabled");
499 }
500
501 $mailing = new CRM_Mailing_BAO_Mailing();
502 $mailing->id = $this->mailing_id;
503 $mailing->find(TRUE);
504 $mailing->free();
505
506 $config = NULL;
507
508 if ($config == NULL) {
509 $config = CRM_Core_Config::singleton();
510 }
511
512 if (property_exists($mailing, 'language') && $mailing->language && $mailing->language != 'en_US') {
513 $swapLang = CRM_Utils_AutoClean::swap('global://dbLocale?getter', 'call://i18n/setLocale', $mailing->language);
514 }
515
516 $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date);
517 $fields = [];
518
519 if (!empty($testParams)) {
520 $mailing->subject = ts('[CiviMail Draft]') . ' ' . $mailing->subject;
521 }
522
523 CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
524
525 // get and format attachments
526 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
527
528 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
529 CRM_Core_Smarty::registerStringResource();
530 }
531
532 // CRM-12376
533 // This handles the edge case scenario where all the mails
534 // have been delivered in prior jobs.
535 $isDelivered = TRUE;
536
537 // make sure that there's no more than $mailerBatchLimit mails processed in a run
538 $mailerBatchLimit = Civi::settings()->get('mailerBatchLimit');
539 $eq = self::findPendingTasks($this->id, $mailing->sms_provider_id ? 'sms' : 'email');
540 while ($eq->fetch()) {
541 if ($mailerBatchLimit > 0 && self::$mailsProcessed >= $mailerBatchLimit) {
542 if (!empty($fields)) {
543 $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
544 }
545 $eq->free();
546 return FALSE;
547 }
548 self::$mailsProcessed++;
549
550 $fields[] = [
551 'id' => $eq->id,
552 'hash' => $eq->hash,
553 'contact_id' => $eq->contact_id,
554 'email' => $eq->email,
555 'phone' => $eq->phone,
556 ];
557 if (count($fields) == self::MAX_CONTACTS_TO_PROCESS) {
558 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
559 if (!$isDelivered) {
560 $eq->free();
561 return $isDelivered;
562 }
563 $fields = [];
564 }
565 }
566
567 $eq->free();
568
569 if (!empty($fields)) {
570 $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
571 }
572 return $isDelivered;
573 }
574
575 /**
576 * @deprecated
577 * This is used by CiviMail but will be made redundant by FlexMailer.
578 * @param array $fields
579 * List of intended recipients.
580 * Each recipient is an array with keys 'hash', 'contact_id', 'email', etc.
581 * @param $mailing
582 * @param $mailer
583 * @param $job_date
584 * @param $attachments
585 *
586 * @return bool|null
587 * @throws Exception
588 */
589 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
590 static $smtpConnectionErrors = 0;
591
592 if (!is_object($mailer) || empty($fields)) {
593 CRM_Core_Error::fatal();
594 }
595
596 // get the return properties
597 $returnProperties = $mailing->getReturnProperties();
598 $params = $targetParams = $deliveredParams = [];
599 $count = 0;
600 $retryGroup = FALSE;
601
602 // CRM-15702: Sending bulk sms to contacts without e-mail address fails.
603 // Solution is to skip checking for on hold
604 //do include a statement to check wether e-mail address is on hold
605 $skipOnHold = TRUE;
606 if ($mailing->sms_provider_id) {
607 //do not include a statement to check wether e-mail address is on hold
608 $skipOnHold = FALSE;
609 }
610
611 foreach ($fields as $key => $field) {
612 $params[] = $field['contact_id'];
613 }
614
615 $details = CRM_Utils_Token::getTokenDetails(
616 $params,
617 $returnProperties,
618 $skipOnHold, TRUE, NULL,
619 $mailing->getFlattenedTokens(),
620 get_class($this),
621 $this->id
622 );
623
624 $config = CRM_Core_Config::singleton();
625 foreach ($fields as $key => $field) {
626 $contactID = $field['contact_id'];
627 if (!array_key_exists($contactID, $details[0])) {
628 $details[0][$contactID] = [];
629 }
630
631 // Compose the mailing.
632 $recipient = $replyToEmail = NULL;
633 $replyValue = strcmp($mailing->replyto_email, $mailing->from_email);
634 if ($replyValue) {
635 $replyToEmail = $mailing->replyto_email;
636 }
637
638 $message = $mailing->compose(
639 $this->id, $field['id'], $field['hash'],
640 $field['contact_id'], $field['email'],
641 $recipient, FALSE, $details[0][$contactID], $attachments,
642 FALSE, NULL, $replyToEmail
643 );
644 if (empty($message)) {
645 // lets keep the message in the queue
646 // most likely a permissions related issue with smarty templates
647 // or a bad contact id? CRM-9833
648 continue;
649 }
650
651 // Send the mailing.
652
653 $body = $message->get();
654 $headers = $message->headers();
655
656 if ($mailing->sms_provider_id) {
657 $provider = CRM_SMS_Provider::singleton(['mailing_id' => $mailing->id]);
658 $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
659 $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
660 }
661
662 // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
663 $recipient = $headers['To'];
664 $result = NULL;
665
666 // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
667 if ($job_date) {
668 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
669 }
670
671 $result = $mailer->send($recipient, $headers, $body, $this->id);
672
673 if ($job_date) {
674 unset($errorScope);
675 }
676
677 if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) {
678 // CRM-9191
679 $message = $result->getMessage();
680 if ($this->isTemporaryError($message)) {
681 // lets log this message and code
682 $code = $result->getCode();
683 CRM_Core_Error::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code");
684
685 // these are socket write errors which most likely means smtp connection errors
686 // lets skip them and reconnect.
687 $smtpConnectionErrors++;
688 if ($smtpConnectionErrors <= 5) {
689 $mailer->disconnect();
690 $retryGroup = TRUE;
691 continue;
692 }
693
694 // seems like we have too many of them in a row, we should
695 // write stuff to disk and abort the cron job
696 $this->writeToDB(
697 $deliveredParams,
698 $targetParams,
699 $mailing,
700 $job_date
701 );
702
703 CRM_Core_Error::debug_log_message("Too many SMTP Socket Errors. Exiting");
704 CRM_Utils_System::civiExit();
705 }
706
707 // Register the bounce event.
708
709 $params = [
710 'event_queue_id' => $field['id'],
711 'job_id' => $this->id,
712 'hash' => $field['hash'],
713 ];
714 $params = array_merge($params,
715 CRM_Mailing_BAO_BouncePattern::match($result->getMessage())
716 );
717 CRM_Mailing_Event_BAO_Bounce::create($params);
718 }
719 elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id) {
720 // Handle SMS errors: CRM-15426
721 $job_id = intval($this->id);
722 $mailing_id = intval($mailing->id);
723 CRM_Core_Error::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows.");
724 CRM_Core_Error::debug_log_message($result->getMessage());
725 }
726 else {
727 // Register the delivery event.
728 $deliveredParams[] = $field['id'];
729 $targetParams[] = $field['contact_id'];
730
731 $count++;
732 if ($count % CRM_Mailing_Config::BULK_MAIL_INSERT_COUNT == 0) {
733 $this->writeToDB(
734 $deliveredParams,
735 $targetParams,
736 $mailing,
737 $job_date
738 );
739 $count = 0;
740
741 // hack to stop mailing job at run time, CRM-4246.
742 // to avoid making too many DB calls for this rare case
743 // lets do it when we snapshot
744 $status = CRM_Core_DAO::getFieldValue(
745 'CRM_Mailing_DAO_MailingJob',
746 $this->id,
747 'status',
748 'id',
749 TRUE
750 );
751
752 if ($status != 'Running') {
753 return FALSE;
754 }
755 }
756 }
757
758 unset($result);
759
760 // seems like a successful delivery or bounce, lets decrement error count
761 // only if we have smtp connection errors
762 if ($smtpConnectionErrors > 0) {
763 $smtpConnectionErrors--;
764 }
765
766 // If we have enabled the Throttle option, this is the time to enforce it.
767 $mailThrottleTime = Civi::settings()->get('mailThrottleTime');
768 if (!empty($mailThrottleTime)) {
769 usleep((int ) $mailThrottleTime);
770 }
771 }
772
773 $result = $this->writeToDB(
774 $deliveredParams,
775 $targetParams,
776 $mailing,
777 $job_date
778 );
779
780 if ($retryGroup) {
781 return FALSE;
782 }
783
784 return $result;
785 }
786
787 /**
788 * Determine if an SMTP error is temporary or permanent.
789 *
790 * @param string $message
791 * PEAR error message.
792 * @return bool
793 * TRUE - Temporary/retriable error
794 * FALSE - Permanent/non-retriable error
795 */
796 protected function isTemporaryError($message) {
797 // SMTP response code is buried in the message.
798 $code = preg_match('/ \(code: (.+), response: /', $message, $matches) ? $matches[1] : '';
799
800 if (strpos($message, 'Failed to write to socket') !== FALSE) {
801 return TRUE;
802 }
803
804 // Register 5xx SMTP response code (permanent failure) as bounce.
805 if (isset($code{0}) && $code{0} === '5') {
806 return FALSE;
807 }
808
809 if (strpos($message, 'Failed to set sender') !== FALSE) {
810 return TRUE;
811 }
812
813 if (strpos($message, 'Failed to add recipient') !== FALSE) {
814 return TRUE;
815 }
816
817 if (strpos($message, 'Failed to send data') !== FALSE) {
818 return TRUE;
819 }
820
821 return FALSE;
822 }
823
824 /**
825 * Cancel a mailing.
826 *
827 * @param int $mailingId
828 * The id of the mailing to be canceled.
829 */
830 public static function cancel($mailingId) {
831 $sql = "
832 SELECT *
833 FROM civicrm_mailing_job
834 WHERE mailing_id = %1
835 AND is_test = 0
836 AND ( ( job_type IS NULL ) OR
837 job_type <> 'child' )
838 ";
839 $params = [1 => [$mailingId, 'Integer']];
840 $job = CRM_Core_DAO::executeQuery($sql, $params);
841 if ($job->fetch() &&
842 in_array($job->status, ['Scheduled', 'Running', 'Paused'])
843 ) {
844
845 self::create(['id' => $job->id, 'end_date' => date('YmdHis'), 'status' => 'Canceled']);
846
847 // also cancel all child jobs
848 $sql = "
849 UPDATE civicrm_mailing_job
850 SET status = 'Canceled',
851 end_date = %2
852 WHERE parent_id = %1
853 AND is_test = 0
854 AND job_type = 'child'
855 AND status IN ( 'Scheduled', 'Running', 'Paused' )
856 ";
857 $params = [
858 1 => [$job->id, 'Integer'],
859 2 => [date('YmdHis'), 'Timestamp'],
860 ];
861 CRM_Core_DAO::executeQuery($sql, $params);
862 }
863 }
864
865 /**
866 * Pause a mailing
867 *
868 * @param int $mailingID
869 * The id of the mailing to be paused.
870 */
871 public static function pause($mailingID) {
872 $sql = "
873 UPDATE civicrm_mailing_job
874 SET status = 'Paused'
875 WHERE mailing_id = %1
876 AND is_test = 0
877 AND status IN ('Scheduled', 'Running')
878 ";
879 CRM_Core_DAO::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
880 }
881
882 /**
883 * Resume a mailing
884 *
885 * @param int $mailingID
886 * The id of the mailing to be resumed.
887 */
888 public static function resume($mailingID) {
889 $sql = "
890 UPDATE civicrm_mailing_job
891 SET status = 'Scheduled'
892 WHERE mailing_id = %1
893 AND is_test = 0
894 AND start_date IS NULL
895 AND status = 'Paused'
896 ";
897 CRM_Core_DAO::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
898
899 $sql = "
900 UPDATE civicrm_mailing_job
901 SET status = 'Running'
902 WHERE mailing_id = %1
903 AND is_test = 0
904 AND start_date IS NOT NULL
905 AND status = 'Paused'
906 ";
907 CRM_Core_DAO::executeQuery($sql, [1 => [$mailingID, 'Integer']]);
908 }
909
910 /**
911 * Return a translated status enum string.
912 *
913 * @param string $status
914 * The status enum.
915 *
916 * @return string
917 * The translated version
918 */
919 public static function status($status) {
920 static $translation = NULL;
921
922 if (empty($translation)) {
923 $translation = [
924 'Scheduled' => ts('Scheduled'),
925 'Running' => ts('Running'),
926 'Complete' => ts('Complete'),
927 'Paused' => ts('Paused'),
928 'Canceled' => ts('Canceled'),
929 ];
930 }
931 return CRM_Utils_Array::value($status, $translation, ts('Not scheduled'));
932 }
933
934 /**
935 * Return a workflow clause for use in SQL queries,
936 * to only process jobs that are approved.
937 *
938 * @return string
939 * For use in a WHERE clause
940 */
941 public static function workflowClause() {
942 // add an additional check and only process
943 // jobs that are approved
944 if (CRM_Mailing_Info::workflowEnabled()) {
945 $approveOptionID = CRM_Core_PseudoConstant::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'Approved');
946 if ($approveOptionID) {
947 return " AND m.approval_status_id = $approveOptionID ";
948 }
949 }
950 return '';
951 }
952
953 /**
954 * @param array $deliveredParams
955 * @param array $targetParams
956 * @param $mailing
957 * @param $job_date
958 *
959 * @return bool
960 * @throws CRM_Core_Exception
961 * @throws Exception
962 */
963 public function writeToDB(
964 &$deliveredParams,
965 &$targetParams,
966 &$mailing,
967 $job_date
968 ) {
969 static $activityTypeID = NULL;
970 static $writeActivity = NULL;
971
972 if (!empty($deliveredParams)) {
973 CRM_Mailing_Event_BAO_Delivered::bulkCreate($deliveredParams);
974 $deliveredParams = [];
975 }
976
977 if ($writeActivity === NULL) {
978 $writeActivity = Civi::settings()->get('write_activity_record');
979 }
980
981 if (!$writeActivity) {
982 return TRUE;
983 }
984
985 $result = TRUE;
986 if (!empty($targetParams) && !empty($mailing->scheduled_id)) {
987 if (!$activityTypeID) {
988 if ($mailing->sms_provider_id) {
989 $mailing->subject = $mailing->name;
990 $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Mass SMS'
991 );
992 }
993 else {
994 $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
995 }
996 if (!$activityTypeID) {
997 CRM_Core_Error::fatal();
998 }
999 }
1000
1001 $activity = [
1002 'source_contact_id' => $mailing->scheduled_id,
1003 // CRM-9519
1004 'target_contact_id' => array_unique($targetParams),
1005 'activity_type_id' => $activityTypeID,
1006 'source_record_id' => $this->mailing_id,
1007 'activity_date_time' => $job_date,
1008 'subject' => $mailing->subject,
1009 'status_id' => 2,
1010 'deleteActivityTarget' => FALSE,
1011 'campaign_id' => $mailing->campaign_id,
1012 ];
1013
1014 //check whether activity is already created for this mailing.
1015 //if yes then create only target contact record.
1016 $query = "
1017 SELECT id
1018 FROM civicrm_activity
1019 WHERE civicrm_activity.activity_type_id = %1
1020 AND civicrm_activity.source_record_id = %2
1021 ";
1022
1023 $queryParams = [
1024 1 => [$activityTypeID, 'Integer'],
1025 2 => [$this->mailing_id, 'Integer'],
1026 ];
1027 $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams);
1028
1029 if ($activityID) {
1030 $activity['id'] = $activityID;
1031
1032 // CRM-9519
1033 if (CRM_Core_BAO_Email::isMultipleBulkMail()) {
1034 static $targetRecordID = NULL;
1035 if (!$targetRecordID) {
1036 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
1037 $targetRecordID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
1038 }
1039
1040 // make sure we don't attempt to duplicate the target activity
1041 foreach ($activity['target_contact_id'] as $key => $targetID) {
1042 $sql = "
1043 SELECT id
1044 FROM civicrm_activity_contact
1045 WHERE activity_id = $activityID
1046 AND contact_id = $targetID
1047 AND record_type_id = $targetRecordID
1048 ";
1049 if (CRM_Core_DAO::singleValueQuery($sql)) {
1050 unset($activity['target_contact_id'][$key]);
1051 }
1052 }
1053 }
1054 }
1055
1056 if (is_a(CRM_Activity_BAO_Activity::create($activity), 'CRM_Core_Error')) {
1057 $result = FALSE;
1058 }
1059
1060 $targetParams = [];
1061 }
1062
1063 return $result;
1064 }
1065
1066 /**
1067 * Search the mailing-event queue for a list of pending delivery tasks.
1068 *
1069 * @param int $jobId
1070 * @param string $medium
1071 * Ex: 'email' or 'sms'.
1072 *
1073 * @return \CRM_Mailing_Event_BAO_Queue
1074 * A query object whose rows provide ('id', 'contact_id', 'hash') and ('email' or 'phone').
1075 */
1076 public static function findPendingTasks($jobId, $medium) {
1077 $eq = new CRM_Mailing_Event_BAO_Queue();
1078 $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
1079 $emailTable = CRM_Core_BAO_Email::getTableName();
1080 $phoneTable = CRM_Core_BAO_Phone::getTableName();
1081 $contactTable = CRM_Contact_BAO_Contact::getTableName();
1082 $deliveredTable = CRM_Mailing_Event_BAO_Delivered::getTableName();
1083 $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
1084
1085 $query = " SELECT $queueTable.id,
1086 $emailTable.email as email,
1087 $queueTable.contact_id,
1088 $queueTable.hash,
1089 NULL as phone
1090 FROM $queueTable
1091 INNER JOIN $emailTable
1092 ON $queueTable.email_id = $emailTable.id
1093 INNER JOIN $contactTable
1094 ON $contactTable.id = $emailTable.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
1104 if ($medium === 'sms') {
1105 $query = "
1106 SELECT $queueTable.id,
1107 $phoneTable.phone as phone,
1108 $queueTable.contact_id,
1109 $queueTable.hash,
1110 NULL as email
1111 FROM $queueTable
1112 INNER JOIN $phoneTable
1113 ON $queueTable.phone_id = $phoneTable.id
1114 INNER JOIN $contactTable
1115 ON $contactTable.id = $phoneTable.contact_id
1116 LEFT JOIN $deliveredTable
1117 ON $queueTable.id = $deliveredTable.event_queue_id
1118 LEFT JOIN $bounceTable
1119 ON $queueTable.id = $bounceTable.event_queue_id
1120 WHERE $queueTable.job_id = " . $jobId . "
1121 AND $deliveredTable.id IS null
1122 AND $bounceTable.id IS null
1123 AND ( $contactTable.is_opt_out = 0
1124 OR $contactTable.do_not_sms = 0 )";
1125 }
1126 $eq->query($query);
1127 return $eq;
1128 }
1129
1130 /**
1131 * Delete the mailing job.
1132 *
1133 * @param int $id
1134 * Mailing Job id.
1135 *
1136 * @return mixed
1137 */
1138 public static function del($id) {
1139 CRM_Utils_Hook::pre('delete', 'MailingJob', $id, CRM_Core_DAO::$_nullArray);
1140
1141 $jobDAO = new CRM_Mailing_BAO_MailingJob();
1142 $jobDAO->id = $id;
1143 $result = $jobDAO->delete();
1144
1145 CRM_Utils_Hook::post('delete', 'MailingJob', $jobDAO->id, $jobDAO);
1146
1147 return $result;
1148 }
1149
1150 }