21use UnexpectedValueException;
51 private string $editToken =
'';
67 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
68 $this->hookRunner =
new HookRunner( $hookContainer );
76 $targetIdentity = $target->getUser();
78 if ( !$targetIdentity->getId() ) {
79 return StatusValue::newFatal(
'emailnotarget' );
83 return StatusValue::newFatal(
'noemailtext' );
86 $targetUser = $this->userFactory->newFromUserIdentity( $targetIdentity );
87 if ( !$targetUser->canReceiveEmail() ) {
88 return StatusValue::newFatal(
'nowikiemailtext' );
91 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
93 !$this->userOptionsLookup->getOption( $targetIdentity,
'email-allow-new-users' ) &&
94 $senderUser->isNewbie()
96 return StatusValue::newFatal(
'nowikiemailtext' );
99 $muteList = $this->userOptionsLookup->getOption(
106 $senderId = $this->centralIdLookup->centralIdFromLocalUser( $this->sender->getUser() );
107 if ( $senderId !== 0 && in_array( $senderId, $muteList ) ) {
126 return StatusValue::newFatal(
'usermaildisabled' );
129 $user = $this->userFactory->newFromAuthority( $this->sender );
133 if ( !$user->isEmailConfirmed() ) {
134 return StatusValue::newFatal(
'mailnologin' );
137 $status = PermissionStatus::newGood();
138 if ( !$this->sender->isDefinitelyAllowed(
'sendemail', $status ) ) {
145 $this->hookRunner->onUserCanSendEmail( $user, $hookErr );
146 $this->hookRunner->onEmailUserPermissionsErrors( $user, $this->editToken, $hookErr );
147 if ( is_array( $hookErr ) ) {
149 $msgParamsArray = is_array( $hookErr[2] ) ? $hookErr[2] : [];
150 $ret = StatusValue::newFatal( $hookErr[1], ...$msgParamsArray );
151 $ret->value = $hookErr[0];
165 $status = $this->canSend();
166 if ( !$status->isOK() ) {
170 $status = PermissionStatus::newGood();
171 if ( !$this->sender->authorizeAction(
'sendemail', $status ) ) {
175 $hookRes = $this->hookRunner->onEmailUserAuthorizeSend( $this->sender, $status );
176 if ( !$hookRes && !$status->isGood() ) {
180 return StatusValue::newGood();
200 $senderIdentity = $this->sender->getUser();
201 $targetStatus = $this->validateTarget( $target );
202 if ( !$targetStatus->isGood() ) {
203 return $targetStatus;
206 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
208 $toAddress = MailAddress::newFromUser( $target );
209 $fromAddress = MailAddress::newFromUser( $senderUser );
212 $text = rtrim( $text ) .
"\n\n-- \n";
213 $text .= $this->contLangMsgFormatter->format(
214 MessageValue::new(
'emailuserfooter', [ $fromAddress->name, $toAddress->name ] )
217 $text .=
"\n" . $this->contLangMsgFormatter->format(
219 'specialmute-email-footer',
221 $this->getSpecialMuteCanonicalURL( $senderIdentity->getName() ),
222 $senderIdentity->getName()
229 if ( !$this->hookRunner->onEmailUser( $toAddress, $fromAddress, $subject, $text, $error ) ) {
232 } elseif ( $error ===
false || $error ===
'' || $error === [] ) {
234 return StatusValue::newFatal(
'hookaborted' );
235 } elseif ( $error ===
true ) {
237 return StatusValue::newGood();
238 } elseif ( is_array( $error ) ) {
240 foreach ( $error as $e ) {
241 $status->fatal( $e );
244 } elseif ( $error instanceof MessageSpecifier ) {
249 $type = get_debug_type( $error );
250 throw new UnexpectedValueException(
251 'EmailUser hook set $error to unsupported type ' . $type
257 $hookRes = $this->hookRunner->onEmailUserSendEmail(
266 if ( !$hookRes && !$hookStatus->isGood() ) {
270 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $fromAddress );
272 $status = $this->emailer->send(
278 [
'replyTo' => $replyTo ]
281 if ( !$status->isGood() ) {
288 if ( $CCMe && !$toAddress->equals( $fromAddress ) ) {
289 $userMsgFormatter = $this->messageFormatterFactory->getTextFormatter( $langCode );
290 $ccTo = $fromAddress;
291 $ccFrom = $fromAddress;
292 $ccSubject = $userMsgFormatter->format(
293 MessageValue::new(
'emailccsubject' )->plaintextParams(
300 $this->hookRunner->onEmailUserCC( $ccTo, $ccFrom, $ccSubject, $ccText );
302 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $ccFrom );
304 $ccStatus = $this->emailer->send(
310 [
'replyTo' => $replyTo ]
312 $status->merge( $ccStatus );
315 $this->hookRunner->onEmailUserComplete( $toAddress, $fromAddress, $subject, $text );
325 private function getFromAndReplyTo( MailAddress $fromAddress ): array {
326 if ( $this->options->get( MainConfigNames::UserEmailUseReplyTo ) ) {
335 $mailFrom =
new MailAddress(
336 $this->options->get( MainConfigNames::PasswordSender ),
337 $this->contLangMsgFormatter->format( MessageValue::new(
'emailsender' ) )
339 $replyTo = $fromAddress;
356 $mailFrom = $fromAddress;
359 return [ $mailFrom, $replyTo ];
366 private function getSpecialMuteCanonicalURL(
string $targetName ): string {
367 if ( defined(
'MW_PHPUNIT_TEST' ) ) {
368 return "Ceci n'est pas une URL";
370 return SpecialPage::getTitleFor(
'Mute', $targetName )->getCanonicalURL();
377 $this->editToken = $token;
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const EnableUserEmail
Name constant for the EnableUserEmail setting, for use with Config::get()
const EnableEmail
Name constant for the EnableEmail setting, for use with Config::get()
const PasswordSender
Name constant for the PasswordSender setting, for use with Config::get()
const UserEmailUseReplyTo
Name constant for the UserEmailUseReplyTo setting, for use with Config::get()
Parent class for all special pages.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
static newGood( $value=null)
Factory function for good results.