MediaWiki master
ConfirmEmailSender.php
Go to the documentation of this file.
1<?php
2
4
5use LogicException;
15use StatusValue;
16
21
22 public const EMAIL_TYPE_CREATED = 'created';
23 public const EMAIL_TYPE_CHANGED = 'changed';
24 public const EMAIL_TYPE_SET = 'set';
25
26 public const CONSTRUCTOR_OPTIONS = [
28 ];
29
30 public function __construct(
31 private readonly ServiceOptions $options,
32 private readonly HookRunner $hookRunner,
33 private readonly UserFactory $userFactory,
34 private readonly IEmailer $emailer,
35 private readonly ConfirmEmailBuilderFactory $confirmEmailBuilderFactory
36 ) {
37 $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
38 }
39
40 private function buildEmailByType(
41 IContextSource $ctx,
42 string $type, ConfirmEmailData $data
44 $builder = $this->confirmEmailBuilderFactory->newFromContext( $ctx );
45 return match ( $type ) {
46 self::EMAIL_TYPE_CREATED => $builder->buildEmailCreated( $data ),
47 self::EMAIL_TYPE_CHANGED => $builder->buildEmailChanged( $data ),
48 self::EMAIL_TYPE_SET => $builder->buildEmailSet( $data ),
49 default => throw new LogicException(
50 '$type "' . $type . '" is not any of the supported types'
51 )
52 };
53 }
54
62 private function sendEmailToRecipient(
63 MessageLocalizer $localizer,
64 User $recipientUser,
65 ConfirmEmailContent $emailContent
66 ): StatusValue {
67 $sender = new MailAddress(
68 $this->options->get( MainConfigNames::PasswordSender ),
69 $localizer->msg( 'emailsender' )->inContentLanguage()->text()
70 );
71
72 return $this->emailer->send(
73 [ MailAddress::newFromUser( $recipientUser ) ],
74 $sender,
75 $emailContent->getSubject(),
76 $emailContent->getPlaintext(),
77 $emailContent->getHtml()
78 );
79 }
80
89 public function sendConfirmationMail(
90 IContextSource $ctx,
91 string $type, ConfirmEmailData $data
92 ): StatusValue {
93 $emailContent = $this->buildEmailByType( $ctx, $type, $data );
94 $recipientUser = $this->userFactory->newFromUserIdentity( $data->getRecipientUser() );
95
96 $emailAsArray = $emailContent->toArray();
97 $this->hookRunner->onUserSendConfirmationMail(
98 $recipientUser,
99 $emailAsArray,
100 [
101 'type' => $type,
102 'ip' => $ctx->getRequest()->getIP(),
103 'confirmURL' => $data->getConfirmationUrl(),
104 'invalidateURL' => $data->getInvalidationUrl(),
105 'expiration' => $data->getUrlExpiration(),
106 ]
107 );
108 $emailContent = ConfirmEmailContent::newFromArray( $emailAsArray );
109
110 return $this->sendEmailToRecipient( $ctx, $recipientUser, $emailContent );
111 }
112}
A class for passing options to services.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Value class defining an email sent to the user.
Value class wrapping variables present in the confirmation email.
Wrapper for IEmailer for sending confirm email sender.
sendConfirmationMail(IContextSource $ctx, string $type, ConfirmEmailData $data)
Send the email address confirmation message.
__construct(private readonly ServiceOptions $options, private readonly HookRunner $hookRunner, private readonly UserFactory $userFactory, private readonly IEmailer $emailer, private readonly ConfirmEmailBuilderFactory $confirmEmailBuilderFactory)
Represent and format a single name and email address pair for SMTP.
static newFromUser(UserEmailContact $user)
A class containing constants representing the names of configuration variables.
const PasswordSender
Name constant for the PasswordSender setting, for use with Config::get()
Create User objects.
User class for the MediaWiki software.
Definition User.php:130
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Interface for objects which can provide a MediaWiki context on request.
Interface for localizing messages in MediaWiki.
Interface for sending arbitrary emails.
Definition IEmailer.php:20