37use UnexpectedValueException;
78 private string $editToken =
'';
103 $this->options = $options;
104 $this->hookRunner =
new HookRunner( $hookContainer );
105 $this->userOptionsLookup = $userOptionsLookup;
106 $this->centralIdLookup = $centralIdLookup;
107 $this->userFactory = $userFactory;
108 $this->emailer = $emailer;
109 $this->messageFormatterFactory = $messageFormatterFactory;
110 $this->contLangMsgFormatter = $contLangMsgFormatter;
112 $this->sender = $sender;
123 $targetIdentity = $target->
getUser();
125 if ( !$targetIdentity->getId() ) {
126 return StatusValue::newFatal(
'emailnotarget' );
130 return StatusValue::newFatal(
'noemailtext' );
133 $targetUser = $this->userFactory->newFromUserIdentity( $targetIdentity );
134 if ( !$targetUser->canReceiveEmail() ) {
135 return StatusValue::newFatal(
'nowikiemailtext' );
138 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
140 !$this->userOptionsLookup->getOption( $targetIdentity,
'email-allow-new-users' ) &&
141 $senderUser->isNewbie()
143 return StatusValue::newFatal(
'nowikiemailtext' );
146 $muteList = $this->userOptionsLookup->getOption(
153 $senderId = $this->centralIdLookup->centralIdFromLocalUser( $this->sender->getUser() );
154 if ( $senderId !== 0 && in_array( $senderId, $muteList ) ) {
173 return StatusValue::newFatal(
'usermaildisabled' );
176 $user = $this->userFactory->newFromAuthority( $this->sender );
180 if ( !$user->isEmailConfirmed() ) {
181 return StatusValue::newFatal(
'mailnologin' );
184 $status = PermissionStatus::newGood();
185 if ( !$this->sender->isDefinitelyAllowed(
'sendemail', $status ) ) {
192 $this->hookRunner->onUserCanSendEmail( $user, $hookErr );
193 $this->hookRunner->onEmailUserPermissionsErrors( $user, $this->editToken, $hookErr );
194 if ( is_array( $hookErr ) ) {
196 $msgParamsArray = is_array( $hookErr[2] ) ? $hookErr[2] : [];
197 $ret = StatusValue::newFatal( $hookErr[1], ...$msgParamsArray );
198 $ret->value = $hookErr[0];
212 $status = $this->canSend();
213 if ( !$status->isOK() ) {
217 $status = PermissionStatus::newGood();
218 if ( !$this->sender->authorizeAction(
'sendemail', $status ) ) {
222 $hookRes = $this->hookRunner->onEmailUserAuthorizeSend( $this->sender, $status );
223 if ( !$hookRes && !$status->isGood() ) {
227 return StatusValue::newGood();
247 $senderIdentity = $this->sender->
getUser();
248 $targetStatus = $this->validateTarget( $target );
249 if ( !$targetStatus->isGood() ) {
250 return $targetStatus;
253 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
255 $toAddress = MailAddress::newFromUser( $target );
256 $fromAddress = MailAddress::newFromUser( $senderUser );
259 $text = rtrim( $text ) .
"\n\n-- \n";
260 $text .= $this->contLangMsgFormatter->format(
261 MessageValue::new(
'emailuserfooter', [ $fromAddress->name, $toAddress->name ] )
264 if ( $this->options->get( MainConfigNames::EnableSpecialMute ) ) {
265 $text .=
"\n" . $this->contLangMsgFormatter->format(
267 'specialmute-email-footer',
269 $this->getSpecialMuteCanonicalURL( $senderIdentity->getName() ),
270 $senderIdentity->getName()
278 if ( !$this->hookRunner->onEmailUser( $toAddress, $fromAddress, $subject, $text, $error ) ) {
281 } elseif ( $error ===
false || $error ===
'' || $error === [] ) {
283 return StatusValue::newFatal(
'hookaborted' );
284 } elseif ( $error ===
true ) {
287 } elseif ( is_array( $error ) ) {
289 foreach ( $error as $e ) {
290 $status->fatal( $e );
298 $type = is_object( $error ) ? get_class( $error ) : gettype( $error );
299 throw new UnexpectedValueException(
300 'EmailUser hook set $error to unsupported type ' . $type
306 $hookRes = $this->hookRunner->onEmailUserSendEmail(
315 if ( !$hookRes && !$hookStatus->isGood() ) {
319 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $fromAddress );
321 $status = $this->emailer->send(
327 [
'replyTo' => $replyTo ]
330 if ( !$status->isGood() ) {
337 if ( $CCMe && !$toAddress->equals( $fromAddress ) ) {
338 $userMsgFormatter = $this->messageFormatterFactory->getTextFormatter( $langCode );
339 $ccTo = $fromAddress;
340 $ccFrom = $fromAddress;
341 $ccSubject = $userMsgFormatter->format(
342 MessageValue::new(
'emailccsubject' )->plaintextParams(
349 $this->hookRunner->onEmailUserCC( $ccTo, $ccFrom, $ccSubject, $ccText );
351 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $ccFrom );
353 $ccStatus = $this->emailer->send(
359 [
'replyTo' => $replyTo ]
361 $status->merge( $ccStatus );
364 $this->hookRunner->onEmailUserComplete( $toAddress, $fromAddress, $subject, $text );
374 private function getFromAndReplyTo(
MailAddress $fromAddress ): array {
375 if ( $this->options->get( MainConfigNames::UserEmailUseReplyTo ) ) {
385 $this->options->get( MainConfigNames::PasswordSender ),
386 $this->contLangMsgFormatter->format( MessageValue::new(
'emailsender' ) )
388 $replyTo = $fromAddress;
405 $mailFrom = $fromAddress;
408 return [ $mailFrom, $replyTo ];
417 private function getSpecialMuteCanonicalURL(
string $targetName ): string {
418 if ( defined(
'MW_PHPUNIT_TEST' ) ) {
419 return "Ceci n'est pas une URL";
421 return SpecialPage::getTitleFor(
'Mute', $targetName )->getCanonicalURL();
429 $this->editToken = $token;
if(!defined('MW_SETUP_CALLBACK'))
Stores a single person's name and email address.
A class containing constants representing the names of configuration variables.
const EnableUserEmail
Name constant for the EnableUserEmail setting, for use with Config::get()
const EnableSpecialMute
Name constant for the EnableSpecialMute 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.