ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAccountMail.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\Refinery\Factory as Refinery;
23use ILIAS\User\Settings\NewAccountMail\Repository as NewAccountMailRepository;
24use ILIAS\User\Settings\NewAccountMail\Mail as NewAccountMail;
25use ILIAS\ResourceStorage\Services as ResourceStorage;
26
28{
29 private readonly ilSetting $settings;
30 private readonly ilTree $repository_tree;
32 public string $u_password = '';
33 public ?ilObjUser $user = null;
34 private bool $lang_variables_as_fallback = false;
35 private readonly ResourceStorage $irss;
36 private readonly NewAccountMailRepository $account_mail_repo;
37 private array $amail = [];
38 private ?string $permanent_link_target = null;
39
40 public function __construct()
41 {
42 global $DIC;
43 $this->settings = $DIC->settings();
44 $this->repository_tree = $DIC->repositoryTree();
45 $this->sender_factory = $DIC->mail()->mime()->senderFactory();
46 $this->irss = $DIC->resourceStorage();
47 $this->account_mail_repo = new NewAccountMailRepository($DIC->database());
48 }
49
50 public function useLangVariablesAsFallback(bool $a_status): void
51 {
52 $this->lang_variables_as_fallback = $a_status;
53 }
54
55 public function areLangVariablesUsedAsFallback(): bool
56 {
58 }
59
60 public function setUserPassword(string $a_pwd): void
61 {
62 $this->u_password = $a_pwd;
63 }
64
65 public function getUserPassword(): string
66 {
67 return $this->u_password;
68 }
69
70 public function setUser(ilObjUser $a_user): void
71 {
72 $this->user = $a_user;
73 }
74
75 public function setPermanentLinkTarget(?string $permanent_link_target): void
76 {
77 if ($permanent_link_target === '') {
78 throw new InvalidArgumentException(
79 'Permanent link target must not be empty'
80 );
81 }
82
83 $this->permanent_link_target = $permanent_link_target;
84 }
85
86 public function getUser(): ?ilObjUser
87 {
88 return $this->user;
89 }
90
91 public function reset(): void
92 {
93 $this->user = null;
94 $this->u_password = '';
95 $this->permanent_link_target = null;
96 }
97
101 private function readAccountMail(string $a_lang): NewAccountMail
102 {
103 if (!isset($this->amail[$a_lang]) || !($this->amail[$a_lang] instanceof NewAccountMail)) {
104 $this->amail[$a_lang] = $this->account_mail_repo->getFor($a_lang);
105 }
106
107 return $this->amail[$a_lang];
108 }
109
116 public function send(): bool
117 {
118 $user = $this->getUser();
119 if (!$user instanceof ilObjUser) {
120 throw new RuntimeException('A user instance must be passed when sending emails');
121 }
122
123 if ($user->getEmail() === '') {
124 return false;
125 }
126
127 // determine language and get account mail data
128 // fall back to default language if acccount mail data is not given for user language.
129 $amail = $this->readAccountMail($user->getLanguage());
131 if ($amail->getBody() === '' || $amail->getSubject() === '') {
132 $fallback_language = 'en';
133 $amail = $this->readAccountMail($this->settings->get('language', $fallback_language));
134 $lang = $this->settings->get('language', $fallback_language);
135 }
136
137 $mmail = new ilMimeMail();
138
139 // fallback if mail data is still not given
140 if (($amail->getBody() === '' || $amail->getSubject() === '') && $this->areLangVariablesUsedAsFallback()) {
142 $tmp_lang = new ilLanguage($lang);
143
144 $mail_subject = $tmp_lang->txt('reg_mail_subject');
145
146 $timelimit = '';
147 if (!$user->checkTimeLimit()) {
148 $tmp_lang->loadLanguageModule('registration');
149
150 // #6098
151 $timelimit_from = new ilDateTime($user->getTimeLimitFrom(), IL_CAL_UNIX);
152 $timelimit_until = new ilDateTime($user->getTimeLimitUntil(), IL_CAL_UNIX);
153 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
154 $timelimit = "\n" . sprintf($tmp_lang->txt('reg_mail_body_timelimit'), $timelimit) . "\n\n";
155 }
156
157 // mail body
158 $mail_body = $tmp_lang->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ",\n\n" .
159 $tmp_lang->txt('reg_mail_body_text1') . "\n\n" .
160 $tmp_lang->txt('reg_mail_body_text2') . "\n" .
161 ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID . "\n";
162 $mail_body .= $tmp_lang->txt('login') . ': ' . $user->getLogin() . "\n";
163 $mail_body .= $tmp_lang->txt('passwd') . ': ' . $this->u_password . "\n";
164 $mail_body .= "\n" . $timelimit;
165 $mail_body .= $tmp_lang->txt('reg_mail_body_text3') . "\n\r";
166 $mail_body .= $user->getProfileAsString($tmp_lang);
167 } else {
168 $attachment = $amail->getAttachment($this->irss);
169 if ($attachment !== null) {
170 $mmail->Attach($attachment[0], '', 'attachment', $attachment[1]);
171 }
172
173 // replace placeholders
174 $mail_subject = $this->replacePlaceholders($amail->getSubject(), $user, $amail, $lang);
175 $mail_body = $this->replacePlaceholders($amail->getBody(), $user, $amail, $lang);
176 }
177
178 $mmail->From($this->sender_factory->system());
179 $mmail->Subject($mail_subject, true);
180 $mmail->To($user->getEmail());
181 $mmail->Body($mail_body);
182
183 $mmail->Send();
184
185 return true;
186 }
187
188 public function replacePlaceholders(string $a_string, ilObjUser $a_user, NewAccountMail $a_amail, string $a_lang): string
189 {
190 global $DIC;
191 $settings = $DIC->settings();
192 $mustache_factory = $DIC->mail()->mustacheFactory();
193
194 $replacements = [];
195
196 // determine salutation
197 $replacements['MAIL_SALUTATION'] = $mustache_factory->getBasicEngine()->render(
198 match ($a_user->getGender()) {
199 'f' => trim($a_amail->getSalutationFemale()),
200 'm' => trim($a_amail->getSalutationMale()),
201 default => trim($a_amail->getSalutationNoneSpecific()),
202 },
203 [
204 'FIRST_NAME' => $a_user->getFirstname(),
205 'LAST_NAME' => $a_user->getLastname(),
206 'LOGIN' => $a_user->getLogin(),
207 ]
208 );
209 $replacements['LOGIN'] = $a_user->getLogin();
210 $replacements['FIRST_NAME'] = $a_user->getFirstname();
211 $replacements['LAST_NAME'] = $a_user->getLastname();
212 // BEGIN Mail Include E-Mail Address in account mail
213 $replacements['EMAIL'] = $a_user->getEmail();
214 // END Mail Include E-Mail Address in account mail
215 $replacements['PASSWORD'] = $this->getUserPassword();
216 $replacements['ILIAS_URL'] = ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID;
217 $replacements['CLIENT_NAME'] = CLIENT_NAME;
218 $replacements['ADMIN_MAIL'] = $settings->get('admin_email');
219 $replacements['IF_PASSWORD'] = $this->getUserPassword() !== '';
220 $replacements['IF_NO_PASSWORD'] = $this->getUserPassword() === '';
221
222 // #13346
223 if (!$a_user->getTimeLimitUnlimited()) {
224 // #6098
225 $replacements['IF_TIMELIMIT'] = !$a_user->getTimeLimitUnlimited();
226 $timelimit_from = new ilDateTime($a_user->getTimeLimitFrom(), IL_CAL_UNIX);
227 $timelimit_until = new ilDateTime($a_user->getTimeLimitUntil(), IL_CAL_UNIX);
228 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
229 $replacements['TIMELIMIT'] = $timelimit;
230 }
231
232 // target
233 $replacements['IF_TARGET'] = false;
234 if ($this->permanent_link_target !== null) {
235 $tarr = explode('_', $this->permanent_link_target);
236 if ($this->repository_tree->isInTree((int) $tarr[1])) {
237 $obj_id = ilObject::_lookupObjId((int) $tarr[1]);
238 $type = ilObject::_lookupType($obj_id);
239 if ($type === $tarr[0]) {
240 $replacements['TARGET_TITLE'] = ilObject::_lookupTitle($obj_id);
241 $replacements['TARGET'] = ILIAS_HTTP_PATH . '/goto.php?client_id=' . CLIENT_ID . '&target=' . $this->permanent_link_target;
242
243 // this looks complicated, but we may have no initilised $lng object here
244 // if mail is send during user creation in authentication
245 $replacements['TARGET_TYPE'] = ilLanguage::_lookupEntry($a_lang, 'common', 'obj_' . $tarr[0]);
246 $replacements['IF_TARGET'] = true;
247 }
248 }
249 }
250
251 return $mustache_factory->getBasicEngine()->render($a_string, $replacements);
252 }
253}
Builds data types.
Definition: Factory.php:36
const IL_CAL_UNIX
useLangVariablesAsFallback(bool $a_status)
readAccountMail(string $a_lang)
readonly ilTree $repository_tree
send()
Sends the mail with its object properties as MimeMail It first tries to read the mail body,...
readonly ilSetting $settings
readonly ResourceStorage $irss
setUser(ilObjUser $a_user)
readonly ilMailMimeSenderFactory $sender_factory
setPermanentLinkTarget(?string $permanent_link_target)
setUserPassword(string $a_pwd)
readonly NewAccountMailRepository $account_mail_repo
replacePlaceholders(string $a_string, ilObjUser $a_user, NewAccountMail $a_amail, string $a_lang)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.
@classDescription Date and time handling
language handling
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
User class.
getProfileAsString(Language $language)
Get formatted mail body text of user profile data.
getFullname(int $max_strlen=0)
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
ILIAS Setting Class.
get(string $a_keyword, ?string $a_default_value=null)
get setting
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const CLIENT_ID
Definition: constants.php:41
const CLIENT_NAME
Definition: constants.php:42
Interface GlobalHttpState.
global $DIC
Definition: shib_login.php:26
$lang
Definition: xapiexit.php:25