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