ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilAccountRegistrationMail.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  protected const MODE_DIRECT_REGISTRATION = 1;
29 
31  private ilLogger $logger;
32  private int $mode = self::MODE_DIRECT_REGISTRATION;
33  private ?string $permanent_link_target = null;
34 
35  public function __construct(ilRegistrationSettings $settings, ilLanguage $lng, ilLogger $logger)
36  {
37  $this->settings = $settings;
38  $this->logger = $logger;
39  parent::__construct(false);
40  }
41 
42  public function getMode(): int
43  {
44  return $this->mode;
45  }
46 
47  public function withPermanentLinkTarget(string $permanent_link_target): self
48  {
49  if ($permanent_link_target === '') {
50  throw new InvalidArgumentException(
51  'Permanent link target must not be empty'
52  );
53  }
54 
55  $clone = clone $this;
56  $clone->permanent_link_target = $permanent_link_target;
57  return $clone;
58  }
59 
61  {
62  $clone = clone $this;
63  $clone->mode = self::MODE_DIRECT_REGISTRATION;
64  return $clone;
65  }
66 
68  {
69  $clone = clone $this;
70  $clone->mode = self::MODE_REGISTRATION_WITH_EMAIL_CONFIRMATION;
71  return $clone;
72  }
73 
74  private function isEmptyMailConfigurationData(array $mailData): bool
75  {
76  return !(
77  isset($mailData['body'], $mailData['subject']) &&
78  is_string($mailData['body']) &&
79  $mailData['body'] !== '' &&
80  is_string($mailData['subject']) &&
81  $mailData['subject'] !== ''
82  );
83  }
84 
85  private function trySendingUserDefinedAccountMail(ilObjUser $user, string $rawPassword): bool
86  {
87  $trimStrings = static function ($value) {
88  if (is_string($value)) {
89  $value = trim($value);
90  }
91 
92  return $value;
93  };
94 
95  $this->logger->debug(sprintf(
96  'Trying to send configurable email dependent welcome email to user %s (id: %s|language: %s) ...',
97  $user->getLogin(),
98  $user->getId(),
99  $user->getLanguage()
100  ));
101 
102  $mailData = array_map($trimStrings, ilObjUserFolder::_lookupNewAccountMail($user->getLanguage()));
103 
104  if ($this->isEmptyMailConfigurationData($mailData)) {
105  $this->logger->debug(sprintf(
106  'Either subject or email missing, trying to determine email configuration via default language: %s',
107  $this->language->getDefaultLanguage()
108  ));
109 
110  $mailData = ilObjUserFolder::_lookupNewAccountMail($this->language->getDefaultLanguage());
111  if (!is_array($mailData)) {
112  $this->logger->debug(sprintf(
113  "Did not find any email configuration for language '%s' at all, skipping attempt ...",
114  $this->language->getDefaultLanguage()
115  ));
116  return false;
117  }
118 
119  $mailData = array_map($trimStrings, $mailData);
120  if ($this->isEmptyMailConfigurationData($mailData)) {
121  $this->logger->debug('Did not find any valid email configuration, skipping attempt ...');
122  return false;
123  }
124  }
125 
126  $accountMail = new ilAccountMail();
127  $accountMail->setUser($user);
128  $accountMail->setPermanentLinkTarget($this->permanent_link_target);
129 
130  if ($this->settings->passwordGenerationEnabled()) {
131  $accountMail->setUserPassword($rawPassword);
132  }
133 
134  if (isset($mailData['att_file'])) {
136  $fs->create();
137 
138  $pathToFile = '/' . implode('/', array_map(static function (string $pathPart): string {
139  return trim($pathPart, '/');
140  }, [
141  $fs->getAbsolutePath(),
142  $mailData['lang'],
143  ]));
144 
145  $accountMail->addAttachment($pathToFile, $mailData['att_file']);
146 
147  $this->logger->debug(sprintf(
148  "Attaching '%s' as '%s' ...",
149  $pathToFile,
150  $mailData['att_file']
151  ));
152  } else {
153  $this->logger->debug('Not attachments configured for this email configuration ...');
154  }
155 
156  $accountMail->send();
157 
158  $this->logger->debug('Welcome email sent');
159 
160  return true;
161  }
162 
164  ilObjUser $user,
165  string $rawPassword,
166  bool $usedRegistrationCode
167  ): void {
168  if (!$user->getEmail()) {
169  $this->logger->debug(sprintf(
170  'Missing email address, did not send account registration mail for user %s (id: %s) ...',
171  $user->getLogin(),
172  $user->getId()
173  ));
174  return;
175  }
176 
177  $this->logger->debug(sprintf(
178  'Sending language variable dependent welcome email to user %s (id: %s|language: %s) as fallback ...',
179  $user->getLogin(),
180  $user->getId(),
181  $user->getLanguage()
182  ));
183 
184  $this->initMimeMail();
185 
186  $this->initLanguageByIso2Code($user->getLanguage());
187 
188  $this->setSubject($this->language->txt('reg_mail_subject'));
189 
190  $this->setBody($this->language->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ',');
191  $this->appendBody("\n\n");
192  $this->appendBody($this->language->txt('reg_mail_body_text1'));
193  $this->appendBody("\n\n");
194  $this->appendBody($this->language->txt('reg_mail_body_text2'));
195  $this->appendBody("\n");
196  $this->appendBody(ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID);
197  $this->appendBody("\n");
198  $this->appendBody($this->language->txt('login') . ': ' . $user->getLogin());
199  $this->appendBody("\n");
200 
201  if ($this->settings->passwordGenerationEnabled()) {
202  $this->appendBody($this->language->txt('passwd') . ': ' . $rawPassword);
203  $this->appendBody("\n");
204  }
205 
206  if ($this->getMode() === self::MODE_DIRECT_REGISTRATION) {
207  if ($this->settings->getRegistrationType() === ilRegistrationSettings::IL_REG_APPROVE && !$usedRegistrationCode) {
208  $this->appendBody("\n");
209  $this->appendBody($this->language->txt('reg_mail_body_pwd_generation'));
210  $this->appendBody("\n\n");
211  }
212  } elseif ($this->getMode() === self::MODE_REGISTRATION_WITH_EMAIL_CONFIRMATION) {
213  $this->appendBody("\n");
214  $this->appendBody($this->language->txt('reg_mail_body_forgot_password_info'));
215  $this->appendBody("\n\n");
216  }
217 
218  $this->appendBody($this->language->txt('reg_mail_body_text3'));
219  $this->appendBody("\n");
220  $this->appendBody($user->getProfileAsString($this->language));
222 
223  $this->sendMimeMail($user->getEmail());
224 
225  $this->logger->debug('Welcome email sent');
226  }
227 
228  public function send(ilObjUser $user, string $rawPassword = '', bool $usedRegistrationCode = false): void
229  {
230  if (!$this->trySendingUserDefinedAccountMail($user, $rawPassword)) {
231  $this->sendLanguageVariableBasedAccountMail($user, $rawPassword, $usedRegistrationCode);
232  }
233  }
234 }
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...
getFullname(int $a_max_strlen=0)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Class ilAccountRegistrationMail.
static _lookupNewAccountMail(string $a_lang)
sendLanguageVariableBasedAccountMail(ilObjUser $user, string $rawPassword, bool $usedRegistrationCode)
const CLIENT_ID
Definition: constants.php:41
__construct(ilRegistrationSettings $settings, ilLanguage $lng, ilLogger $logger)
Class ilObjAuthSettingsGUI.
withPermanentLinkTarget(string $permanent_link_target)
send(ilObjUser $user, string $rawPassword='', bool $usedRegistrationCode=false)
__construct(Container $dic, ilPlugin $plugin)
global $lng
Definition: privfeed.php:31
trySendingUserDefinedAccountMail(ilObjUser $user, string $rawPassword)
language()
description: > Example for rendring a language glyph.
Definition: language.php:41
static _getInstallationSignature()