ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilAccountMail.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
34 {
35  private readonly ilSetting $settings;
36  private readonly ilTree $repositoryTree;
38  public string $u_password = '';
39  public ?ilObjUser $user = null;
40  private bool $lang_variables_as_fallback = false;
42  private array $attachments = [];
43  private bool $attachConfiguredFiles = false;
44  private array $amail = [];
45  private ?string $permanent_link_target = null;
46 
47  public function __construct()
48  {
49  global $DIC;
50  $this->settings = $DIC->settings();
51  $this->repositoryTree = $DIC->repositoryTree();
52  $this->senderFactory = $DIC->mail()->mime()->senderFactory();
53  }
54 
55  public function useLangVariablesAsFallback(bool $a_status): void
56  {
57  $this->lang_variables_as_fallback = $a_status;
58  }
59 
60  public function areLangVariablesUsedAsFallback(): bool
61  {
63  }
64 
65  public function shouldAttachConfiguredFiles(): bool
66  {
68  }
69 
70  public function setAttachConfiguredFiles(bool $attachConfiguredFiles): void
71  {
72  $this->attachConfiguredFiles = $attachConfiguredFiles;
73  }
74 
75  public function setUserPassword(string $a_pwd): void
76  {
77  $this->u_password = $a_pwd;
78  }
79 
80  public function getUserPassword(): string
81  {
82  return $this->u_password;
83  }
84 
85  public function setUser(ilObjUser $a_user): void
86  {
87  if (
88  $this->user instanceof ilObjUser &&
89  $a_user->getId() !== $this->user->getId()
90  ) {
91  $this->attachments = [];
92  }
93 
94  $this->user = $a_user;
95  }
96 
97  public function setPermanentLinkTarget(?string $permanent_link_target): void
98  {
99  if ($permanent_link_target === '') {
100  throw new InvalidArgumentException(
101  'Permanent link target must not be empty'
102  );
103  }
104 
105  $this->permanent_link_target = $permanent_link_target;
106  }
107 
108  public function getUser(): ?ilObjUser
109  {
110  return $this->user;
111  }
112 
113  public function reset(): void
114  {
115  $this->user = null;
116  $this->u_password = '';
117  $this->permanent_link_target = null;
118  }
119 
124  private function ensureValidMailDataShape(array $mailData): array
125  {
126  foreach (['lang', 'subject', 'body', 'sal_f', 'sal_g', 'sal_m', 'type'] as $key) {
127  if (!isset($mailData[$key])) {
128  $mailData[$key] = '';
129  }
130  }
131 
132  $mailData['subject'] = trim($mailData['subject']);
133  $mailData['body'] = trim($mailData['body']);
134 
135  return $mailData;
136  }
137 
141  private function readAccountMail(string $a_lang): array
142  {
143  if (!isset($this->amail[$a_lang]) || !is_array($this->amail[$a_lang])) {
144  $this->amail[$a_lang] = $this->ensureValidMailDataShape(
146  );
147  }
148 
149  return $this->amail[$a_lang];
150  }
151 
156  private function addAttachments(array $mailData): void
157  {
158  if (isset($mailData['att_file']) && $this->shouldAttachConfiguredFiles()) {
160  $fs->create();
161 
162  $pathToFile = '/' . implode('/', array_map(static function (string $pathPart): string {
163  return trim($pathPart, '/');
164  }, [
165  $fs->getAbsolutePath(),
166  $mailData['lang'],
167  ]));
168 
169  $this->addAttachment($pathToFile, $mailData['att_file']);
170  }
171  }
172 
180  public function send(): bool
181  {
182  $user = $this->getUser();
183  if (!$user instanceof ilObjUser) {
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());
194  $lang = $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  global $DIC;
255  $tree = $DIC->repositoryTree();
256  $ilSetting = $DIC->settings();
257  $mustache_factory = $DIC->mail()->mustacheFactory();
258 
259  $replacements = [];
260 
261  // determine salutation
262  $replacements['MAIL_SALUTATION'] = $mustache_factory->getBasicEngine()->render(
263  match ($a_user->getGender()) {
264  'f' => trim((string) $a_amail['sal_f']),
265  'm' => trim((string) $a_amail['sal_m']),
266  default => trim((string) $a_amail['sal_g']),
267  },
268  [
269  'FIRST_NAME' => $a_user->getFirstname(),
270  'LAST_NAME' => $a_user->getLastname(),
271  'LOGIN' => $a_user->getLogin(),
272  ]
273  );
274  $replacements['LOGIN'] = $a_user->getLogin();
275  $replacements['FIRST_NAME'] = $a_user->getFirstname();
276  $replacements['LAST_NAME'] = $a_user->getLastname();
277  // BEGIN Mail Include E-Mail Address in account mail
278  $replacements['EMAIL'] = $a_user->getEmail();
279  // END Mail Include E-Mail Address in account mail
280  $replacements['PASSWORD'] = $this->getUserPassword();
281  $replacements['ILIAS_URL'] = ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID;
282  $replacements['CLIENT_NAME'] = CLIENT_NAME;
283  $replacements['ADMIN_MAIL'] = $ilSetting->get('admin_email');
284  $replacements['IF_PASSWORD'] = $this->getUserPassword() != '';
285  $replacements['IF_NO_PASSWORD'] = $this->getUserPassword() == '';
286 
287  // #13346
288  if (!$a_user->getTimeLimitUnlimited()) {
289  // #6098
290  $replacements['IF_TIMELIMIT'] = !$a_user->getTimeLimitUnlimited();
291  $timelimit_from = new ilDateTime($a_user->getTimeLimitFrom(), IL_CAL_UNIX);
292  $timelimit_until = new ilDateTime($a_user->getTimeLimitUntil(), IL_CAL_UNIX);
293  $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
294  $replacements['TIMELIMIT'] = $timelimit;
295  }
296 
297  // target
298  $replacements['IF_TARGET'] = false;
299  if ($this->permanent_link_target !== null) {
300  $tarr = explode('_', $this->permanent_link_target);
301  if ($this->repositoryTree->isInTree((int) $tarr[1])) {
302  $obj_id = ilObject::_lookupObjId((int) $tarr[1]);
303  $type = ilObject::_lookupType($obj_id);
304  if ($type === $tarr[0]) {
305  $replacements['TARGET_TITLE'] = ilObject::_lookupTitle($obj_id);
306  $replacements['TARGET'] = ILIAS_HTTP_PATH . '/goto.php?client_id=' . CLIENT_ID . '&target=' . $this->permanent_link_target;
307 
308  // this looks complicated, but we may have no initilised $lng object here
309  // if mail is send during user creation in authentication
310  $replacements['TARGET_TYPE'] = ilLanguage::_lookupEntry($a_lang, 'common', 'obj_' . $tarr[0]);
311  $replacements['IF_TARGET'] = true;
312  }
313  }
314  }
315 
316  return $mustache_factory->getBasicEngine()->render($a_string, $replacements);
317  }
318 
319  public function addAttachment(string $a_filename, string $a_display_name): void
320  {
321  $this->attachments[$a_filename] = $a_display_name;
322  }
323 }
send()
Sends the mail with its object properties as MimeMail It first tries to read the mail body...
const USER_FOLDER_ID
Definition: constants.php:33
readonly ilMailMimeSenderFactory $senderFactory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_NAME
Definition: constants.php:42
readAccountMail(string $a_lang)
setAttachConfiguredFiles(bool $attachConfiguredFiles)
getFullname(int $a_max_strlen=0)
ensureValidMailDataShape(array $mailData)
setPermanentLinkTarget(?string $permanent_link_target)
addAttachments(array $mailData)
const IL_CAL_UNIX
static _lookupObjId(int $ref_id)
replacePlaceholders(string $a_string, ilObjUser $a_user, array $a_amail, string $a_lang)
setUserPassword(string $a_pwd)
static _lookupTitle(int $obj_id)
static _lookupNewAccountMail(string $a_lang)
useLangVariablesAsFallback(bool $a_status)
addAttachment(string $a_filename, string $a_display_name)
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:25
readonly ilSetting $settings
$filename
Definition: buildRTE.php:78
readonly ilTree $repositoryTree
setUser(ilObjUser $a_user)
global $ilSetting
Definition: privfeed.php:32
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ilObjUser $user=null)
Format a period of two dates Shows: 14.
Class ilAccountMail.
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
static _lookupType(int $id, bool $reference=false)