ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAccountRegistrationMail.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
12 
14  private $settings;
15 
17  private $lng;
18 
20  private $logger;
21 
23  private $mode = self::MODE_DIRECT_REGISTRATION;
24 
32  {
33  $this->settings = $settings;
34  $this->lng = $lng;
35  $this->logger = $logger;
36 
37  parent::__construct(false);
38  }
39 
43  public function getMode()
44  {
45  return $this->mode;
46  }
47 
51  public function withDirectRegistrationMode()
52  {
53  $clone = clone $this;
54  $clone->mode = self::MODE_DIRECT_REGISTRATION;
55 
56  return $clone;
57  }
58 
63  {
64  $clone = clone $this;
65  $clone->mode = self::MODE_REGISTRATION_WITH_EMAIL_CONFIRMATION;
66 
67  return $clone;
68  }
69 
74  private function isEmptyMailConfigurationData($mailData)
75  {
76  return !(
77  isset($mailData['body']) &&
78  is_string($mailData['body']) &&
79  $mailData['body'] !== '' &&
80  isset($mailData['subject']) &&
81  is_string($mailData['subject']) &&
82  $mailData['subject'] !== ''
83  );
84  }
85 
91  private function trySendingUserDefinedAccountMail(ilObjUser $user, $rawPassword)
92  {
93  $trimStrings = function ($value) {
94  if (is_string($value)) {
95  $value = trim($value);
96  }
97 
98  return $value;
99  };
100 
101  $this->logger->debug(sprintf(
102  "Trying to send configurable email dependent welcome email to user %s (id: %s|language: %s) ...",
103  $user->getLogin(),
104  $user->getId(),
105  $user->getLanguage()
106  ));
107 
109  if (!is_array($mailData)) {
110  $this->logger->debug(sprintf(
111  "Did not find any email configuration for language '%s' at all, skipping attempt ...",
112  $user->getLanguage()
113  ));
114  return false;
115  }
116 
117  $mailData = array_map($trimStrings, $mailData);
118 
119  if ($this->isEmptyMailConfigurationData($mailData)) {
120  $this->logger->debug(sprintf(
121  "Either subject or email missing, trying to determine email configuration via default language: %s",
122  $this->language->getDefaultLanguage()
123  ));
124 
125  $mailData = ilObjUserFolder::_lookupNewAccountMail($this->language->getDefaultLanguage());
126  if (!is_array($mailData)) {
127  $this->logger->debug(sprintf(
128  "Did not find any email configuration for language '%s' at all, skipping attempt ...",
129  $this->language->getDefaultLanguage()
130  ));
131  return false;
132  }
133 
134  $mailData = array_map($trimStrings, $mailData);
135  if ($this->isEmptyMailConfigurationData($mailData)) {
136  $this->logger->debug(sprintf(
137  "Did not find any valid email configuration, skipping attempt ..."
138  ));
139  return false;
140  }
141  }
142 
143  $accountMail = new ilAccountMail();
144  $accountMail->setUser($user);
145 
146  if ($this->settings->passwordGenerationEnabled()) {
147  $accountMail->setUserPassword($rawPassword);
148  }
149 
150  if (isset($mailData['att_file'])) {
152  $fs->create();
153 
154  $pathToFile = '/' . implode('/', array_map(function ($pathPart) {
155  return trim($pathPart, '/');
156  }, [
157  $fs->getAbsolutePath(),
158  $mailData['lang'],
159  ]));
160 
161  $accountMail->addAttachment($pathToFile, $mailData['att_file']);
162 
163  $this->logger->debug(sprintf(
164  "Attaching '%s' as '%s' ...",
165  $pathToFile,
166  $mailData['att_file']
167  ));
168  } else {
169  $this->logger->debug(sprintf(
170  "Not attachments configured for this email configuration ..."
171  ));
172  }
173 
174  $accountMail->send();
175 
176  $this->logger->debug(sprintf(
177  "Welcome email sent"
178  ));
179 
180  return true;
181  }
182 
188  private function sendLanguageVariableBasedAccountMail(ilObjUser $user, $rawPassword, $usedRegistrationCode)
189  {
190  if (!$user->getEmail()) {
191  $this->logger->debug(sprintf(
192  "Missing email address, did not send account registration mail for user %s (id: %s) ...",
193  $user->getLogin(),
194  $user->getId()
195  ));
196  return;
197  }
198 
199  $this->logger->debug(sprintf(
200  "Sending language variable dependent welcome email to user %s (id: %s|language: %s) as fallback ...",
201  $user->getLogin(),
202  $user->getId(),
203  $user->getLanguage()
204  ));
205 
206  $this->initMimeMail();
207 
208  $this->initLanguageByIso2Code($user->getLanguage());
209 
210  $this->setSubject($this->language->txt('reg_mail_subject'));
211 
212  $this->setBody($this->language->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ',');
213  $this->appendBody("\n\n");
214  $this->appendBody($this->language->txt('reg_mail_body_text1'));
215  $this->appendBody("\n\n");
216  $this->appendBody($this->language->txt('reg_mail_body_text2'));
217  $this->appendBody("\n");
218  $this->appendBody(ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID);
219  $this->appendBody("\n");
220  $this->appendBody($this->language->txt('login') . ': ' . $user->getLogin());
221  $this->appendBody("\n");
222 
223  if ($this->settings->passwordGenerationEnabled()) {
224  $this->appendBody($this->language->txt('passwd') . ': ' . $rawPassword);
225  $this->appendBody("\n");
226  }
227 
228  if ($this->getMode() === self::MODE_DIRECT_REGISTRATION) {
229  if ($this->settings->getRegistrationType() == IL_REG_APPROVE && !$usedRegistrationCode) {
230  $this->appendBody("\n");
231  $this->appendBody($this->language->txt('reg_mail_body_pwd_generation'));
232  $this->appendBody("\n\n");
233  }
234  } elseif ($this->getMode() === self::MODE_REGISTRATION_WITH_EMAIL_CONFIRMATION) {
235  $this->appendBody("\n");
236  $this->appendBody($this->language->txt('reg_mail_body_forgot_password_info'));
237  $this->appendBody("\n\n");
238  }
239 
240  $this->appendBody($this->language->txt('reg_mail_body_text3'));
241  $this->appendBody("\n");
242  $this->appendBody($user->getProfileAsString($this->language));
244 
245  $this->sendMimeMail($user->getEmail());
246 
247  $this->logger->debug(sprintf(
248  "Welcome email sent"
249  ));
250  }
251 
257  public function send(ilObjUser $user, $rawPassword = '', $usedRegistrationCode = false)
258  {
259  if (!$this->trySendingUserDefinedAccountMail($user, $rawPassword)) {
260  $this->sendLanguageVariableBasedAccountMail($user, $rawPassword, $usedRegistrationCode);
261  }
262  }
263 }
settings()
Definition: settings.php:2
getLogin()
get login / username public
const USER_FOLDER_ID
Definition: constants.php:31
sendLanguageVariableBasedAccountMail(ilObjUser $user, $rawPassword, $usedRegistrationCode)
getEmail()
get email address public
Class ilAccountRegistrationMail.
getId()
get object id public
static _lookupNewAccountMail($a_lang)
const CLIENT_ID
Definition: constants.php:39
__construct(ilRegistrationSettings $settings, ilLanguage $lng, ilLogger $logger)
ilAccountRegistrationMail constructor.
Class ilObjAuthSettingsGUI.
getFullname($a_max_strlen=0)
get fullname public
Base class for mime mail notifications.
send(ilObjUser $user, $rawPassword='', $usedRegistrationCode=false)
__construct(Container $dic, ilPlugin $plugin)
Class ilAccountMail.
appendBody($a_body)
Append body text.
Component logger with individual log levels by component id.
language()
Definition: language.php:2
getLanguage()
returns a 2char-language-string public
trySendingUserDefinedAccountMail(ilObjUser $user, $rawPassword)
static _getInstallationSignature()