ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
ScheduledMailsCron.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Mail\Cron;
22
23use ilMail;
24use Generator;
25use ilContext;
26use ilObjUser;
27use Throwable;
28use ilLanguage;
29use DateTimeZone;
30use ilFormatMail;
34use DateTimeImmutable;
39use ILIAS\Data\Factory as DataFactory;
42use ILIAS\Mail\Folder\OutboxDatabaseRepository;
45
47{
48 private readonly ilLanguage $lng;
49 private readonly ilObjUser $user;
50 private bool $init_done = false;
51 private readonly ilMail $mail;
52 private readonly ilFormatMail $umail;
54
55 private function init(): void
56 {
57 global $DIC;
58
59 if (!$this->init_done) {
60 $this->lng = $DIC->language();
61 $this->user = $DIC->user();
62 $this->mail = new ilMail($this->user->getId());
63 $this->umail = new ilFormatMail($this->user->getId());
64
65 $this->lng->loadLanguageModule('mail');
66 $this->init_done = true;
67 $this->outbox_repository = new OutboxDatabaseRepository(
68 $DIC->database(),
69 (new DataFactory())->clock(),
70 $this->mail
71 );
72 }
73 }
74
75 public function getId(): string
76 {
77 return 'mail_scheduled_mails';
78 }
79
80 public function getTitle(): string
81 {
82 $this->init();
83
84 return $this->lng->txt('mail_cron_scheduled_mails');
85 }
86
87 public function getDescription(): string
88 {
89 $this->init();
90
91 return $this->lng->txt('mail_cron_scheduled_mails_desc');
92 }
93
94 public function hasAutoActivation(): bool
95 {
96 return true;
97 }
98
99 public function hasFlexibleSchedule(): bool
100 {
101 return true;
102 }
103
105 {
106 return JobScheduleType::DAILY;
107 }
108
109 public function getDefaultScheduleValue(): ?int
110 {
111 return 1;
112 }
113
114 public function run(): JobResult
115 {
116 $this->init();
117
118 $job_result = new JobResult();
119 $job_result->setStatus(JobResult::STATUS_OK);
120
121 ilLoggerFactory::getLogger('mail')->info('Start sending scheduled mails from all users.');
122
123 $mails = $this->outbox_repository->getOutboxMails();
124 $sent_mail_ids = [];
125 foreach ($mails as $mail) {
127 try {
128 $mailer = $this->umail
129 ->withContextId(ilContext::CONTEXT_CRON);
130
131 $mailer->setSaveInSentbox(true);
132
133 $mailer->autoresponder()->enableAutoresponder();
134 $errors = $mailer->enqueue(
135 $mail->getTo(),
136 $mail->getCc(),
137 $mail->getBcc(),
138 $mail->getSubject(),
139 $mail->getMessage(),
140 $mail->getAttachments(),
141 $mail->isUsePlaceholder()
142 );
143
144 if (empty($errors)) {
145 $sent_mail_ids[] = $mail->getInternalMailId();
146 }
147 } catch (Throwable $e) {
148 $job_result->setStatus(JobResult::STATUS_FAIL);
149 ilLoggerFactory::getLogger('mail')->error(
150 'Error sending scheduled mail with id ' . ((string) ($mail->getInternalMailId() ?? 'unknown')) . ': ' .
151 $e->getMessage() . '\n' . $e->getTraceAsString()
152 );
153 $job_result->setMessage(substr($e->getMessage() . ' ' . $e->getTraceAsString(), 0, 4000));
154
155 return $job_result;
156 }
157 }
158 $this->mail->deleteMails($sent_mail_ids);
159 ilLoggerFactory::getLogger('mail')->info(
160 'Sent ' . count($sent_mail_ids) . ' scheduled mails and removed them from outbox.'
161 );
162 $job_result->setMessage('Processed ' . count($sent_mail_ids) . ' mails.');
163
164 return $job_result;
165 }
166}
final const int STATUS_FAIL
Definition: JobResult.php:30
final const int STATUS_OK
Definition: JobResult.php:27
Builds data types.
Definition: Factory.php:36
hasAutoActivation()
Is to be activated on "installation", does only work for ILIAS core cron jobs.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CONTEXT_CRON
Class ilDBConstants.
language handling
static getLogger(string $a_component_id)
Get component logger.
User class.
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26