21use UnexpectedValueException;
60 private string $editToken =
'';
87 $this->options = $options;
88 $this->hookRunner =
new HookRunner( $hookContainer );
89 $this->userOptionsLookup = $userOptionsLookup;
90 $this->centralIdLookup = $centralIdLookup;
91 $this->userFactory = $userFactory;
92 $this->emailer = $emailer;
93 $this->messageFormatterFactory = $messageFormatterFactory;
94 $this->contLangMsgFormatter = $contLangMsgFormatter;
96 $this->sender = $sender;
107 $targetIdentity = $target->getUser();
109 if ( !$targetIdentity->getId() ) {
110 return StatusValue::newFatal(
'emailnotarget' );
114 return StatusValue::newFatal(
'noemailtext' );
117 $targetUser = $this->userFactory->newFromUserIdentity( $targetIdentity );
118 if ( !$targetUser->canReceiveEmail() ) {
119 return StatusValue::newFatal(
'nowikiemailtext' );
122 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
124 !$this->userOptionsLookup->getOption( $targetIdentity,
'email-allow-new-users' ) &&
125 $senderUser->isNewbie()
127 return StatusValue::newFatal(
'nowikiemailtext' );
130 $muteList = $this->userOptionsLookup->getOption(
137 $senderId = $this->centralIdLookup->centralIdFromLocalUser( $this->sender->getUser() );
138 if ( $senderId !== 0 && in_array( $senderId, $muteList ) ) {
157 return StatusValue::newFatal(
'usermaildisabled' );
160 $user = $this->userFactory->newFromAuthority( $this->sender );
164 if ( !$user->isEmailConfirmed() ) {
165 return StatusValue::newFatal(
'mailnologin' );
168 $status = PermissionStatus::newGood();
169 if ( !$this->sender->isDefinitelyAllowed(
'sendemail', $status ) ) {
176 $this->hookRunner->onUserCanSendEmail( $user, $hookErr );
177 $this->hookRunner->onEmailUserPermissionsErrors( $user, $this->editToken, $hookErr );
178 if ( is_array( $hookErr ) ) {
180 $msgParamsArray = is_array( $hookErr[2] ) ? $hookErr[2] : [];
181 $ret = StatusValue::newFatal( $hookErr[1], ...$msgParamsArray );
182 $ret->value = $hookErr[0];
196 $status = $this->canSend();
197 if ( !$status->isOK() ) {
201 $status = PermissionStatus::newGood();
202 if ( !$this->sender->authorizeAction(
'sendemail', $status ) ) {
206 $hookRes = $this->hookRunner->onEmailUserAuthorizeSend( $this->sender, $status );
207 if ( !$hookRes && !$status->isGood() ) {
211 return StatusValue::newGood();
231 $senderIdentity = $this->sender->getUser();
232 $targetStatus = $this->validateTarget( $target );
233 if ( !$targetStatus->isGood() ) {
234 return $targetStatus;
237 $senderUser = $this->userFactory->newFromAuthority( $this->sender );
239 $toAddress = MailAddress::newFromUser( $target );
240 $fromAddress = MailAddress::newFromUser( $senderUser );
243 $text = rtrim( $text ) .
"\n\n-- \n";
244 $text .= $this->contLangMsgFormatter->format(
245 MessageValue::new(
'emailuserfooter', [ $fromAddress->name, $toAddress->name ] )
248 if ( $this->options->get( MainConfigNames::EnableSpecialMute ) ) {
249 $text .=
"\n" . $this->contLangMsgFormatter->format(
251 'specialmute-email-footer',
253 $this->getSpecialMuteCanonicalURL( $senderIdentity->getName() ),
254 $senderIdentity->getName()
262 if ( !$this->hookRunner->onEmailUser( $toAddress, $fromAddress, $subject, $text, $error ) ) {
265 } elseif ( $error ===
false || $error ===
'' || $error === [] ) {
267 return StatusValue::newFatal(
'hookaborted' );
268 } elseif ( $error ===
true ) {
271 } elseif ( is_array( $error ) ) {
273 foreach ( $error as $e ) {
274 $status->fatal( $e );
277 } elseif ( $error instanceof MessageSpecifier ) {
282 $type = get_debug_type( $error );
283 throw new UnexpectedValueException(
284 'EmailUser hook set $error to unsupported type ' . $type
290 $hookRes = $this->hookRunner->onEmailUserSendEmail(
299 if ( !$hookRes && !$hookStatus->isGood() ) {
303 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $fromAddress );
305 $status = $this->emailer->send(
311 [
'replyTo' => $replyTo ]
314 if ( !$status->isGood() ) {
321 if ( $CCMe && !$toAddress->equals( $fromAddress ) ) {
322 $userMsgFormatter = $this->messageFormatterFactory->getTextFormatter( $langCode );
323 $ccTo = $fromAddress;
324 $ccFrom = $fromAddress;
325 $ccSubject = $userMsgFormatter->format(
326 MessageValue::new(
'emailccsubject' )->plaintextParams(
333 $this->hookRunner->onEmailUserCC( $ccTo, $ccFrom, $ccSubject, $ccText );
335 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $ccFrom );
337 $ccStatus = $this->emailer->send(
343 [
'replyTo' => $replyTo ]
345 $status->merge( $ccStatus );
348 $this->hookRunner->onEmailUserComplete( $toAddress, $fromAddress, $subject, $text );
358 private function getFromAndReplyTo( MailAddress $fromAddress ): array {
359 if ( $this->options->get( MainConfigNames::UserEmailUseReplyTo ) ) {
368 $mailFrom =
new MailAddress(
369 $this->options->get( MainConfigNames::PasswordSender ),
370 $this->contLangMsgFormatter->format( MessageValue::new(
'emailsender' ) )
372 $replyTo = $fromAddress;
389 $mailFrom = $fromAddress;
392 return [ $mailFrom, $replyTo ];
401 private function getSpecialMuteCanonicalURL(
string $targetName ): string {
402 if ( defined(
'MW_PHPUNIT_TEST' ) ) {
403 return "Ceci n'est pas une URL";
405 return SpecialPage::getTitleFor(
'Mute', $targetName )->getCanonicalURL();
413 $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 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.