3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Mail
;
24 use MediaWiki\Config\ServiceOptions
;
25 use MediaWiki\HookContainer\HookContainer
;
26 use MediaWiki\HookContainer\HookRunner
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\Permissions\Authority
;
29 use MediaWiki\Permissions\PermissionStatus
;
30 use MediaWiki\Preferences\MultiUsernameFilter
;
31 use MediaWiki\SpecialPage\SpecialPage
;
32 use MediaWiki\User\CentralId\CentralIdLookup
;
33 use MediaWiki\User\Options\UserOptionsLookup
;
34 use MediaWiki\User\UserFactory
;
36 use UnexpectedValueException
;
37 use Wikimedia\Message\IMessageFormatterFactory
;
38 use Wikimedia\Message\ITextFormatter
;
39 use Wikimedia\Message\MessageSpecifier
;
40 use Wikimedia\Message\MessageValue
;
43 * Send email between two wiki users.
45 * Obtain via EmailUserFactory
47 * This class is stateless and can be used for multiple sends.
54 * @internal For use by ServiceWiring
56 public const CONSTRUCTOR_OPTIONS
= [
57 MainConfigNames
::EnableEmail
,
58 MainConfigNames
::EnableUserEmail
,
59 MainConfigNames
::EnableSpecialMute
,
60 MainConfigNames
::PasswordSender
,
61 MainConfigNames
::UserEmailUseReplyTo
,
64 private ServiceOptions
$options;
65 private HookRunner
$hookRunner;
66 private UserOptionsLookup
$userOptionsLookup;
67 private CentralIdLookup
$centralIdLookup;
68 private UserFactory
$userFactory;
69 private IEmailer
$emailer;
70 private IMessageFormatterFactory
$messageFormatterFactory;
71 private ITextFormatter
$contLangMsgFormatter;
72 private Authority
$sender;
74 /** @var string Temporary property to support the deprecated EmailUserPermissionsErrors hook */
75 private string $editToken = '';
78 * @internal For use by EmailUserFactory.
80 * @param ServiceOptions $options
81 * @param HookContainer $hookContainer
82 * @param UserOptionsLookup $userOptionsLookup
83 * @param CentralIdLookup $centralIdLookup
84 * @param UserFactory $userFactory
85 * @param IEmailer $emailer
86 * @param IMessageFormatterFactory $messageFormatterFactory
87 * @param ITextFormatter $contLangMsgFormatter
88 * @param Authority $sender
90 public function __construct(
91 ServiceOptions
$options,
92 HookContainer
$hookContainer,
93 UserOptionsLookup
$userOptionsLookup,
94 CentralIdLookup
$centralIdLookup,
95 UserFactory
$userFactory,
97 IMessageFormatterFactory
$messageFormatterFactory,
98 ITextFormatter
$contLangMsgFormatter,
101 $options->assertRequiredOptions( self
::CONSTRUCTOR_OPTIONS
);
102 $this->options
= $options;
103 $this->hookRunner
= new HookRunner( $hookContainer );
104 $this->userOptionsLookup
= $userOptionsLookup;
105 $this->centralIdLookup
= $centralIdLookup;
106 $this->userFactory
= $userFactory;
107 $this->emailer
= $emailer;
108 $this->messageFormatterFactory
= $messageFormatterFactory;
109 $this->contLangMsgFormatter
= $contLangMsgFormatter;
111 $this->sender
= $sender;
116 * @todo This method might perhaps be moved to a UserEmailContactLookup or something.
118 * @param UserEmailContact $target Target user
119 * @return StatusValue
121 public function validateTarget( UserEmailContact
$target ): StatusValue
{
122 $targetIdentity = $target->getUser();
124 if ( !$targetIdentity->getId() ) {
125 return StatusValue
::newFatal( 'emailnotarget' );
128 if ( !$target->isEmailConfirmed() ) {
129 return StatusValue
::newFatal( 'noemailtext' );
132 $targetUser = $this->userFactory
->newFromUserIdentity( $targetIdentity );
133 if ( !$targetUser->canReceiveEmail() ) {
134 return StatusValue
::newFatal( 'nowikiemailtext' );
137 $senderUser = $this->userFactory
->newFromAuthority( $this->sender
);
139 !$this->userOptionsLookup
->getOption( $targetIdentity, 'email-allow-new-users' ) &&
140 $senderUser->isNewbie()
142 return StatusValue
::newFatal( 'nowikiemailtext' );
145 $muteList = $this->userOptionsLookup
->getOption(
151 $muteList = MultiUsernameFilter
::splitIds( $muteList );
152 $senderId = $this->centralIdLookup
->centralIdFromLocalUser( $this->sender
->getUser() );
153 if ( $senderId !== 0 && in_array( $senderId, $muteList ) ) {
154 return StatusValue
::newFatal( 'nowikiemailtext' );
158 return StatusValue
::newGood();
162 * Checks whether email sending is allowed.
164 * @return StatusValue For BC, the StatusValue's value can be set to a string representing
165 * a message key to use with ErrorPageError. Only SpecialEmailUser should rely on this.
167 public function canSend(): StatusValue
{
169 !$this->options
->get( MainConfigNames
::EnableEmail
) ||
170 !$this->options
->get( MainConfigNames
::EnableUserEmail
)
172 return StatusValue
::newFatal( 'usermaildisabled' );
175 $user = $this->userFactory
->newFromAuthority( $this->sender
);
177 // Run this before checking 'sendemail' permission
178 // to show appropriate message to anons (T160309)
179 if ( !$user->isEmailConfirmed() ) {
180 return StatusValue
::newFatal( 'mailnologin' );
183 $status = PermissionStatus
::newGood();
184 if ( !$this->sender
->isDefinitelyAllowed( 'sendemail', $status ) ) {
190 // TODO Remove deprecated hooks
191 $this->hookRunner
->onUserCanSendEmail( $user, $hookErr );
192 $this->hookRunner
->onEmailUserPermissionsErrors( $user, $this->editToken
, $hookErr );
193 if ( is_array( $hookErr ) ) {
194 // SpamBlacklist uses null for the third element, and there might be more handlers not using an array.
195 $msgParamsArray = is_array( $hookErr[2] ) ?
$hookErr[2] : [];
196 $ret = StatusValue
::newFatal( $hookErr[1], ...$msgParamsArray );
197 $ret->value
= $hookErr[0];
201 return StatusValue
::newGood();
205 * Authorize the email sending, checking permissions etc.
207 * @return StatusValue For BC, the StatusValue's value can be set to a string representing
208 * a message key to use with ErrorPageError. Only SpecialEmailUser should rely on this.
210 public function authorizeSend(): StatusValue
{
211 $status = $this->canSend();
212 if ( !$status->isOK() ) {
216 $status = PermissionStatus
::newGood();
217 if ( !$this->sender
->authorizeAction( 'sendemail', $status ) ) {
221 $hookRes = $this->hookRunner
->onEmailUserAuthorizeSend( $this->sender
, $status );
222 if ( !$hookRes && !$status->isGood() ) {
226 return StatusValue
::newGood();
230 * Really send a mail, without permission checks.
232 * @param UserEmailContact $target
233 * @param string $subject
234 * @param string $text
236 * @param string $langCode Code of the language to be used for interface messages
237 * @return StatusValue
239 public function sendEmailUnsafe(
240 UserEmailContact
$target,
246 $senderIdentity = $this->sender
->getUser();
247 $targetStatus = $this->validateTarget( $target );
248 if ( !$targetStatus->isGood() ) {
249 return $targetStatus;
252 $senderUser = $this->userFactory
->newFromAuthority( $this->sender
);
254 $toAddress = MailAddress
::newFromUser( $target );
255 $fromAddress = MailAddress
::newFromUser( $senderUser );
257 // Add a standard footer and trim up trailing newlines
258 $text = rtrim( $text ) . "\n\n-- \n";
259 $text .= $this->contLangMsgFormatter
->format(
260 MessageValue
::new( 'emailuserfooter', [ $fromAddress->name
, $toAddress->name
] )
263 if ( $this->options
->get( MainConfigNames
::EnableSpecialMute
) ) {
264 $text .= "\n" . $this->contLangMsgFormatter
->format(
266 'specialmute-email-footer',
268 $this->getSpecialMuteCanonicalURL( $senderIdentity->getName() ),
269 $senderIdentity->getName()
276 // TODO Remove deprecated ugly hook
277 if ( !$this->hookRunner
->onEmailUser( $toAddress, $fromAddress, $subject, $text, $error ) ) {
278 if ( $error instanceof StatusValue
) {
280 } elseif ( $error === false ||
$error === '' ||
$error === [] ) {
281 // Possibly to tell HTMLForm to pretend there was no submission?
282 return StatusValue
::newFatal( 'hookaborted' );
283 } elseif ( $error === true ) {
284 // Hook sent the mail itself and indicates success?
285 return StatusValue
::newGood();
286 } elseif ( is_array( $error ) ) {
287 $status = StatusValue
::newGood();
288 foreach ( $error as $e ) {
289 $status->fatal( $e );
292 } elseif ( $error instanceof MessageSpecifier
) {
293 return StatusValue
::newFatal( $error );
295 // Setting $error to something else was deprecated in 1.29 and
296 // removed in 1.36, and so an exception is now thrown
297 $type = get_debug_type( $error );
298 throw new UnexpectedValueException(
299 'EmailUser hook set $error to unsupported type ' . $type
304 $hookStatus = StatusValue
::newGood();
305 $hookRes = $this->hookRunner
->onEmailUserSendEmail(
314 if ( !$hookRes && !$hookStatus->isGood() ) {
318 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $fromAddress );
320 $status = $this->emailer
->send(
326 [ 'replyTo' => $replyTo ]
329 if ( !$status->isGood() ) {
333 // if the user requested a copy of this mail, do this now,
334 // unless they are emailing themselves, in which case one
335 // copy of the message is sufficient.
336 if ( $CCMe && !$toAddress->equals( $fromAddress ) ) {
337 $userMsgFormatter = $this->messageFormatterFactory
->getTextFormatter( $langCode );
338 $ccTo = $fromAddress;
339 $ccFrom = $fromAddress;
340 $ccSubject = $userMsgFormatter->format(
341 MessageValue
::new( 'emailccsubject' )->plaintextParams(
342 $target->getUser()->getName(),
348 $this->hookRunner
->onEmailUserCC( $ccTo, $ccFrom, $ccSubject, $ccText );
350 [ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $ccFrom );
352 $ccStatus = $this->emailer
->send(
358 [ 'replyTo' => $replyTo ]
360 $status->merge( $ccStatus );
363 $this->hookRunner
->onEmailUserComplete( $toAddress, $fromAddress, $subject, $text );
369 * @param MailAddress $fromAddress
371 * @phan-return array{0:MailAddress,1:?MailAddress}
373 private function getFromAndReplyTo( MailAddress
$fromAddress ): array {
374 if ( $this->options
->get( MainConfigNames
::UserEmailUseReplyTo
) ) {
376 * Put the generic wiki autogenerated address in the From:
377 * header and reserve the user for Reply-To.
379 * This is a bit ugly, but will serve to differentiate
380 * wiki-borne mails from direct mails and protects against
381 * SPF and bounce problems with some mailers (see below).
383 $mailFrom = new MailAddress(
384 $this->options
->get( MainConfigNames
::PasswordSender
),
385 $this->contLangMsgFormatter
->format( MessageValue
::new( 'emailsender' ) )
387 $replyTo = $fromAddress;
390 * Put the sending user's e-mail address in the From: header.
392 * This is clean-looking and convenient, but has issues.
393 * One is that it doesn't as clearly differentiate the wiki mail
394 * from "directly" sent mails.
396 * Another is that some mailers (like sSMTP) will use the From
397 * address as the envelope sender as well. For open sites this
398 * can cause mails to be flunked for SPF violations (since the
399 * wiki server isn't an authorized sender for various users'
400 * domains) as well as creating a privacy issue as bounces
401 * containing the recipient's e-mail address may get sent to
404 $mailFrom = $fromAddress;
407 return [ $mailFrom, $replyTo ];
411 * @param string $targetName
413 * XXX This code is still heavily reliant on global state, so temporarily skip it in tests.
414 * @codeCoverageIgnore
416 private function getSpecialMuteCanonicalURL( string $targetName ): string {
417 if ( defined( 'MW_PHPUNIT_TEST' ) ) {
418 return "Ceci n'est pas une URL";
420 return SpecialPage
::getTitleFor( 'Mute', $targetName )->getCanonicalURL();
424 * @internal Only for BC with SpecialEmailUser
425 * @param string $token
427 public function setEditToken( string $token ): void
{
428 $this->editToken
= $token;