ILIAS  trunk Revision v5.2.0beta1-34132-g2d4d73d4a0
ilAccountMail Class Reference

Class ilAccountMail. More...

+ Collaboration diagram for ilAccountMail:

Public Member Functions

 __construct ()
 
 useLangVariablesAsFallback (bool $a_status)
 
 areLangVariablesUsedAsFallback ()
 
 shouldAttachConfiguredFiles ()
 
 setAttachConfiguredFiles (bool $attachConfiguredFiles)
 
 setUserPassword (string $a_pwd)
 
 getUserPassword ()
 
 setUser (ilObjUser $a_user)
 
 getUser ()
 
 getTarget ()
 
 reset ()
 
 send ()
 Sends the mail with its object properties as MimeMail It first tries to read the mail body, subject and sender address from posted named formular fields. More...
 
 replacePlaceholders (string $a_string, ilObjUser $a_user, array $a_amail, string $a_lang)
 
 addAttachment (string $a_filename, string $a_display_name)
 

Data Fields

string $u_password = ''
 
ilObjUser $user = null
 
string $target = ''
 

Private Member Functions

 ensureValidMailDataShape (array $mailData)
 
 readAccountMail (string $a_lang)
 
 addAttachments (array $mailData)
 

Private Attributes

readonly GlobalHttpState $http
 
readonly ilSetting $settings
 
readonly Refinery $refinery
 
readonly ilTree $repositoryTree
 
readonly ilMailMimeSenderFactory $senderFactory
 
bool $lang_variables_as_fallback = false
 
array $attachments = []
 
bool $attachConfiguredFiles = false
 
array $amail = []
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilAccountMail::__construct ( )

Definition at line 49 of file class.ilAccountMail.php.

References $DIC, ILIAS\FileDelivery\http(), ILIAS\Repository\refinery(), ILIAS\Repository\repositoryTree(), and ILIAS\Repository\settings().

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()->senderFactory();
57  }
static http()
Fetches the global http state from ILIAS.
$DIC
Definition: xapitoken.php:62
+ Here is the call graph for this function:

Member Function Documentation

◆ addAttachment()

ilAccountMail::addAttachment ( string  $a_filename,
string  $a_display_name 
)

Definition at line 318 of file class.ilAccountMail.php.

Referenced by addAttachments().

318  : void
319  {
320  $this->attachments[$a_filename] = $a_display_name;
321  }
+ Here is the caller graph for this function:

◆ addAttachments()

ilAccountMail::addAttachments ( array  $mailData)
private
Parameters
array{lang?string, subject?: string, body?: string, sal_f?: string, sal_g?: string, sal_m?: string, type?: string, att_file?: string} $mailData
Exceptions

Definition at line 154 of file class.ilAccountMail.php.

References addAttachment(), shouldAttachConfiguredFiles(), and USER_FOLDER_ID.

Referenced by send().

154  : void
155  {
156  if (isset($mailData['att_file']) && $this->shouldAttachConfiguredFiles()) {
158  $fs->create();
159 
160  $pathToFile = '/' . implode('/', array_map(static function (string $pathPart): string {
161  return trim($pathPart, '/');
162  }, [
163  $fs->getAbsolutePath(),
164  $mailData['lang'],
165  ]));
166 
167  $this->addAttachment($pathToFile, $mailData['att_file']);
168  }
169  }
const USER_FOLDER_ID
Definition: constants.php:33
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addAttachment(string $a_filename, string $a_display_name)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ areLangVariablesUsedAsFallback()

ilAccountMail::areLangVariablesUsedAsFallback ( )

Definition at line 64 of file class.ilAccountMail.php.

References $lang_variables_as_fallback.

Referenced by send().

64  : bool
65  {
67  }
+ Here is the caller graph for this function:

◆ ensureValidMailDataShape()

ilAccountMail::ensureValidMailDataShape ( array  $mailData)
private
Parameters
array{lang?string, subject?: string, body?: string, sal_f?: string, sal_g?: string, sal_m?: string, type?: string, att_file?: string} $mailData
Returns
array{lang?: string, subject?: string, body?: string, sal_f?: string, sal_g?: string, sal_m?: string, type?: string, att_file?: string}

Definition at line 122 of file class.ilAccountMail.php.

References ILIAS\LTI\ToolProvider\$key.

Referenced by readAccountMail().

122  : 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  }
string $key
Consumer key/client ID value.
Definition: System.php:193
+ Here is the caller graph for this function:

◆ getTarget()

ilAccountMail::getTarget ( )

Definition at line 106 of file class.ilAccountMail.php.

References $target.

106  : string
107  {
108  return $this->target;
109  }

◆ getUser()

ilAccountMail::getUser ( )

Definition at line 101 of file class.ilAccountMail.php.

References $user.

Referenced by send().

101  : ?ilObjUser
102  {
103  return $this->user;
104  }
+ Here is the caller graph for this function:

◆ getUserPassword()

ilAccountMail::getUserPassword ( )

Definition at line 84 of file class.ilAccountMail.php.

References $u_password.

Referenced by replacePlaceholders().

84  : string
85  {
86  return $this->u_password;
87  }
+ Here is the caller graph for this function:

◆ readAccountMail()

ilAccountMail::readAccountMail ( string  $a_lang)
private
Returns
array{lang?: string, subject?: string, body?: string, sal_f?: string, sal_g?: string, sal_m?: string, type?: string}

Definition at line 139 of file class.ilAccountMail.php.

References ilObjUserFolder\_lookupNewAccountMail(), and ensureValidMailDataShape().

Referenced by send().

139  : 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  }
ensureValidMailDataShape(array $mailData)
static _lookupNewAccountMail(string $a_lang)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replacePlaceholders()

ilAccountMail::replacePlaceholders ( string  $a_string,
ilObjUser  $a_user,
array  $a_amail,
string  $a_lang 
)

Definition at line 250 of file class.ilAccountMail.php.

References $DIC, $ilSetting, $target, ilLanguage\_lookupEntry(), ilObject\_lookupObjId(), ilObject\_lookupTitle(), ilObject\_lookupType(), CLIENT_ID, CLIENT_NAME, ilDatePresentation\formatPeriod(), ilObjUser\getEmail(), ilObjUser\getFirstname(), ilObjUser\getGender(), ilObjUser\getLastname(), ilObjUser\getLogin(), ilObjUser\getTimeLimitFrom(), ilObjUser\getTimeLimitUnlimited(), ilObjUser\getTimeLimitUntil(), getUserPassword(), ILIAS\FileDelivery\http(), IL_CAL_UNIX, ILIAS\Repository\refinery(), and ILIAS\Repository\repositoryTree().

Referenced by send().

250  : string
251  {
252  global $DIC;
253  $tree = $DIC->repositoryTree();
254  $ilSetting = $DIC->settings();
255  $mustache_factory = $DIC->mail()->mustacheFactory();
256 
257  $replacements = [];
258 
259  // determine salutation
260  switch ($a_user->getGender()) {
261  case "f":
262  $replacements["MAIL_SALUTATION"] = trim($a_amail["sal_f"]);
263  break;
264  case "m":
265  $replacements["MAIL_SALUTATION"] = trim($a_amail["sal_m"]);
266  break;
267  default:
268  $replacements["MAIL_SALUTATION"] = trim($a_amail["sal_g"]);
269  }
270  $replacements["LOGIN"] = $a_user->getLogin();
271  $replacements["FIRST_NAME"] = $a_user->getFirstname();
272  $replacements["LAST_NAME"] = $a_user->getLastname();
273  // BEGIN Mail Include E-Mail Address in account mail
274  $replacements["EMAIL"] = $a_user->getEmail();
275  // END Mail Include E-Mail Address in account mail
276  $replacements["PASSWORD"] = $this->getUserPassword();
277  $replacements["ILIAS_URL"] = ILIAS_HTTP_PATH . "/login.php?client_id=" . CLIENT_ID;
278  $replacements["CLIENT_NAME"] = CLIENT_NAME;
279  $replacements["ADMIN_MAIL"] = $ilSetting->get("admin_email");
280  $replacements["IF_PASSWORD"] = $this->getUserPassword() != "";
281  $replacements["IF_NO_PASSWORD"] = $this->getUserPassword() == "";
282 
283  // #13346
284  if (!$a_user->getTimeLimitUnlimited()) {
285  // #6098
286  $replacements["IF_TIMELIMIT"] = !$a_user->getTimeLimitUnlimited();
287  $timelimit_from = new ilDateTime($a_user->getTimeLimitFrom(), IL_CAL_UNIX);
288  $timelimit_until = new ilDateTime($a_user->getTimeLimitUntil(), IL_CAL_UNIX);
289  $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
290  $replacements["TIMELIMIT"] = $timelimit;
291  }
292 
293  // target
294  $replacements["IF_TARGET"] = false;
295  if ($this->http->wrapper()->query()->has('target') &&
296  $this->http->wrapper()->query()->retrieve('target', $this->refinery->kindlyTo()->string()) !== ''
297  ) {
298  $target = $this->http->wrapper()->query()->retrieve('target', $this->refinery->kindlyTo()->string());
299  $tarr = explode('_', (string) $target);
300  if ($this->repositoryTree->isInTree((int) $tarr[1])) {
301  $obj_id = ilObject::_lookupObjId((int) $tarr[1]);
302  $type = ilObject::_lookupType($obj_id);
303  if ($type === $tarr[0]) {
304  $replacements["TARGET_TITLE"] = ilObject::_lookupTitle($obj_id);
305  $replacements["TARGET"] = ILIAS_HTTP_PATH . '/goto.php?client_id=' . CLIENT_ID . '&target=' . $target;
306 
307  // this looks complicated, but we may have no initilised $lng object here
308  // if mail is send during user creation in authentication
309  $replacements["TARGET_TYPE"] = ilLanguage::_lookupEntry($a_lang, "common", "obj_" . $tarr[0]);
310  $replacements["IF_TARGET"] = true;
311  }
312  }
313  }
314 
315  return $mustache_factory->getBasicEngine()->render($a_string, $replacements);
316  }
const CLIENT_NAME
Definition: constants.php:42
const IL_CAL_UNIX
static _lookupObjId(int $ref_id)
static http()
Fetches the global http state from ILIAS.
static _lookupTitle(int $obj_id)
const CLIENT_ID
Definition: constants.php:41
$DIC
Definition: xapitoken.php:62
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
global $ilSetting
Definition: privfeed.php:16
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
static _lookupType(int $id, bool $reference=false)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reset()

ilAccountMail::reset ( )

Definition at line 111 of file class.ilAccountMail.php.

References ILIAS\Repository\user().

111  : void
112  {
113  $this->user = null;
114  $this->u_password = '';
115  $this->target = '';
116  }
+ Here is the call graph for this function:

◆ send()

ilAccountMail::send ( )

Sends the mail with its object properties as MimeMail It first tries to read the mail body, subject and sender address from posted named formular fields.

If no field values found the defaults are used. Placehoders will be replaced by the appropriate data.

Exceptions
RuntimeException

Definition at line 178 of file class.ilAccountMail.php.

References $filename, $lang, addAttachments(), areLangVariablesUsedAsFallback(), ilObjUser\checkTimeLimit(), CLIENT_ID, ilDatePresentation\formatPeriod(), ilObjUser\getEmail(), ilObjUser\getFullname(), ilObjUser\getLanguage(), ilObjUser\getLogin(), ilObjUser\getTimeLimitFrom(), ilObjUser\getTimeLimitUntil(), getUser(), IL_CAL_UNIX, readAccountMail(), replacePlaceholders(), and ILIAS\Repository\settings().

178  : bool
179  {
180  $user = $this->getUser();
181  if (!$user instanceof ilObjUser) {
182  throw new RuntimeException('A user instance must be passed when sending emails');
183  }
184 
185  if ($user->getEmail() === '') {
186  return false;
187  }
188 
189  // determine language and get account mail data
190  // fall back to default language if acccount mail data is not given for user language.
192  $lang = $user->getLanguage();
193  if ($amail['body'] === '' || $amail['subject'] === '') {
194  $fallback_language = 'en';
195  $amail = $this->readAccountMail($this->settings->get('language', $fallback_language));
196  $lang = $this->settings->get('language', $fallback_language);
197  }
198 
199  // fallback if mail data is still not given
200  if (($amail['body'] === '' || $amail['subject'] === '') && $this->areLangVariablesUsedAsFallback()) {
201  $lang = $user->getLanguage();
202  $tmp_lang = new ilLanguage($lang);
203 
204  $mail_subject = $tmp_lang->txt('reg_mail_subject');
205 
206  $timelimit = "";
207  if (!$user->checkTimeLimit()) {
208  $tmp_lang->loadLanguageModule("registration");
209 
210  // #6098
211  $timelimit_from = new ilDateTime($user->getTimeLimitFrom(), IL_CAL_UNIX);
212  $timelimit_until = new ilDateTime($user->getTimeLimitUntil(), IL_CAL_UNIX);
213  $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
214  $timelimit = "\n" . sprintf($tmp_lang->txt('reg_mail_body_timelimit'), $timelimit) . "\n\n";
215  }
216 
217  // mail body
218  $mail_body = $tmp_lang->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ",\n\n" .
219  $tmp_lang->txt('reg_mail_body_text1') . "\n\n" .
220  $tmp_lang->txt('reg_mail_body_text2') . "\n" .
221  ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID . "\n";
222  $mail_body .= $tmp_lang->txt('login') . ': ' . $user->getLogin() . "\n";
223  $mail_body .= $tmp_lang->txt('passwd') . ': ' . $this->u_password . "\n";
224  $mail_body .= "\n" . $timelimit;
225  $mail_body .= $tmp_lang->txt('reg_mail_body_text3') . "\n\r";
226  $mail_body .= $user->getProfileAsString($tmp_lang);
227  } else {
228  $this->addAttachments($amail);
229 
230  // replace placeholders
231  $mail_subject = $this->replacePlaceholders($amail['subject'], $user, $amail, $lang);
232  $mail_body = $this->replacePlaceholders($amail['body'], $user, $amail, $lang);
233  }
234 
235  $mmail = new ilMimeMail();
236  $mmail->From($this->senderFactory->system());
237  $mmail->Subject($mail_subject, true);
238  $mmail->To($user->getEmail());
239  $mmail->Body($mail_body);
240 
241  foreach ($this->attachments as $filename => $display_name) {
242  $mmail->Attach($filename, '', 'attachment', $display_name);
243  }
244 
245  $mmail->Send();
246 
247  return true;
248  }
readAccountMail(string $a_lang)
getFullname(int $a_max_strlen=0)
addAttachments(array $mailData)
const IL_CAL_UNIX
replacePlaceholders(string $a_string, ilObjUser $a_user, array $a_amail, string $a_lang)
const CLIENT_ID
Definition: constants.php:41
$filename
Definition: buildRTE.php:78
$lang
Definition: xapiexit.php:26
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
+ Here is the call graph for this function:

◆ setAttachConfiguredFiles()

ilAccountMail::setAttachConfiguredFiles ( bool  $attachConfiguredFiles)

Definition at line 74 of file class.ilAccountMail.php.

References $attachConfiguredFiles.

74  : void
75  {
76  $this->attachConfiguredFiles = $attachConfiguredFiles;
77  }

◆ setUser()

ilAccountMail::setUser ( ilObjUser  $a_user)

Definition at line 89 of file class.ilAccountMail.php.

References ilObject\getId(), and ILIAS\Repository\user().

89  : 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  }
+ Here is the call graph for this function:

◆ setUserPassword()

ilAccountMail::setUserPassword ( string  $a_pwd)

Definition at line 79 of file class.ilAccountMail.php.

79  : void
80  {
81  $this->u_password = $a_pwd;
82  }

◆ shouldAttachConfiguredFiles()

ilAccountMail::shouldAttachConfiguredFiles ( )

Definition at line 69 of file class.ilAccountMail.php.

References $attachConfiguredFiles.

Referenced by addAttachments().

69  : bool
70  {
72  }
+ Here is the caller graph for this function:

◆ useLangVariablesAsFallback()

ilAccountMail::useLangVariablesAsFallback ( bool  $a_status)

Definition at line 59 of file class.ilAccountMail.php.

59  : void
60  {
61  $this->lang_variables_as_fallback = $a_status;
62  }

Field Documentation

◆ $amail

array ilAccountMail::$amail = []
private

Definition at line 47 of file class.ilAccountMail.php.

◆ $attachConfiguredFiles

bool ilAccountMail::$attachConfiguredFiles = false
private

◆ $attachments

array ilAccountMail::$attachments = []
private

Definition at line 45 of file class.ilAccountMail.php.

◆ $http

readonly GlobalHttpState ilAccountMail::$http
private

Definition at line 35 of file class.ilAccountMail.php.

◆ $lang_variables_as_fallback

bool ilAccountMail::$lang_variables_as_fallback = false
private

Definition at line 43 of file class.ilAccountMail.php.

Referenced by areLangVariablesUsedAsFallback().

◆ $refinery

readonly Refinery ilAccountMail::$refinery
private

Definition at line 37 of file class.ilAccountMail.php.

◆ $repositoryTree

readonly ilTree ilAccountMail::$repositoryTree
private

Definition at line 38 of file class.ilAccountMail.php.

◆ $senderFactory

readonly ilMailMimeSenderFactory ilAccountMail::$senderFactory
private

Definition at line 39 of file class.ilAccountMail.php.

◆ $settings

readonly ilSetting ilAccountMail::$settings
private

Definition at line 36 of file class.ilAccountMail.php.

◆ $target

string ilAccountMail::$target = ''

Definition at line 42 of file class.ilAccountMail.php.

Referenced by getTarget(), and replacePlaceholders().

◆ $u_password

string ilAccountMail::$u_password = ''

Definition at line 40 of file class.ilAccountMail.php.

Referenced by getUserPassword().

◆ $user

ilObjUser ilAccountMail::$user = null

Definition at line 41 of file class.ilAccountMail.php.

Referenced by getUser().


The documentation for this class was generated from the following file: