ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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)
 
 setPermanentLinkTarget (?string $permanent_link_target)
 
 getUser ()
 
 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
 

Private Member Functions

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

Private Attributes

readonly ilSetting $settings
 
readonly ilTree $repositoryTree
 
readonly ilMailMimeSenderFactory $senderFactory
 
bool $lang_variables_as_fallback = false
 
array $attachments = []
 
bool $attachConfiguredFiles = false
 
array $amail = []
 
string $permanent_link_target = null
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilAccountMail::__construct ( )

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

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

48  {
49  global $DIC;
50  $this->settings = $DIC->settings();
51  $this->repositoryTree = $DIC->repositoryTree();
52  $this->senderFactory = $DIC->mail()->mime()->senderFactory();
53  }
global $DIC
Definition: shib_login.php:25
+ Here is the call graph for this function:

Member Function Documentation

◆ addAttachment()

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

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

Referenced by addAttachments().

319  : void
320  {
321  $this->attachments[$a_filename] = $a_display_name;
322  }
+ 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 156 of file class.ilAccountMail.php.

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

Referenced by send().

156  : 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  }
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 60 of file class.ilAccountMail.php.

References $lang_variables_as_fallback.

Referenced by send().

60  : bool
61  {
63  }
+ 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 124 of file class.ilAccountMail.php.

Referenced by readAccountMail().

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

◆ getUser()

ilAccountMail::getUser ( )

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

References $user.

Referenced by send().

108  : ?ilObjUser
109  {
110  return $this->user;
111  }
+ Here is the caller graph for this function:

◆ getUserPassword()

ilAccountMail::getUserPassword ( )

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

References $u_password.

Referenced by replacePlaceholders().

80  : string
81  {
82  return $this->u_password;
83  }
+ 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 141 of file class.ilAccountMail.php.

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

Referenced by send().

141  : 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  }
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 252 of file class.ilAccountMail.php.

References $DIC, $ilSetting, $permanent_link_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(), IL_CAL_UNIX, and ILIAS\Repository\repositoryTree().

Referenced by send().

252  : 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  }
const CLIENT_NAME
Definition: constants.php:42
const IL_CAL_UNIX
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:25
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.
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 113 of file class.ilAccountMail.php.

References ILIAS\Repository\user().

113  : void
114  {
115  $this->user = null;
116  $this->u_password = '';
117  $this->permanent_link_target = null;
118  }
+ 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 180 of file class.ilAccountMail.php.

References $filename, 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().

180  : 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.
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  }
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
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ilObjUser $user=null)
Format a period of two dates Shows: 14.
+ Here is the call graph for this function:

◆ setAttachConfiguredFiles()

ilAccountMail::setAttachConfiguredFiles ( bool  $attachConfiguredFiles)

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

References $attachConfiguredFiles.

70  : void
71  {
72  $this->attachConfiguredFiles = $attachConfiguredFiles;
73  }

◆ setPermanentLinkTarget()

ilAccountMail::setPermanentLinkTarget ( ?string  $permanent_link_target)

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

References $permanent_link_target.

97  : 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  }

◆ setUser()

ilAccountMail::setUser ( ilObjUser  $a_user)

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

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

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

◆ setUserPassword()

ilAccountMail::setUserPassword ( string  $a_pwd)

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

75  : void
76  {
77  $this->u_password = $a_pwd;
78  }

◆ shouldAttachConfiguredFiles()

ilAccountMail::shouldAttachConfiguredFiles ( )

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

References $attachConfiguredFiles.

Referenced by addAttachments().

65  : bool
66  {
68  }
+ Here is the caller graph for this function:

◆ useLangVariablesAsFallback()

ilAccountMail::useLangVariablesAsFallback ( bool  $a_status)

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

55  : void
56  {
57  $this->lang_variables_as_fallback = $a_status;
58  }

Field Documentation

◆ $amail

array ilAccountMail::$amail = []
private

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

◆ $attachConfiguredFiles

bool ilAccountMail::$attachConfiguredFiles = false
private

◆ $attachments

array ilAccountMail::$attachments = []
private

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

◆ $lang_variables_as_fallback

bool ilAccountMail::$lang_variables_as_fallback = false
private

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

Referenced by areLangVariablesUsedAsFallback().

◆ $permanent_link_target

string ilAccountMail::$permanent_link_target = null
private

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

Referenced by replacePlaceholders(), and setPermanentLinkTarget().

◆ $repositoryTree

readonly ilTree ilAccountMail::$repositoryTree
private

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

◆ $senderFactory

readonly ilMailMimeSenderFactory ilAccountMail::$senderFactory
private

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

◆ $settings

readonly ilSetting ilAccountMail::$settings
private

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

◆ $u_password

string ilAccountMail::$u_password = ''

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

Referenced by getUserPassword().

◆ $user

ilObjUser ilAccountMail::$user = null

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

Referenced by getUser().


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