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