diff --git a/src/Api/Controller/SendConfirmationEmailController.php b/src/Api/Controller/SendConfirmationEmailController.php index ce228e6809..9c8a499e8c 100644 --- a/src/Api/Controller/SendConfirmationEmailController.php +++ b/src/Api/Controller/SendConfirmationEmailController.php @@ -10,9 +10,8 @@ namespace Flarum\Api\Controller; use Flarum\Http\UrlGenerator; -use Flarum\Mail\Job\SendRawEmailJob; use Flarum\Settings\SettingsRepositoryInterface; -use Flarum\User\EmailToken; +use Flarum\User\AccountActivationMailerTrait; use Flarum\User\Exception\PermissionDeniedException; use Illuminate\Contracts\Queue\Queue; use Illuminate\Support\Arr; @@ -24,6 +23,8 @@ class SendConfirmationEmailController implements RequestHandlerInterface { + use AccountActivationMailerTrait; + /** * @var SettingsRepositoryInterface */ @@ -72,19 +73,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface throw new PermissionDeniedException; } - $token = EmailToken::generate($actor->email, $actor->id); - $token->save(); - - $data = [ - '{username}' => $actor->username, - '{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]), - '{forum}' => $this->settings->get('forum_title') - ]; - - $body = $this->translator->trans('core.email.activate_account.body', $data); - $subject = $this->translator->trans('core.email.activate_account.subject'); + $token = $this->generateToken($actor, $actor->email); + $data = $this->getEmailData($actor, $token); - $this->queue->push(new SendRawEmailJob($actor->email, $subject, $body)); + $this->sendConfirmationEmail($actor, $data); return new EmptyResponse; } diff --git a/src/User/AccountActivationMailer.php b/src/User/AccountActivationMailer.php index 3d6d79b9cc..9585391438 100644 --- a/src/User/AccountActivationMailer.php +++ b/src/User/AccountActivationMailer.php @@ -10,7 +10,6 @@ namespace Flarum\User; use Flarum\Http\UrlGenerator; -use Flarum\Mail\Job\SendRawEmailJob; use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\Event\Registered; use Illuminate\Contracts\Queue\Queue; @@ -18,6 +17,8 @@ class AccountActivationMailer { + use AccountActivationMailerTrait; + /** * @var SettingsRepositoryInterface */ @@ -60,42 +61,9 @@ public function handle(Registered $event) return; } - $data = $this->getEmailData($user, $user->email); - - $body = $this->translator->trans('core.email.activate_account.body', $data); - $subject = '['.$data['{forum}'].'] '.$this->translator->trans('core.email.activate_account.subject'); - - $this->queue->push(new SendRawEmailJob($user->email, $subject, $body)); - } - - /** - * @param User $user - * @param string $email - * @return EmailToken - */ - protected function generateToken(User $user, $email) - { - $token = EmailToken::generate($email, $user->id); - $token->save(); - - return $token; - } - - /** - * Get the data that should be made available to email templates. - * - * @param User $user - * @param string $email - * @return array - */ - protected function getEmailData(User $user, $email) - { - $token = $this->generateToken($user, $email); + $token = $this->generateToken($user, $user->email); + $data = $this->getEmailData($user, $token); - return [ - '{username}' => $user->display_name, - '{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]), - '{forum}' => $this->settings->get('forum_title') - ]; + $this->sendConfirmationEmail($user, $data); } } diff --git a/src/User/AccountActivationMailerTrait.php b/src/User/AccountActivationMailerTrait.php new file mode 100644 index 0000000000..0cb32371aa --- /dev/null +++ b/src/User/AccountActivationMailerTrait.php @@ -0,0 +1,56 @@ +id); + $token->save(); + + return $token; + } + + /** + * Get the data that should be made available to email templates. + * + * @param User $user + * @param EmailToken $token + * @return array + */ + protected function getEmailData(User $user, EmailToken $token) + { + return [ + '{username}' => $user->display_name, + '{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]), + '{forum}' => $this->settings->get('forum_title') + ]; + } + + /** + * @param User $user + * @param array $data + */ + protected function sendConfirmationEmail(User $user, $data) + { + $body = $this->translator->trans('core.email.activate_account.body', $data); + $subject = $this->translator->trans('core.email.activate_account.subject'); + + $this->queue->push(new SendRawEmailJob($user->email, $subject, $body)); + } +}