Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | require_once 'Mail.php'; | |
4c6ce474 EM |
35 | |
36 | /** | |
37 | * Class CRM_Mailing_BAO_MailingJob | |
38 | */ | |
9da8dc8c | 39 | class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob { |
7da04cde | 40 | const MAX_CONTACTS_TO_PROCESS = 1000; |
6a488035 | 41 | |
97b7d4a0 | 42 | /** |
a335f6b2 | 43 | * (Dear God Why) Keep a global count of mails processed within the current |
97b7d4a0 TO |
44 | * request. |
45 | * | |
97b7d4a0 TO |
46 | * @var int |
47 | */ | |
48 | static $mailsProcessed = 0; | |
49 | ||
6a488035 | 50 | /** |
fe482240 | 51 | * Class constructor. |
6a488035 | 52 | */ |
00be9182 | 53 | public function __construct() { |
6a488035 TO |
54 | parent::__construct(); |
55 | } | |
56 | ||
af4c09bc | 57 | /** |
c490a46a | 58 | * @param array $params |
af4c09bc EM |
59 | * |
60 | * @return CRM_Mailing_BAO_MailingJob | |
61 | */ | |
84dd5db6 | 62 | static public function create($params) { |
9da8dc8c | 63 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
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(); | |
85d20752 TO |
70 | $mailing->id = $params['mailing_id']; |
71 | if ($mailing->id && $mailing->find(TRUE)) { | |
f5d5729b | 72 | $mailing->getRecipients($job->id, $params['mailing_id'], TRUE, $mailing->dedupe_email); |
85d20752 TO |
73 | return $job; |
74 | } | |
75 | else { | |
76 | throw new CRM_Core_Exception("Failed to create job: Unknown mailing ID"); | |
77 | } | |
6a488035 | 78 | } |
af4c09bc | 79 | |
6a488035 TO |
80 | /** |
81 | * Initiate all pending/ready jobs | |
82 | * | |
100fef9d | 83 | * @param array $testParams |
af4c09bc | 84 | * @param null $mode |
6a488035 TO |
85 | */ |
86 | public static function runJobs($testParams = NULL, $mode = NULL) { | |
9da8dc8c | 87 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 | 88 | |
353ffa53 TO |
89 | $config = CRM_Core_Config::singleton(); |
90 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); | |
6a488035 TO |
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'); | |
353ffa53 TO |
102 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); |
103 | $domainID = CRM_Core_Config::domainID(); | |
6a488035 TO |
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 | ||
6a488035 TO |
133 | while ($job->fetch()) { |
134 | // still use job level lock for each child job | |
83617886 | 135 | $lock = Civi::lockManager()->acquire("data.mailing.job.{$job->id}"); |
6a488035 TO |
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 | |
1a5727bd | 145 | $job->status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 146 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 147 | $job->id, |
6606b2e1 DL |
148 | 'status', |
149 | 'id', | |
150 | TRUE | |
6a488035 TO |
151 | ); |
152 | ||
1a5727bd DL |
153 | if ( |
154 | $job->status != 'Running' && | |
6a488035 TO |
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 | |
353ffa53 TO |
173 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
174 | $saveJob->id = $job->id; | |
6a488035 | 175 | $saveJob->start_date = date('YmdHis'); |
353ffa53 | 176 | $saveJob->status = 'Running'; |
6a488035 TO |
177 | $saveJob->save(); |
178 | ||
179 | $transaction->commit(); | |
180 | } | |
181 | ||
182 | // Get the mailer | |
6a488035 | 183 | if ($mode === NULL) { |
048222df | 184 | $mailer = \Civi::service('pear_mail'); |
6a488035 TO |
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) { | |
25606795 | 197 | // Finish the job. |
6a488035 TO |
198 | |
199 | $transaction = new CRM_Core_Transaction(); | |
200 | ||
353ffa53 TO |
201 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
202 | $saveJob->id = $job->id; | |
6a488035 | 203 | $saveJob->end_date = date('YmdHis'); |
353ffa53 | 204 | $saveJob->status = 'Complete'; |
6a488035 TO |
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 | ||
e0ef6999 | 221 | /** |
25606795 SB |
222 | * Post process to determine if the parent job |
223 | * as well as the mailing is complete after the run. | |
e0ef6999 EM |
224 | * @param null $mode |
225 | */ | |
6a488035 TO |
226 | public static function runJobs_post($mode = NULL) { |
227 | ||
9da8dc8c | 228 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
229 | |
230 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
231 | ||
353ffa53 TO |
232 | $config = CRM_Core_Config::singleton(); |
233 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); | |
6a488035 TO |
234 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
235 | ||
236 | $currentTime = date('YmdHis'); | |
353ffa53 TO |
237 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); |
238 | $domainID = CRM_Core_Config::domainID(); | |
6a488035 TO |
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 | ||
9da8dc8c | 258 | $child_job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
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 | ||
353ffa53 TO |
277 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
278 | $saveJob->id = $job->id; | |
6a488035 | 279 | $saveJob->end_date = date('YmdHis'); |
353ffa53 | 280 | $saveJob->status = 'Complete'; |
6a488035 TO |
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 | ||
e0ef6999 | 293 | /** |
4f1f1f2a | 294 | * before we run jobs, we need to split the jobs |
e0ef6999 EM |
295 | * @param int $offset |
296 | * @param null $mode | |
297 | */ | |
6a488035 | 298 | public static function runJobs_pre($offset = 200, $mode = NULL) { |
9da8dc8c | 299 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 | 300 | |
353ffa53 | 301 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 TO |
302 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
303 | ||
304 | $currentTime = date('YmdHis'); | |
305 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); | |
306 | ||
9da8dc8c | 307 | $workflowClause = CRM_Mailing_BAO_MailingJob::workflowClause(); |
6a488035 TO |
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 | ||
6a488035 TO |
334 | $job->query($query); |
335 | ||
6a488035 TO |
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 | |
83617886 | 340 | $lock = Civi::lockManager()->acquire("data.mailing.job.{$job->id}"); |
6a488035 TO |
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 | |
6606b2e1 | 348 | $job->status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 349 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 350 | $job->id, |
6606b2e1 DL |
351 | 'status', |
352 | 'id', | |
353 | TRUE | |
6a488035 TO |
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 | ||
353ffa53 TO |
365 | $saveJob = new CRM_Mailing_DAO_MailingJob(); |
366 | $saveJob->id = $job->id; | |
6a488035 | 367 | $saveJob->start_date = date('YmdHis'); |
353ffa53 | 368 | $saveJob->status = 'Running'; |
6a488035 TO |
369 | $saveJob->save(); |
370 | ||
371 | $transaction->commit(); | |
372 | ||
373 | // Release the job lock | |
374 | $lock->release(); | |
375 | } | |
376 | } | |
377 | ||
e0ef6999 | 378 | /** |
fe482240 | 379 | * Split the parent job into n number of child job based on an offset. |
4f1f1f2a | 380 | * If null or 0 , we create only one child job |
e0ef6999 EM |
381 | * @param int $offset |
382 | */ | |
6a488035 TO |
383 | public function split_job($offset = 200) { |
384 | $recipient_count = CRM_Mailing_BAO_Recipients::mailingSize($this->mailing_id); | |
385 | ||
9da8dc8c | 386 | $jobTable = CRM_Mailing_DAO_MailingJob::getTableName(); |
6a488035 | 387 | |
6a488035 TO |
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 | "; | |
35f7561f | 395 | $params = array( |
353ffa53 | 396 | 1 => array($this->mailing_id, 'Integer'), |
6a488035 TO |
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 | |
cbe30f5a | 406 | // probably use a CRM_Mailing_DAO_MailingJob( ); |
6a488035 TO |
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 | ||
e0ef6999 | 422 | /** |
100fef9d | 423 | * @param array $testParams |
e0ef6999 | 424 | */ |
6a488035 TO |
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 | |
353ffa53 | 439 | $now = time(); |
6a488035 | 440 | $params = array(); |
353ffa53 | 441 | $count = 0; |
6a488035 TO |
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++; | |
dfb542fc | 457 | if ($count % CRM_Mailing_Config::BULK_MAIL_INSERT_COUNT == 0) { |
6a488035 TO |
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 | /** | |
fe482240 | 471 | * Send the mailing. |
6a488035 | 472 | * |
90c8230e TO |
473 | * @param object $mailer |
474 | * A Mail object to send the messages. | |
2a6da8d7 | 475 | * |
100fef9d | 476 | * @param array $testParams |
6a488035 TO |
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 | ||
353ffa53 TO |
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(); | |
6a488035 | 488 | $contactTable = CRM_Contact_BAO_Contact::getTableName(); |
353ffa53 TO |
489 | $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName(); |
490 | $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); | |
6a488035 TO |
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 | |
7a9646c1 | 530 | AND ( $contactTable.is_opt_out = 0 |
531 | OR $contactTable.do_not_sms = 0 )"; | |
6a488035 TO |
532 | } |
533 | $eq->query($query); | |
534 | ||
97b7d4a0 | 535 | $config = NULL; |
6a488035 TO |
536 | |
537 | if ($config == NULL) { | |
538 | $config = CRM_Core_Config::singleton(); | |
539 | } | |
540 | ||
a7c57397 TO |
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); | |
fd1f3a26 SV |
543 | } |
544 | ||
6a488035 TO |
545 | $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date); |
546 | $fields = array(); | |
547 | ||
548 | if (!empty($testParams)) { | |
a5db8e6f | 549 | $mailing->subject = ts('[CiviMail Draft]') . ' ' . $mailing->subject; |
6a488035 TO |
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 | ||
df7f4ae1 DL |
561 | // CRM-12376 |
562 | // This handles the edge case scenario where all the mails | |
25606795 | 563 | // have been delivered in prior jobs. |
df7f4ae1 | 564 | $isDelivered = TRUE; |
6a488035 | 565 | |
dc00ac6d TO |
566 | // make sure that there's no more than $mailerBatchLimit mails processed in a run |
567 | $mailerBatchLimit = Civi::settings()->get('mailerBatchLimit'); | |
6a488035 TO |
568 | while ($eq->fetch()) { |
569 | // if ( ( $mailsProcessed % 100 ) == 0 ) { | |
570 | // CRM_Utils_System::xMemory( "$mailsProcessed: " ); | |
571 | // } | |
572 | ||
dc00ac6d | 573 | if ($mailerBatchLimit > 0 && self::$mailsProcessed >= $mailerBatchLimit) { |
6a488035 TO |
574 | if (!empty($fields)) { |
575 | $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); | |
576 | } | |
577 | $eq->free(); | |
578 | return FALSE; | |
579 | } | |
97b7d4a0 | 580 | self::$mailsProcessed++; |
6a488035 TO |
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 | ||
e0ef6999 | 607 | /** |
519947b8 TO |
608 | * @param array $fields |
609 | * List of intended recipients. | |
610 | * Each recipient is an array with keys 'hash', 'contact_id', 'email', etc. | |
e0ef6999 EM |
611 | * @param $mailing |
612 | * @param $mailer | |
613 | * @param $job_date | |
614 | * @param $attachments | |
615 | * | |
616 | * @return bool|null | |
617 | * @throws Exception | |
618 | */ | |
6a488035 TO |
619 | public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) { |
620 | static $smtpConnectionErrors = 0; | |
621 | ||
1a5727bd | 622 | if (!is_object($mailer) || empty($fields)) { |
6a488035 TO |
623 | CRM_Core_Error::fatal(); |
624 | } | |
625 | ||
626 | // get the return properties | |
627 | $returnProperties = $mailing->getReturnProperties(); | |
353ffa53 TO |
628 | $params = $targetParams = $deliveredParams = array(); |
629 | $count = 0; | |
7a9646c1 | 630 | |
3d9b94a1 SB |
631 | // CRM-15702: Sending bulk sms to contacts without e-mail address fails. |
632 | // Solution is to skip checking for on hold | |
25606795 | 633 | $skipOnHold = TRUE; //do include a statement to check wether e-mail address is on hold |
e36508bf | 634 | if ($mailing->sms_provider_id) { |
35f7561f | 635 | $skipOnHold = FALSE; //do not include a statement to check wether e-mail address is on hold |
e36508bf | 636 | } |
6a488035 TO |
637 | |
638 | foreach ($fields as $key => $field) { | |
639 | $params[] = $field['contact_id']; | |
640 | } | |
641 | ||
642 | $details = CRM_Utils_Token::getTokenDetails( | |
643 | $params, | |
644 | $returnProperties, | |
e36508bf | 645 | $skipOnHold, TRUE, NULL, |
6a488035 TO |
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 | } | |
6a488035 | 657 | |
25606795 | 658 | // Compose the mailing. |
6a488035 TO |
659 | $recipient = $replyToEmail = NULL; |
660 | $replyValue = strcmp($mailing->replyto_email, $mailing->from_email); | |
661 | if ($replyValue) { | |
662 | $replyToEmail = $mailing->replyto_email; | |
663 | } | |
664 | ||
7eebf0c7 | 665 | $message = $mailing->compose( |
1a5727bd | 666 | $this->id, $field['id'], $field['hash'], |
6a488035 TO |
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 | ||
25606795 | 678 | // Send the mailing. |
6a488035 TO |
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)); | |
353ffa53 TO |
685 | $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]); |
686 | $headers = $provider->getRecipientDetails($field, $details[0][$contactID]); | |
6a488035 TO |
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) { | |
6a4257d4 | 695 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 TO |
696 | } |
697 | ||
698 | $result = $mailer->send($recipient, $headers, $body, $this->id); | |
699 | ||
700 | if ($job_date) { | |
6a4257d4 | 701 | unset($errorScope); |
6a488035 TO |
702 | } |
703 | ||
c5a6413b | 704 | if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) { |
6a488035 TO |
705 | // CRM-9191 |
706 | $message = $result->getMessage(); | |
18810158 DL |
707 | if ( |
708 | strpos($message, 'Failed to write to socket') !== FALSE || | |
709 | strpos($message, 'Failed to set sender') !== FALSE | |
710 | ) { | |
6a488035 TO |
711 | // lets log this message and code |
712 | $code = $result->getCode(); | |
18810158 | 713 | CRM_Core_Error::debug_log_message("SMTP Socket Error or failed to set sender error. Message: $message, Code: $code"); |
6a488035 TO |
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 | ||
6a488035 TO |
722 | // seems like we have too many of them in a row, we should |
723 | // write stuff to disk and abort the cron job | |
2ede60ec DL |
724 | $this->writeToDB( |
725 | $deliveredParams, | |
6a488035 TO |
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 | ||
25606795 | 735 | // Register the bounce event. |
6a488035 TO |
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 | } | |
39373d13 JM |
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 | } | |
6a488035 | 754 | else { |
25606795 | 755 | // Register the delivery event. |
6a488035 TO |
756 | $deliveredParams[] = $field['id']; |
757 | $targetParams[] = $field['contact_id']; | |
758 | ||
759 | $count++; | |
dfb542fc | 760 | if ($count % CRM_Mailing_Config::BULK_MAIL_INSERT_COUNT == 0) { |
2ede60ec DL |
761 | $this->writeToDB( |
762 | $deliveredParams, | |
6a488035 TO |
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 | |
1a5727bd | 772 | $status = CRM_Core_DAO::getFieldValue( |
cbe30f5a | 773 | 'CRM_Mailing_DAO_MailingJob', |
6a488035 | 774 | $this->id, |
1a5727bd DL |
775 | 'status', |
776 | 'id', | |
777 | TRUE | |
6a488035 | 778 | ); |
1a5727bd | 779 | |
6a488035 TO |
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. | |
dc00ac6d TO |
795 | $mailThrottleTime = Civi::settings()->get('mailThrottleTime'); |
796 | if (!empty($mailThrottleTime)) { | |
797 | usleep((int ) $mailThrottleTime); | |
6a488035 TO |
798 | } |
799 | } | |
800 | ||
2ede60ec DL |
801 | $result = $this->writeToDB( |
802 | $deliveredParams, | |
6a488035 TO |
803 | $targetParams, |
804 | $mailing, | |
805 | $job_date | |
806 | ); | |
807 | ||
808 | return $result; | |
809 | } | |
810 | ||
811 | /** | |
fe482240 | 812 | * Cancel a mailing. |
6a488035 | 813 | * |
90c8230e TO |
814 | * @param int $mailingId |
815 | * The id of the mailing to be canceled. | |
6a488035 TO |
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 | ||
353ffa53 TO |
832 | $newJob = new CRM_Mailing_BAO_MailingJob(); |
833 | $newJob->id = $job->id; | |
6a488035 | 834 | $newJob->end_date = date('YmdHis'); |
353ffa53 | 835 | $newJob->status = 'Canceled'; |
6a488035 TO |
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 | "; | |
35f7561f | 848 | $params = array( |
353ffa53 | 849 | 1 => array($job->id, 'Integer'), |
6a488035 TO |
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 | /** | |
fe482240 | 859 | * Return a translated status enum string. |
6a488035 | 860 | * |
90c8230e TO |
861 | * @param string $status |
862 | * The status enum. | |
6a488035 | 863 | * |
a6c01b45 CW |
864 | * @return string |
865 | * The translated version | |
6a488035 TO |
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 | * | |
a6c01b45 CW |
886 | * @return string |
887 | * For use in a WHERE clause | |
6a488035 TO |
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 | ||
e0ef6999 | 904 | /** |
100fef9d CW |
905 | * @param array $deliveredParams |
906 | * @param array $targetParams | |
e0ef6999 EM |
907 | * @param $mailing |
908 | * @param $job_date | |
909 | * | |
910 | * @return bool | |
911 | * @throws CRM_Core_Exception | |
912 | * @throws Exception | |
913 | */ | |
2ede60ec DL |
914 | public function writeToDB( |
915 | &$deliveredParams, | |
6a488035 TO |
916 | &$targetParams, |
917 | &$mailing, | |
918 | $job_date | |
919 | ) { | |
920 | static $activityTypeID = NULL; | |
2ede60ec | 921 | static $writeActivity = NULL; |
6a488035 TO |
922 | |
923 | if (!empty($deliveredParams)) { | |
924 | CRM_Mailing_Event_BAO_Delivered::bulkCreate($deliveredParams); | |
925 | $deliveredParams = array(); | |
926 | } | |
927 | ||
2ede60ec | 928 | if ($writeActivity === NULL) { |
aaffa79f | 929 | $writeActivity = Civi::settings()->get('write_activity_record'); |
2ede60ec | 930 | } |
6a488035 | 931 | |
2ede60ec DL |
932 | if (!$writeActivity) { |
933 | return TRUE; | |
934 | } | |
935 | ||
936 | $result = TRUE; | |
937 | if (!empty($targetParams) && !empty($mailing->scheduled_id)) { | |
6a488035 | 938 | if (!$activityTypeID) { |
6a488035 TO |
939 | if ($mailing->sms_provider_id) { |
940 | $mailing->subject = $mailing->name; | |
2ede60ec DL |
941 | $activityTypeID = CRM_Core_OptionGroup::getValue( |
942 | 'activity_type', | |
6a488035 TO |
943 | 'Mass SMS', |
944 | 'name' | |
945 | ); | |
946 | } | |
2ede60ec DL |
947 | else { |
948 | $activityTypeID = CRM_Core_OptionGroup::getValue( | |
949 | 'activity_type', | |
950 | 'Bulk Email', | |
951 | 'name' | |
952 | ); | |
953 | } | |
6a488035 TO |
954 | if (!$activityTypeID) { |
955 | CRM_Core_Error::fatal(); | |
956 | } | |
957 | } | |
958 | ||
6a488035 TO |
959 | $activity = array( |
960 | 'source_contact_id' => $mailing->scheduled_id, | |
961 | // CRM-9519 | |
962 | 'target_contact_id' => array_unique($targetParams), | |
6a488035 TO |
963 | 'activity_type_id' => $activityTypeID, |
964 | 'source_record_id' => $this->mailing_id, | |
965 | 'activity_date_time' => $job_date, | |
966 | 'subject' => $mailing->subject, | |
967 | 'status_id' => 2, | |
968 | 'deleteActivityTarget' => FALSE, | |
969 | 'campaign_id' => $mailing->campaign_id, | |
970 | ); | |
971 | ||
972 | //check whether activity is already created for this mailing. | |
973 | //if yes then create only target contact record. | |
974 | $query = " | |
975 | SELECT id | |
976 | FROM civicrm_activity | |
977 | WHERE civicrm_activity.activity_type_id = %1 | |
2ede60ec DL |
978 | AND civicrm_activity.source_record_id = %2 |
979 | "; | |
6a488035 | 980 | |
2ede60ec DL |
981 | $queryParams = array( |
982 | 1 => array($activityTypeID, 'Integer'), | |
6a488035 TO |
983 | 2 => array($this->mailing_id, 'Integer'), |
984 | ); | |
2ede60ec | 985 | $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams); |
6a488035 TO |
986 | |
987 | if ($activityID) { | |
988 | $activity['id'] = $activityID; | |
989 | ||
990 | // CRM-9519 | |
991 | if (CRM_Core_BAO_Email::isMultipleBulkMail()) { | |
b561230a DL |
992 | static $targetRecordID = NULL; |
993 | if (!$targetRecordID) { | |
994 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); | |
995 | $targetRecordID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
996 | } | |
997 | ||
6a488035 TO |
998 | // make sure we don't attempt to duplicate the target activity |
999 | foreach ($activity['target_contact_id'] as $key => $targetID) { | |
b561230a DL |
1000 | $sql = " |
1001 | SELECT id | |
1002 | FROM civicrm_activity_contact | |
1003 | WHERE activity_id = $activityID | |
1004 | AND contact_id = $targetID | |
1005 | AND record_type_id = $targetRecordID | |
1006 | "; | |
6a488035 TO |
1007 | if (CRM_Core_DAO::singleValueQuery($sql)) { |
1008 | unset($activity['target_contact_id'][$key]); | |
1009 | } | |
1010 | } | |
1011 | } | |
1012 | } | |
1013 | ||
2ede60ec | 1014 | if (is_a(CRM_Activity_BAO_Activity::create($activity), 'CRM_Core_Error')) { |
6a488035 TO |
1015 | $result = FALSE; |
1016 | } | |
1017 | ||
1018 | $targetParams = array(); | |
1019 | } | |
1020 | ||
1021 | return $result; | |
1022 | } | |
96025800 | 1023 | |
6a488035 | 1024 | } |