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