ILIAS  release_8 Revision v8.24
class.ilAccountMail.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\Refinery\Factory as Refinery;
23
34{
37 private Refinery $refinery;
40 public string $u_password = '';
41 public ?ilObjUser $user = null;
42 public string $target = '';
43 private bool $lang_variables_as_fallback = false;
45 private array $attachments = [];
46 private bool $attachConfiguredFiles = false;
47 private array $amail = [];
48
49 public function __construct()
50 {
51 global $DIC;
52 $this->http = $DIC->http();
53 $this->refinery = $DIC->refinery();
54 $this->settings = $DIC->settings();
55 $this->repositoryTree = $DIC->repositoryTree();
56 $this->senderFactory = $DIC['mail.mime.sender.factory'];
57 }
58
59 public function useLangVariablesAsFallback(bool $a_status): void
60 {
61 $this->lang_variables_as_fallback = $a_status;
62 }
63
64 public function areLangVariablesUsedAsFallback(): bool
65 {
67 }
68
69 public function shouldAttachConfiguredFiles(): bool
70 {
72 }
73
75 {
76 $this->attachConfiguredFiles = $attachConfiguredFiles;
77 }
78
79 public function setUserPassword(string $a_pwd): void
80 {
81 $this->u_password = $a_pwd;
82 }
83
84 public function getUserPassword(): string
85 {
86 return $this->u_password;
87 }
88
89 public function setUser(ilObjUser $a_user): void
90 {
91 if (
92 $this->user instanceof ilObjUser &&
93 $a_user->getId() !== $this->user->getId()
94 ) {
95 $this->attachments = [];
96 }
97
98 $this->user = $a_user;
99 }
100
101 public function getUser(): ?ilObjUser
102 {
103 return $this->user;
104 }
105
106 public function getTarget(): string
107 {
108 return $this->target;
109 }
110
111 public function reset(): void
112 {
113 $this->user = null;
114 $this->u_password = '';
115 $this->target = '';
116 }
117
122 private function ensureValidMailDataShape(array $mailData): array
123 {
124 foreach (['lang', 'subject', 'body', 'sal_f', 'sal_g', 'sal_m', 'type'] as $key) {
125 if (!isset($mailData[$key])) {
126 $mailData[$key] = '';
127 }
128 }
129
130 $mailData['subject'] = trim($mailData['subject']);
131 $mailData['body'] = trim($mailData['body']);
132
133 return $mailData;
134 }
135
139 private function readAccountMail(string $a_lang): array
140 {
141 if (!isset($this->amail[$a_lang]) || !is_array($this->amail[$a_lang])) {
142 $this->amail[$a_lang] = $this->ensureValidMailDataShape(
144 );
145 }
146
147 return $this->amail[$a_lang];
148 }
149
155 private function addAttachments(array $mailData): void
156 {
157 if (isset($mailData['att_file']) && $this->shouldAttachConfiguredFiles()) {
159 $fs->create();
160
161 $pathToFile = '/' . implode('/', array_map(static function (string $pathPart): string {
162 return trim($pathPart, '/');
163 }, [
164 $fs->getAbsolutePath(),
165 $mailData['lang'],
166 ]));
167
168 $this->addAttachment($pathToFile, $mailData['att_file']);
169 }
170 }
171
180 public function send(): bool
181 {
182 $user = $this->getUser();
183 if (null === $user) {
184 throw new RuntimeException('A user instance must be passed when sending emails');
185 }
186
187 if (!$user->getEmail()) {
188 return false;
189 }
190
191 // determine language and get account mail data
192 // fall back to default language if acccount mail data is not given for user language.
193 $amail = $this->readAccountMail($user->getLanguage());
195 if ($amail['body'] === '' || $amail['subject'] === '') {
196 $fallback_language = 'en';
197 $amail = $this->readAccountMail($this->settings->get('language', $fallback_language));
198 $lang = $this->settings->get('language', $fallback_language);
199 }
200
201 // fallback if mail data is still not given
202 if (($amail['body'] === '' || $amail['subject'] === '') && $this->areLangVariablesUsedAsFallback()) {
203 $lang = $user->getLanguage();
204 $tmp_lang = new ilLanguage($lang);
205
206 $mail_subject = $tmp_lang->txt('reg_mail_subject');
207
208 $timelimit = "";
209 if (!$user->checkTimeLimit()) {
210 $tmp_lang->loadLanguageModule("registration");
211
212 // #6098
213 $timelimit_from = new ilDateTime($user->getTimeLimitFrom(), IL_CAL_UNIX);
214 $timelimit_until = new ilDateTime($user->getTimeLimitUntil(), IL_CAL_UNIX);
215 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
216 $timelimit = "\n" . sprintf($tmp_lang->txt('reg_mail_body_timelimit'), $timelimit) . "\n\n";
217 }
218
219 // mail body
220 $mail_body = $tmp_lang->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ",\n\n" .
221 $tmp_lang->txt('reg_mail_body_text1') . "\n\n" .
222 $tmp_lang->txt('reg_mail_body_text2') . "\n" .
223 ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID . "\n";
224 $mail_body .= $tmp_lang->txt('login') . ': ' . $user->getLogin() . "\n";
225 $mail_body .= $tmp_lang->txt('passwd') . ': ' . $this->u_password . "\n";
226 $mail_body .= "\n" . $timelimit;
227 $mail_body .= $tmp_lang->txt('reg_mail_body_text3') . "\n\r";
228 $mail_body .= $user->getProfileAsString($tmp_lang);
229 } else {
230 $this->addAttachments($amail);
231
232 // replace placeholders
233 $mail_subject = $this->replacePlaceholders($amail['subject'], $user, $amail, $lang);
234 $mail_body = $this->replacePlaceholders($amail['body'], $user, $amail, $lang);
235 }
236
237 $mmail = new ilMimeMail();
238 $mmail->From($this->senderFactory->system());
239 $mmail->Subject($mail_subject, true);
240 $mmail->To($user->getEmail());
241 $mmail->Body($mail_body);
242
243 foreach ($this->attachments as $filename => $display_name) {
244 $mmail->Attach($filename, '', 'attachment', $display_name);
245 }
246
247 $mmail->Send();
248
249 return true;
250 }
251
252 public function replacePlaceholders(string $a_string, ilObjUser $a_user, array $a_amail, string $a_lang): string
253 {
254 switch ($a_user->getGender()) {
255 case 'f':
256 $gender_salut = $a_amail['sal_f'];
257 break;
258 case 'm':
259 $gender_salut = $a_amail['sal_m'];
260 break;
261 default:
262 $gender_salut = $a_amail['sal_g'];
263 }
264 $gender_salut = trim($gender_salut);
265
266 $a_string = str_replace(
267 [
268 '[MAIL_SALUTATION]',
269 '[LOGIN]',
270 '[FIRST_NAME]',
271 '[LAST_NAME]',
272 '[EMAIL]',
273 '[PASSWORD]',
274 '[ILIAS_URL]',
275 '[INSTALLATION_NAME]',
276 '[ADMIN_MAIL]',
277 ],
278 [
279 $gender_salut,
280 $a_user->getLogin(),
281 $a_user->getFirstname(),
282 $a_user->getLastname(),
283 $a_user->getEmail(),
284 $this->getUserPassword(),
285 ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID,
287 $this->settings->get('admin_email', ''),
288 ],
289 $a_string
290 );
291
292 // (no) password sections
293 if ($this->getUserPassword() === '') {
294 // #12232
295 $a_string = preg_replace(
296 "/\[IF_PASSWORD\].*\[\/IF_PASSWORD\]/imsU",
297 "",
298 $a_string
299 );
300 $a_string = preg_replace(
301 "/\[IF_NO_PASSWORD\](.*)\[\/IF_NO_PASSWORD\]/imsU",
302 "$1",
303 $a_string
304 );
305 } else {
306 $a_string = preg_replace(
307 "/\[IF_NO_PASSWORD\].*\[\/IF_NO_PASSWORD\]/imsU",
308 "",
309 $a_string
310 );
311 $a_string = preg_replace(
312 "/\[IF_PASSWORD\](.*)\[\/IF_PASSWORD\]/imsU",
313 "$1",
314 $a_string
315 );
316 }
317
318 // #13346
319 if (!$a_user->getTimeLimitUnlimited()) {
320 // #6098
321 $a_string = preg_replace(
322 "/\[IF_TIMELIMIT\](.*)\[\/IF_TIMELIMIT\]/imsU",
323 "$1",
324 $a_string
325 );
326 $timelimit_from = new ilDateTime($a_user->getTimeLimitFrom(), IL_CAL_UNIX);
327 $timelimit_until = new ilDateTime($a_user->getTimeLimitUntil(), IL_CAL_UNIX);
328 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
329 $a_string = str_replace("[TIMELIMIT]", $timelimit, $a_string);
330 } else {
331 $a_string = preg_replace(
332 "/\[IF_TIMELIMIT\](.*)\[\/IF_TIMELIMIT\]/imsU",
333 "",
334 $a_string
335 );
336 }
337
338 // target
339 $tar = false;
340 if ($this->http->wrapper()->query()->has('target') &&
341 $this->http->wrapper()->query()->retrieve('target', $this->refinery->kindlyTo()->string()) !== ''
342 ) {
343 $target = $this->http->wrapper()->query()->retrieve('target', $this->refinery->kindlyTo()->string());
344 $tarr = explode('_', $target);
345 if ($this->repositoryTree->isInTree((int) $tarr[1])) {
346 $obj_id = ilObject::_lookupObjId((int) $tarr[1]);
347 $type = ilObject::_lookupType($obj_id);
348 if ($type === $tarr[0]) {
349 $a_string = str_replace(
350 ['[TARGET_TITLE]', '[TARGET]'],
351 [
352 ilObject::_lookupTitle($obj_id),
353 ILIAS_HTTP_PATH . '/goto.php?client_id=' . CLIENT_ID . '&target=' . $target
354 ],
355 $a_string
356 );
357
358 // this looks complicated, but we may have no initiliased $lng object here
359 // if mail is send during user creation in authentication
360 $a_string = str_replace(
361 '[TARGET_TYPE]',
362 ilLanguage::_lookupEntry($a_lang, 'common', 'obj_' . $tarr[0]),
363 $a_string
364 );
365
366 $tar = true;
367 }
368 }
369 }
370
371 // (no) target section
372 if (!$tar) {
373 $a_string = preg_replace("/\[IF_TARGET\].*\[\/IF_TARGET\]/imsU", '', $a_string);
374 } else {
375 $a_string = preg_replace("/\[IF_TARGET\](.*)\[\/IF_TARGET\]/imsU", "$1", $a_string);
376 }
377
378 return $a_string;
379 }
380
381 public function addAttachment(string $a_filename, string $a_display_name): void
382 {
383 $this->attachments[$a_filename] = $a_display_name;
384 }
385}
$filename
Definition: buildRTE.php:78
Builds data types.
Definition: Factory.php:21
const IL_CAL_UNIX
Class ilAccountMail.
useLangVariablesAsFallback(bool $a_status)
ilMailMimeSenderFactory $senderFactory
readAccountMail(string $a_lang)
send()
Sends the mail with its object properties as MimeMail It first tries to read the mail body,...
GlobalHttpState $http
replacePlaceholders(string $a_string, ilObjUser $a_user, array $a_amail, string $a_lang)
addAttachment(string $a_filename, string $a_display_name)
setUser(ilObjUser $a_user)
ensureValidMailDataShape(array $mailData)
addAttachments(array $mailData)
setUserPassword(string $a_pwd)
setAttachConfiguredFiles(bool $attachConfiguredFiles)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
@classDescription Date and time handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
Class ilMailMimeSenderFactory.
static _lookupNewAccountMail(string $a_lang)
User class.
getFullname(int $a_max_strlen=0)
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_ID
Definition: constants.php:41
const CLIENT_NAME
Definition: constants.php:42
const USER_FOLDER_ID
Definition: constants.php:33
global $DIC
Definition: feed.php:28
Interface GlobalHttpState.
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
$type
$lang
Definition: xapiexit.php:26