ILIAS  release_8 Revision v8.24
class.ilAccountRegistrationMail.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 protected const MODE_DIRECT_REGISTRATION = 1;
29
33
35 {
36 $this->settings = $settings;
37 $this->logger = $logger;
39 }
40
41 public function getMode(): int
42 {
43 return $this->mode;
44 }
45
47 {
48 $clone = clone $this;
49 $clone->mode = self::MODE_DIRECT_REGISTRATION;
50 return $clone;
51 }
52
54 {
55 $clone = clone $this;
57 return $clone;
58 }
59
60 private function isEmptyMailConfigurationData(array $mailData): bool
61 {
62 return !(
63 isset($mailData['body'], $mailData['subject']) &&
64 is_string($mailData['body']) &&
65 $mailData['body'] !== '' &&
66 is_string($mailData['subject']) &&
67 $mailData['subject'] !== ''
68 );
69 }
70
71 private function trySendingUserDefinedAccountMail(ilObjUser $user, string $rawPassword): bool
72 {
73 $trimStrings = static function ($value) {
74 if (is_string($value)) {
75 $value = trim($value);
76 }
77
78 return $value;
79 };
80
81 $this->logger->debug(sprintf(
82 "Trying to send configurable email dependent welcome email to user %s (id: %s|language: %s) ...",
83 $user->getLogin(),
84 $user->getId(),
85 $user->getLanguage()
86 ));
87
88 $mailData = array_map($trimStrings, ilObjUserFolder::_lookupNewAccountMail($user->getLanguage()));
89
90 if ($this->isEmptyMailConfigurationData($mailData)) {
91 $this->logger->debug(sprintf(
92 "Either subject or email missing, trying to determine email configuration via default language: %s",
93 $this->language->getDefaultLanguage()
94 ));
95
96 $mailData = ilObjUserFolder::_lookupNewAccountMail($this->language->getDefaultLanguage());
97 if (!is_array($mailData)) {
98 $this->logger->debug(sprintf(
99 "Did not find any email configuration for language '%s' at all, skipping attempt ...",
100 $this->language->getDefaultLanguage()
101 ));
102 return false;
103 }
104
105 $mailData = array_map($trimStrings, $mailData);
106 if ($this->isEmptyMailConfigurationData($mailData)) {
107 $this->logger->debug("Did not find any valid email configuration, skipping attempt ...");
108 return false;
109 }
110 }
111
112 $accountMail = new ilAccountMail();
113 $accountMail->setUser($user);
114
115 if ($this->settings->passwordGenerationEnabled()) {
116 $accountMail->setUserPassword($rawPassword);
117 }
118
119 if (isset($mailData['att_file'])) {
121 $fs->create();
122
123 $pathToFile = '/' . implode('/', array_map(static function (string $pathPart): string {
124 return trim($pathPart, '/');
125 }, [
126 $fs->getAbsolutePath(),
127 $mailData['lang'],
128 ]));
129
130 $accountMail->addAttachment($pathToFile, $mailData['att_file']);
131
132 $this->logger->debug(sprintf(
133 "Attaching '%s' as '%s' ...",
134 $pathToFile,
135 $mailData['att_file']
136 ));
137 } else {
138 $this->logger->debug("Not attachments configured for this email configuration ...");
139 }
140
141 $accountMail->send();
142
143 $this->logger->debug("Welcome email sent");
144
145 return true;
146 }
147
149 ilObjUser $user,
150 string $rawPassword,
151 bool $usedRegistrationCode
152 ): void {
153 if (!$user->getEmail()) {
154 $this->logger->debug(sprintf(
155 "Missing email address, did not send account registration mail for user %s (id: %s) ...",
156 $user->getLogin(),
157 $user->getId()
158 ));
159 return;
160 }
161
162 $this->logger->debug(sprintf(
163 "Sending language variable dependent welcome email to user %s (id: %s|language: %s) as fallback ...",
164 $user->getLogin(),
165 $user->getId(),
166 $user->getLanguage()
167 ));
168
169 $this->initMimeMail();
170
171 $this->initLanguageByIso2Code($user->getLanguage());
172
173 $this->setSubject($this->language->txt('reg_mail_subject'));
174
175 $this->setBody($this->language->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ',');
176 $this->appendBody("\n\n");
177 $this->appendBody($this->language->txt('reg_mail_body_text1'));
178 $this->appendBody("\n\n");
179 $this->appendBody($this->language->txt('reg_mail_body_text2'));
180 $this->appendBody("\n");
181 $this->appendBody(ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID);
182 $this->appendBody("\n");
183 $this->appendBody($this->language->txt('login') . ': ' . $user->getLogin());
184 $this->appendBody("\n");
185
186 if ($this->settings->passwordGenerationEnabled()) {
187 $this->appendBody($this->language->txt('passwd') . ': ' . $rawPassword);
188 $this->appendBody("\n");
189 }
190
191 if ($this->getMode() === self::MODE_DIRECT_REGISTRATION) {
192 if ($this->settings->getRegistrationType() === ilRegistrationSettings::IL_REG_APPROVE && !$usedRegistrationCode) {
193 $this->appendBody("\n");
194 $this->appendBody($this->language->txt('reg_mail_body_pwd_generation'));
195 $this->appendBody("\n\n");
196 }
197 } elseif ($this->getMode() === self::MODE_REGISTRATION_WITH_EMAIL_CONFIRMATION) {
198 $this->appendBody("\n");
199 $this->appendBody($this->language->txt('reg_mail_body_forgot_password_info'));
200 $this->appendBody("\n\n");
201 }
202
203 $this->appendBody($this->language->txt('reg_mail_body_text3'));
204 $this->appendBody("\n");
205 $this->appendBody($user->getProfileAsString($this->language));
207
208 $this->sendMimeMail($user->getEmail());
209
210 $this->logger->debug("Welcome email sent");
211 }
212
213 public function send(ilObjUser $user, string $rawPassword = '', bool $usedRegistrationCode = false): void
214 {
215 if (!$this->trySendingUserDefinedAccountMail($user, $rawPassword)) {
216 $this->sendLanguageVariableBasedAccountMail($user, $rawPassword, $usedRegistrationCode);
217 }
218 }
219}
Class ilAccountMail.
Class ilAccountRegistrationMail.
trySendingUserDefinedAccountMail(ilObjUser $user, string $rawPassword)
sendLanguageVariableBasedAccountMail(ilObjUser $user, string $rawPassword, bool $usedRegistrationCode)
send(ilObjUser $user, string $rawPassword='', bool $usedRegistrationCode=false)
__construct(ilRegistrationSettings $settings, ilLanguage $lng, ilLogger $logger)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
Component logger with individual log levels by component id.
static _getInstallationSignature()
Base class for mime mail notifications.
static _lookupNewAccountMail(string $a_lang)
User class.
Class ilObjAuthSettingsGUI.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
const CLIENT_ID
Definition: constants.php:41
const USER_FOLDER_ID
Definition: constants.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$lng