MediaWiki master
ConfirmEmailSender.php
Go to the documentation of this file.
1<?php
2
4
5use LogicException;
15use Psr\Log\LoggerInterface;
16use StatusValue;
17
22
23 public const string EMAIL_TYPE_CREATED = 'created';
24 public const string EMAIL_TYPE_CHANGED = 'changed';
25 public const string EMAIL_TYPE_SET = 'set';
26
27 public const array CONSTRUCTOR_OPTIONS = [
29 ];
30
31 public function __construct(
32 private readonly ServiceOptions $options,
33 private readonly HookRunner $hookRunner,
34 private readonly UserFactory $userFactory,
35 private readonly IEmailer $emailer,
36 private readonly ConfirmEmailBuilderFactory $confirmEmailBuilderFactory,
37 private readonly LoggerInterface $logger,
38 ) {
39 $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
40 }
41
42 private function buildEmailByType(
43 IContextSource $ctx,
44 string $type, ConfirmEmailData $data
46 $builder = $this->confirmEmailBuilderFactory->newFromContext( $ctx );
47 return match ( $type ) {
48 self::EMAIL_TYPE_CREATED => $builder->buildEmailCreated( $data ),
49 self::EMAIL_TYPE_CHANGED => $builder->buildEmailChanged( $data ),
50 self::EMAIL_TYPE_SET => $builder->buildEmailSet( $data ),
51 default => throw new LogicException(
52 '$type "' . $type . '" is not any of the supported types'
53 )
54 };
55 }
56
64 private function sendEmailToRecipient(
65 MessageLocalizer $localizer,
66 User $recipientUser,
67 ConfirmEmailContent $emailContent
68 ): StatusValue {
69 $sender = new MailAddress(
70 $this->options->get( MainConfigNames::PasswordSender ),
71 $localizer->msg( 'emailsender' )->inContentLanguage()->text()
72 );
73
74 return $this->emailer->send(
75 [ MailAddress::newFromUser( $recipientUser ) ],
76 $sender,
77 $emailContent->getSubject(),
78 $emailContent->getPlaintext(),
79 $emailContent->getHtml()
80 );
81 }
82
91 public function sendConfirmationMail(
92 IContextSource $ctx,
93 string $type, ConfirmEmailData $data
94 ): StatusValue {
95 $emailContent = $this->buildEmailByType( $ctx, $type, $data );
96 $recipientUser = $this->userFactory->newFromUserIdentity( $data->getRecipientUser() );
97
98 $emailAsArray = $emailContent->toArray();
99 $this->hookRunner->onUserSendConfirmationMail(
100 $recipientUser,
101 $emailAsArray,
102 [
103 'type' => $type,
104 'ip' => $ctx->getRequest()->getIP(),
105 'confirmURL' => $data->getConfirmationUrl(),
106 'invalidateURL' => $data->getInvalidationUrl(),
107 'expiration' => $data->getUrlExpiration(),
108 ]
109 );
110 $emailContent = ConfirmEmailContent::newFromArray( $emailAsArray );
111
112 $status = $this->sendEmailToRecipient( $ctx, $recipientUser, $emailContent );
113
114 if ( $status->isGood() ) {
115 $this->logger->info( 'Confirmation email sent', [
116 'event' => 'email_confirmation_sent',
117 'type' => $type,
118 ] );
119 } else {
120 $this->logger->warning( 'Confirmation email failed', [
121 'event' => 'email_confirmation_send_failed',
122 'type' => $type,
123 ] );
124 }
125
126 return $status;
127 }
128}
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.
__construct(private readonly ServiceOptions $options, private readonly HookRunner $hookRunner, private readonly UserFactory $userFactory, private readonly IEmailer $emailer, private readonly ConfirmEmailBuilderFactory $confirmEmailBuilderFactory, private readonly LoggerInterface $logger,)
sendConfirmationMail(IContextSource $ctx, string $type, ConfirmEmailData $data)
Send the email address confirmation message.
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:129
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