37use UnexpectedValueException;
80 private string $editToken =
'';
105 $this->options = $options;
106 $this->hookRunner =
new HookRunner( $hookContainer );
107 $this->userOptionsLookup = $userOptionsLookup;
108 $this->centralIdLookup = $centralIdLookup;
109 $this->userFactory = $userFactory;
110 $this->emailer = $emailer;
111 $this->messageFormatterFactory = $messageFormatterFactory;
112 $this->contLangMsgFormatter = $contLangMsgFormatter;
114 $this->sender = $sender;
125 $targetIdentity = $target->
getUser();
127 if ( !$targetIdentity->getId() ) {
128 return StatusValue::newFatal(
'emailnotarget' );
132 return StatusValue::newFatal(
'noemailtext' );
135 $targetUser = $this->userFactory->newFromUserIdentity( $targetIdentity );
136 if ( !$targetUser->canReceiveEmail() ) {
137 return StatusValue::newFatal(
'nowikiemailtext' );
140 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
142 !$this->userOptionsLookup->getOption( $targetIdentity,
'email-allow-new-users' ) &&
143 $senderUser->isNewbie()
145 return StatusValue::newFatal(
'nowikiemailtext' );
148 $muteList = $this->userOptionsLookup->getOption(
155 $senderId = $this->centralIdLookup->centralIdFromLocalUser( $this->sender->getUser() );
156 if ( $senderId !== 0 && in_array( $senderId, $muteList ) ) {
175 return StatusValue::newFatal(
'usermaildisabled' );
178 $user = $this->userFactory->newFromAuthority( $this->sender );
182 if ( !$user->isEmailConfirmed() ) {
183 return StatusValue::newFatal(
'mailnologin' );
186 $status = PermissionStatus::newGood();
187 if ( !$this->sender->isDefinitelyAllowed(
'sendemail', $status ) ) {
194 $this->hookRunner->onUserCanSendEmail( $user, $hookErr );
195 $this->hookRunner->onEmailUserPermissionsErrors( $user, $this->editToken, $hookErr );
196 if ( is_array( $hookErr ) ) {
198 $msgParamsArray = is_array( $hookErr[2] ) ? $hookErr[2] : [];
199 $ret = StatusValue::newFatal( $hookErr[1], ...$msgParamsArray );
200 $ret->value = $hookErr[0];
214 $status = $this->canSend();
215 if ( !$status->isOK() ) {
219 $status = PermissionStatus::newGood();
220 if ( !$this->sender->authorizeAction(
'sendemail', $status ) ) {
224 $hookRes = $this->hookRunner->onEmailUserAuthorizeSend( $this->sender, $status );
225 if ( !$hookRes && !$status->isGood() ) {
229 return StatusValue::newGood();
249 $senderIdentity = $this->sender->
getUser();
250 $targetStatus = $this->validateTarget( $target );
251 if ( !$targetStatus->isGood() ) {
252 return $targetStatus;
255 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
257 $toAddress = MailAddress::newFromUser( $target );
258 $fromAddress = MailAddress::newFromUser( $senderUser );
261 $text = rtrim( $text ) .
"\n\n-- \n";
262 $text .= $this->contLangMsgFormatter->format(
263 MessageValue::new(
'emailuserfooter', [ $fromAddress->name, $toAddress->name ] )
266 if ( $this->options->get( MainConfigNames::EnableSpecialMute ) ) {
267 $text .=
"\n" . $this->contLangMsgFormatter->format(
269 'specialmute-email-footer',
271 $this->getSpecialMuteCanonicalURL( $senderIdentity->getName() ),
272 $senderIdentity->getName()
280 if ( !$this->hookRunner->onEmailUser( $toAddress, $fromAddress, $subject, $text, $error ) ) {
283 } elseif ( $error ===
false || $error ===
'' || $error === [] ) {
285 return StatusValue::newFatal(
'hookaborted' );
286 } elseif ( $error ===
true ) {
289 } elseif ( is_array( $error ) ) {
291 foreach ( $error as $e ) {
292 $status->fatal( $e );
300 $type = is_object( $error ) ? get_class( $error ) : gettype( $error );
301 throw new UnexpectedValueException(
302 'EmailUser hook set $error to unsupported type ' . $type
308 $hookRes = $this->hookRunner->onEmailUserSendEmail(
317 if ( !$hookRes && !$hookStatus->isGood() ) {
321 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $fromAddress );
323 $status = $this->emailer->send(
329 [
'replyTo' => $replyTo ]
332 if ( !$status->isGood() ) {
339 if ( $CCMe && !$toAddress->equals( $fromAddress ) ) {
340 $userMsgFormatter = $this->messageFormatterFactory->getTextFormatter( $langCode );
341 $ccTo = $fromAddress;
342 $ccFrom = $fromAddress;
343 $ccSubject = $userMsgFormatter->format(
344 MessageValue::new(
'emailccsubject' )->plaintextParams(
351 $this->hookRunner->onEmailUserCC( $ccTo, $ccFrom, $ccSubject, $ccText );
353 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $ccFrom );
355 $ccStatus = $this->emailer->send(
361 [
'replyTo' => $replyTo ]
363 $status->merge( $ccStatus );
366 $this->hookRunner->onEmailUserComplete( $toAddress, $fromAddress, $subject, $text );
376 private function getFromAndReplyTo(
MailAddress $fromAddress ): array {
377 if ( $this->options->get( MainConfigNames::UserEmailUseReplyTo ) ) {
387 $this->options->get( MainConfigNames::PasswordSender ),
388 $this->contLangMsgFormatter->format( MessageValue::new(
'emailsender' ) )
390 $replyTo = $fromAddress;
407 $mailFrom = $fromAddress;
410 return [ $mailFrom, $replyTo ];
419 private function getSpecialMuteCanonicalURL(
string $targetName ): string {
420 if ( defined(
'MW_PHPUNIT_TEST' ) ) {
421 return "Ceci n'est pas une URL";
423 return SpecialPage::getTitleFor(
'Mute', $targetName )->getCanonicalURL();
431 $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.