21use UnexpectedValueException;
59 private string $editToken =
'';
86 $this->options = $options;
87 $this->hookRunner =
new HookRunner( $hookContainer );
88 $this->userOptionsLookup = $userOptionsLookup;
89 $this->centralIdLookup = $centralIdLookup;
90 $this->userFactory = $userFactory;
91 $this->emailer = $emailer;
92 $this->messageFormatterFactory = $messageFormatterFactory;
93 $this->contLangMsgFormatter = $contLangMsgFormatter;
95 $this->sender = $sender;
106 $targetIdentity = $target->getUser();
108 if ( !$targetIdentity->getId() ) {
109 return StatusValue::newFatal(
'emailnotarget' );
113 return StatusValue::newFatal(
'noemailtext' );
116 $targetUser = $this->userFactory->newFromUserIdentity( $targetIdentity );
117 if ( !$targetUser->canReceiveEmail() ) {
118 return StatusValue::newFatal(
'nowikiemailtext' );
121 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
123 !$this->userOptionsLookup->getOption( $targetIdentity,
'email-allow-new-users' ) &&
124 $senderUser->isNewbie()
126 return StatusValue::newFatal(
'nowikiemailtext' );
129 $muteList = $this->userOptionsLookup->getOption(
136 $senderId = $this->centralIdLookup->centralIdFromLocalUser( $this->sender->getUser() );
137 if ( $senderId !== 0 && in_array( $senderId, $muteList ) ) {
156 return StatusValue::newFatal(
'usermaildisabled' );
159 $user = $this->userFactory->newFromAuthority( $this->sender );
163 if ( !$user->isEmailConfirmed() ) {
164 return StatusValue::newFatal(
'mailnologin' );
167 $status = PermissionStatus::newGood();
168 if ( !$this->sender->isDefinitelyAllowed(
'sendemail', $status ) ) {
175 $this->hookRunner->onUserCanSendEmail( $user, $hookErr );
176 $this->hookRunner->onEmailUserPermissionsErrors( $user, $this->editToken, $hookErr );
177 if ( is_array( $hookErr ) ) {
179 $msgParamsArray = is_array( $hookErr[2] ) ? $hookErr[2] : [];
180 $ret = StatusValue::newFatal( $hookErr[1], ...$msgParamsArray );
181 $ret->value = $hookErr[0];
195 $status = $this->canSend();
196 if ( !$status->isOK() ) {
200 $status = PermissionStatus::newGood();
201 if ( !$this->sender->authorizeAction(
'sendemail', $status ) ) {
205 $hookRes = $this->hookRunner->onEmailUserAuthorizeSend( $this->sender, $status );
206 if ( !$hookRes && !$status->isGood() ) {
210 return StatusValue::newGood();
230 $senderIdentity = $this->sender->getUser();
231 $targetStatus = $this->validateTarget( $target );
232 if ( !$targetStatus->isGood() ) {
233 return $targetStatus;
236 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
238 $toAddress = MailAddress::newFromUser( $target );
239 $fromAddress = MailAddress::newFromUser( $senderUser );
242 $text = rtrim( $text ) .
"\n\n-- \n";
243 $text .= $this->contLangMsgFormatter->format(
244 MessageValue::new(
'emailuserfooter', [ $fromAddress->name, $toAddress->name ] )
247 $text .=
"\n" . $this->contLangMsgFormatter->format(
249 'specialmute-email-footer',
251 $this->getSpecialMuteCanonicalURL( $senderIdentity->getName() ),
252 $senderIdentity->getName()
259 if ( !$this->hookRunner->onEmailUser( $toAddress, $fromAddress, $subject, $text, $error ) ) {
262 } elseif ( $error ===
false || $error ===
'' || $error === [] ) {
264 return StatusValue::newFatal(
'hookaborted' );
265 } elseif ( $error ===
true ) {
267 return StatusValue::newGood();
268 } elseif ( is_array( $error ) ) {
270 foreach ( $error as $e ) {
271 $status->fatal( $e );
274 } elseif ( $error instanceof MessageSpecifier ) {
279 $type = get_debug_type( $error );
280 throw new UnexpectedValueException(
281 'EmailUser hook set $error to unsupported type ' . $type
287 $hookRes = $this->hookRunner->onEmailUserSendEmail(
296 if ( !$hookRes && !$hookStatus->isGood() ) {
300 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $fromAddress );
302 $status = $this->emailer->send(
308 [
'replyTo' => $replyTo ]
311 if ( !$status->isGood() ) {
318 if ( $CCMe && !$toAddress->equals( $fromAddress ) ) {
319 $userMsgFormatter = $this->messageFormatterFactory->getTextFormatter( $langCode );
320 $ccTo = $fromAddress;
321 $ccFrom = $fromAddress;
322 $ccSubject = $userMsgFormatter->format(
323 MessageValue::new(
'emailccsubject' )->plaintextParams(
330 $this->hookRunner->onEmailUserCC( $ccTo, $ccFrom, $ccSubject, $ccText );
332 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $ccFrom );
334 $ccStatus = $this->emailer->send(
340 [
'replyTo' => $replyTo ]
342 $status->merge( $ccStatus );
345 $this->hookRunner->onEmailUserComplete( $toAddress, $fromAddress, $subject, $text );
355 private function getFromAndReplyTo( MailAddress $fromAddress ): array {
356 if ( $this->options->get( MainConfigNames::UserEmailUseReplyTo ) ) {
365 $mailFrom =
new MailAddress(
366 $this->options->get( MainConfigNames::PasswordSender ),
367 $this->contLangMsgFormatter->format( MessageValue::new(
'emailsender' ) )
369 $replyTo = $fromAddress;
386 $mailFrom = $fromAddress;
389 return [ $mailFrom, $replyTo ];
398 private function getSpecialMuteCanonicalURL(
string $targetName ): string {
399 if ( defined(
'MW_PHPUNIT_TEST' ) ) {
400 return "Ceci n'est pas une URL";
402 return SpecialPage::getTitleFor(
'Mute', $targetName )->getCanonicalURL();
410 $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.