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