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