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