ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAccountRegistrationMail.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\User\Settings\NewAccountMail\Repository as NewAccountMailRepository;
22
28{
29 protected const MODE_DIRECT_REGISTRATION = 1;
31
33 private ?string $permanent_link_target = null;
34
35 public function __construct(
36 private readonly ilRegistrationSettings $settings,
37 private readonly ilLogger $logger,
38 private readonly NewAccountMailRepository $account_mail_repository
39 ) {
41 }
42
43 public function getMode(): int
44 {
45 return $this->mode;
46 }
47
48 public function withPermanentLinkTarget(string $permanent_link_target): self
49 {
50 if ($permanent_link_target === '') {
51 throw new InvalidArgumentException(
52 'Permanent link target must not be empty'
53 );
54 }
55
56 $clone = clone $this;
57 $clone->permanent_link_target = $permanent_link_target;
58 return $clone;
59 }
60
62 {
63 $clone = clone $this;
64 $clone->mode = self::MODE_DIRECT_REGISTRATION;
65 return $clone;
66 }
67
69 {
70 $clone = clone $this;
72 return $clone;
73 }
74
75 private function isEmptyMailConfigurationData(array $mailData): bool
76 {
77 return !(
78 isset($mailData['body'], $mailData['subject']) &&
79 is_string($mailData['body']) &&
80 $mailData['body'] !== '' &&
81 is_string($mailData['subject']) &&
82 $mailData['subject'] !== ''
83 );
84 }
85
86 private function trySendingUserDefinedAccountMail(ilObjUser $user, string $rawPassword): bool
87 {
88 $this->logger->debug(sprintf(
89 'Trying to send configurable email dependent welcome email to user %s (id: %s|language: %s) ...',
90 $user->getLogin(),
91 $user->getId(),
92 $user->getLanguage()
93 ));
94
95 $mailData = $this->account_mail_repository->getFor($user->getLanguage());
96
97 if ($mailData->getBody() === '' && $mailData->getSubject() === '') {
98 $this->logger->debug(sprintf(
99 'Either subject or email missing, trying to determine email configuration via default language: %s',
100 $this->language->getDefaultLanguage()
101 ));
102
103 $mailData = $this->account_mail_repository->getFor($this->language->getDefaultLanguage());
104
105 if ($mailData->getBody() === '' && $mailData->getSubject() === '') {
106 $this->logger->debug('Did not find any valid email configuration, skipping attempt ...');
107 return false;
108 }
109 }
110
111 $accountMail = new ilAccountMail();
112 $accountMail->setUser($user);
113 $accountMail->setPermanentLinkTarget($this->permanent_link_target);
114
115 if ($this->settings->passwordGenerationEnabled()) {
116 $accountMail->setUserPassword($rawPassword);
117 }
118
119 $accountMail->send();
120
121 $this->logger->debug('Welcome email sent');
122
123 return true;
124 }
125
127 ilObjUser $user,
128 string $rawPassword,
129 bool $usedRegistrationCode
130 ): void {
131 if (!$user->getEmail()) {
132 $this->logger->debug(sprintf(
133 'Missing email address, did not send account registration mail for user %s (id: %s) ...',
134 $user->getLogin(),
135 $user->getId()
136 ));
137 return;
138 }
139
140 $this->logger->debug(sprintf(
141 'Sending language variable dependent welcome email to user %s (id: %s|language: %s) as fallback ...',
142 $user->getLogin(),
143 $user->getId(),
144 $user->getLanguage()
145 ));
146
147 $this->initMimeMail();
148
149 $this->initLanguageByIso2Code($user->getLanguage());
150
151 $this->setSubject($this->language->txt('reg_mail_subject'));
152
153 $this->setBody($this->language->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ',');
154 $this->appendBody("\n\n");
155 $this->appendBody($this->language->txt('reg_mail_body_text1'));
156 $this->appendBody("\n\n");
157 $this->appendBody($this->language->txt('reg_mail_body_text2'));
158 $this->appendBody("\n");
159 $this->appendBody(ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID);
160 $this->appendBody("\n");
161 $this->appendBody($this->language->txt('login') . ': ' . $user->getLogin());
162 $this->appendBody("\n");
163
164 if ($this->settings->passwordGenerationEnabled()) {
165 $this->appendBody($this->language->txt('passwd') . ': ' . $rawPassword);
166 $this->appendBody("\n");
167 }
168
169 if ($this->getMode() === self::MODE_DIRECT_REGISTRATION) {
170 if ($this->settings->getRegistrationType() === ilRegistrationSettings::IL_REG_APPROVE && !$usedRegistrationCode) {
171 $this->appendBody("\n");
172 $this->appendBody($this->language->txt('reg_mail_body_pwd_generation'));
173 $this->appendBody("\n\n");
174 }
175 } elseif ($this->getMode() === self::MODE_REGISTRATION_WITH_EMAIL_CONFIRMATION) {
176 $this->appendBody("\n");
177 $this->appendBody($this->language->txt('reg_mail_body_forgot_password_info'));
178 $this->appendBody("\n\n");
179 }
180
181 $this->appendBody($this->language->txt('reg_mail_body_text3'));
182 $this->appendBody("\n");
183 $this->appendBody($user->getProfileAsString($this->language));
185
186 $this->sendMimeMail($user->getEmail());
187
188 $this->logger->debug('Welcome email sent');
189 }
190
191 public function send(ilObjUser $user, string $rawPassword = '', bool $usedRegistrationCode = false): void
192 {
193 if (!$this->trySendingUserDefinedAccountMail($user, $rawPassword)) {
194 $this->sendLanguageVariableBasedAccountMail($user, $rawPassword, $usedRegistrationCode);
195 }
196 }
197}
Class ilAccountRegistrationMail.
trySendingUserDefinedAccountMail(ilObjUser $user, string $rawPassword)
sendLanguageVariableBasedAccountMail(ilObjUser $user, string $rawPassword, bool $usedRegistrationCode)
send(ilObjUser $user, string $rawPassword='', bool $usedRegistrationCode=false)
__construct(private readonly ilRegistrationSettings $settings, private readonly ilLogger $logger, private readonly NewAccountMailRepository $account_mail_repository)
withPermanentLinkTarget(string $permanent_link_target)
Component logger with individual log levels by component id.
static _getInstallationSignature()
User class.
Class ilObjAuthSettingsGUI.
const CLIENT_ID
Definition: constants.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
if(!file_exists('../ilias.ini.php'))