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