feat: use components for email templating (#4102)
Some checks are pending
Backend Tests / run (push) Waiting to run
Frontend Workflow / run (push) Waiting to run
Static Code Analysis / run (push) Waiting to run

This commit is contained in:
Sami Mazouz 2024-11-02 12:09:51 +01:00 committed by GitHub
parent 0ac01b9053
commit 0c32184775
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 193 additions and 178 deletions

View File

@ -1,13 +1,13 @@
@extends('flarum.forum::email.html.notification.base')
<x-mail::html.notification>
<x-slot:body>
{!! $formatter->convert($translator->trans('flarum-mentions.email.group_mentioned.html.body', [
'{mentioner_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number])
])) !!}
</x-slot:body>
@section('notificationContent')
{!! $formatter->convert($translator->trans('flarum-mentions.email.group_mentioned.html.body', [
'{mentioner_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number])
])) !!}
@endsection
@section('contentPreview')
{!! $blueprint->post->formatContent() !!}
@endsection
<x-slot:preview>
{!! $blueprint->post->formatContent() !!}
</x-slot:preview>
</x-mail::html.notification>

View File

@ -1,14 +1,14 @@
@extends('flarum.forum::email.html.notification.base')
<x-mail::html.notification>
<x-slot:body>
{!! $formatter->convert($translator->trans('flarum-mentions.email.post_mentioned.html.body', [
'{replier_display_name}' => $blueprint->reply->user->display_name,
'{post_number}' => $blueprint->post->number,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->reply->discussion_id, 'near' => $blueprint->reply->number])
])) !!}
</x-slot:body>
@section('notificationContent')
{!! $formatter->convert($translator->trans('flarum-mentions.email.post_mentioned.html.body', [
'{replier_display_name}' => $blueprint->reply->user->display_name,
'{post_number}' => $blueprint->post->number,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->reply->discussion_id, 'near' => $blueprint->reply->number])
])) !!}
@endsection
@section('contentPreview')
{!! $blueprint->reply->formatContent() !!}
@endsection
<x-slot:preview>
{!! $blueprint->reply->formatContent() !!}
</x-slot:preview>
</x-mail::html.notification>

View File

@ -1,13 +1,13 @@
@extends('flarum.forum::email.html.notification.base')
<x-mail::html.notification>
<x-slot:body>
{!! $formatter->convert($translator->trans('flarum-mentions.email.user_mentioned.html.body', [
'{mentioner_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number])
])) !!}
</x-slot:body>
@section('notificationContent')
{!! $formatter->convert($translator->trans('flarum-mentions.email.user_mentioned.html.body', [
'{mentioner_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number])
])) !!}
@endsection
@section('contentPreview')
{!! $blueprint->post->formatContent() !!}
@endsection
<x-slot:preview>
{!! $blueprint->post->formatContent() !!}
</x-slot:preview>
</x-mail::html.notification>

View File

@ -1,10 +1,10 @@
@extends('flarum.forum::email.plain.notification.base')
@section('content')
<x-mail::plain.notification>
<x-slot:body>
{!! $translator->trans('flarum-mentions.email.group_mentioned.plain.body', [
'{mentioner_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number]),
'{content}' => $blueprint->post->content
]) !!}
@endsection
</x-slot:body>
</x-mail::plain.notification>

View File

@ -1,6 +1,5 @@
@extends('flarum.forum::email.plain.notification.base')
@section('content')
<x-mail::plain.notification>
<x-slot:body>
{!! $translator->trans('flarum-mentions.email.post_mentioned.plain.body', [
'{replier_display_name}' => $blueprint->reply->user->display_name,
'{post_number}' => $blueprint->post->number,
@ -8,4 +7,5 @@
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->reply->discussion_id, 'near' => $blueprint->reply->number]),
'{content}' => $blueprint->reply->content
]) !!}
@endsection
</x-slot:body>
</x-mail::plain.notification>

View File

@ -1,10 +1,10 @@
@extends('flarum.forum::email.plain.notification.base')
@section('content')
<x-mail::plain.notification>
<x-slot:body>
{!! $translator->trans('flarum-mentions.email.user_mentioned.plain.body', [
'{mentioner_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number]),
'{content}' => $blueprint->post->content
]) !!}
@endsection
</x-slot:body>
</x-mail::plain.notification>

View File

@ -2,15 +2,15 @@
/** @var \Flarum\Messages\Notification\MessageReceivedBlueprint $blueprint */
@endphp
@extends('flarum.forum::email.html.notification.base')
<x-mail::html.notification>
<x-slot:body>
{!! $formatter->convert($translator->trans('flarum-messages.email.message_received.html.body', [
'{user_display_name}' => $blueprint->message->user->display_name,
'{url}' => $url->to('forum')->route('messages.dialog', ['id' => $blueprint->message->dialog_id, 'near' => $blueprint->message->id])
])) !!}
</x-slot:body>
@section('notificationContent')
{!! $formatter->convert($translator->trans('flarum-messages.email.message_received.html.body', [
'{user_display_name}' => $blueprint->message->user->display_name,
'{url}' => $url->to('forum')->route('messages.dialog', ['id' => $blueprint->message->dialog_id, 'near' => $blueprint->message->id])
])) !!}
@endsection
@section('contentPreview')
{!! $blueprint->message->formatContent() !!}
@endsection
<x-slot:preview>
{!! $blueprint->message->formatContent() !!}
</x-slot:preview>
</x-mail::html.notification>

View File

@ -2,12 +2,12 @@
/** @var \Flarum\Messages\Notification\MessageReceivedBlueprint $blueprint */
@endphp
@extends('flarum.forum::email.plain.notification.base')
@section('content')
<x-mail::plain.notification>
<x-slot:body>
{!! $translator->trans('flarum-messages.email.message_received.plain.body', [
'{user_display_name}' => $blueprint->message->user->display_name,
'{url}' => $url->to('forum')->route('messages.dialog', ['id' => $blueprint->message->dialog_id, 'near' => $blueprint->message->id]),
'{content}' => $blueprint->message->content
]) !!}
@endsection
</x-slot:body>
</x-mail::plain.notification>

View File

@ -1,13 +1,13 @@
@extends('flarum.forum::email.html.notification.base')
<x-mail::html.notification>
<x-slot:body>
{!! $formatter->convert($translator->trans('flarum-subscriptions.email.new_post.html.body', [
'{poster_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number])
])) !!}
</x-slot:body>
@section('notificationContent')
{!! $formatter->convert($translator->trans('flarum-subscriptions.email.new_post.html.body', [
'{poster_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number])
])) !!}
@endsection
@section('contentPreview')
{!! $blueprint->post->formatContent() !!}
@endsection
<x-slot:preview>
{!! $blueprint->post->formatContent() !!}
</x-slot:preview>
</x-mail::html.notification>

View File

@ -1,10 +1,10 @@
@extends('flarum.forum::email.plain.notification.base')
@section('content')
<x-mail::plain.notification>
<x-slot:body>
{!! $translator->trans('flarum-subscriptions.email.new_post.plain.body', [
'{poster_display_name}' => $blueprint->post->user->display_name,
'{title}' => $blueprint->post->discussion->title,
'{url}' => $url->to('forum')->route('discussion', ['id' => $blueprint->post->discussion_id, 'near' => $blueprint->post->number]),
'{content}' => $blueprint->post->content
]) !!}
@endsection
</x-slot:body>
</x-mail::plain.notification>

View File

@ -1,11 +1,11 @@
@extends('flarum.forum::email.html.information.base')
<x-mail::html.information>
<x-slot:body>
{!! $formatter->convert($translator->trans('flarum-suspend.email.suspended.html.body', [
'{forumTitle}' => $settings->get('forum_title')
])) !!}
</x-slot:body>
@section('informationContent')
{!! $formatter->convert($translator->trans('flarum-suspend.email.suspended.html.body', [
'{forumTitle}' => $settings->get('forum_title')
])) !!}
@endsection
@section('contentPreview')
{!! $formatter->convert($blueprint->user->suspend_message ?? $translator->trans('flarum-suspend.email.no_reason_given')) !!}
@endsection
<x-slot:preview>
{!! $formatter->convert($blueprint->user->suspend_message ?? $translator->trans('flarum-suspend.email.no_reason_given')) !!}
</x-slot:preview>
</x-mail::html.information>

View File

@ -1,8 +1,8 @@
@extends('flarum.forum::email.html.information.base')
@section('content')
{!! $formatter->convert($translator->trans('flarum-suspend.email.unsuspended.html.body', [
'{forumTitle}' => $settings->get('forum_title'),
'{forum_url}' => $url->to('forum')->base(),
])) !!}
@endsection
<x-mail::html.information>
<x-slot:body>
{!! $formatter->convert($translator->trans('flarum-suspend.email.unsuspended.html.body', [
'{forumTitle}' => $settings->get('forum_title'),
'{forum_url}' => $url->to('forum')->base(),
])) !!}
</x-slot:body>
</x-mail::html.information>

View File

@ -1,7 +1,7 @@
@extends('flarum.forum::email.plain.information.base')
@section('content')
<x-mail::plain.information>
<x-slot:body>
{!! $translator->trans('flarum-suspend.email.suspended.plain.body', [
'{suspension_message}' => $blueprint->user->suspend_message ?? $translator->trans('flarum-suspend.email.no_reason_given'),
]) !!}
@endsection
</x-slot:body>
</x-mail::plain.information>

View File

@ -1,7 +1,7 @@
@extends('flarum.forum::email.plain.information.base')
@section('content')
<x-mail::plain.information>
<x-slot:body>
{!! $translator->trans('flarum-suspend.email.unsuspended.plain.body', [
'{forum_url}' => $url->to('forum')->base(),
]) !!}
@endsection
</x-slot:body>
</x-mail::plain.information>

View File

@ -11,6 +11,7 @@ namespace Flarum\Mail\Job;
use Flarum\Queue\AbstractJob;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\View\Factory;
use Illuminate\Mail\Message;
class SendInformationalEmailJob extends AbstractJob
@ -23,13 +24,13 @@ class SendInformationalEmailJob extends AbstractJob
private readonly string $forumTitle,
private readonly ?string $bodyTitle = null,
protected array $views = [
'text' => 'flarum.forum::email.plain.information.base',
'html' => 'flarum.forum::email.html.information.base'
'text' => 'mail::plain.information.generic',
'html' => 'mail::html.information.generic'
]
) {
}
public function handle(Mailer $mailer): void
public function handle(Mailer $mailer, Factory $view): void
{
$forumTitle = $this->forumTitle;
$infoContent = $this->body;
@ -37,9 +38,11 @@ class SendInformationalEmailJob extends AbstractJob
$title = $this->bodyTitle;
$username = $this->displayName;
$view->share(compact('forumTitle', 'userEmail', 'title', 'username'));
$mailer->send(
$this->views,
compact('forumTitle', 'infoContent', 'userEmail', 'title', 'username'),
compact('infoContent'),
function (Message $message) {
$message->to($this->email);
$message->subject($this->subject);

View File

@ -83,6 +83,10 @@ class MailServiceProvider extends AbstractServiceProvider
});
$this->container->alias('mailer', MailerContract::class);
$this->container->afterResolving(\Illuminate\Contracts\View\Factory::class, function (\Illuminate\Contracts\View\Factory $blade) {
$blade->addNamespace('mail', __DIR__.'/../../views/email');
});
}
public function boot(Dispatcher $events): void

View File

@ -15,6 +15,7 @@ use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\View\Factory;
use Illuminate\Mail\Message;
use Illuminate\Support\Arr;
@ -24,7 +25,8 @@ class NotificationMailer
protected Mailer $mailer,
protected TranslatorInterface $translator,
protected SettingsRepositoryInterface $settings,
protected UrlGenerator $url
protected UrlGenerator $url,
protected Factory $view,
) {
}
@ -44,9 +46,13 @@ class NotificationMailer
$username = $user->display_name;
$userEmail = $user->email;
$data = compact('blueprint', 'user', 'unsubscribeLink', 'settingsLink', 'type', 'forumTitle', 'username', 'userEmail');
$this->view->share($data);
$this->mailer->send(
$this->getEmailViews($blueprint),
compact('blueprint', 'user', 'unsubscribeLink', 'settingsLink', 'type', 'forumTitle', 'username', 'userEmail'),
$data,
function (Message $message) use ($blueprint, $user) {
$message->to($user->email, $user->display_name)
->subject($blueprint->getEmailSubject($this->translator));

View File

@ -57,7 +57,7 @@
@endif
</a>
</div>
@yield('header')
{{ $header ?? '' }}
</div>
<div class="content">
@ -67,7 +67,7 @@
</div>
@endif
<div class="main-content">
@yield('content')
{{ $content ?? '' }}
</div>
@if(!isset($signoff) || $signoff !== false)
<div class="signoff">
@ -77,7 +77,7 @@
</div>
<div class="footer">
@yield('footer')
{{ $footer ?? '' }}
</div>
</body>

View File

@ -0,0 +1,18 @@
<x-mail::html>
<x-slot:header>
<h2>{{ $title ?? $translator->trans('core.email.informational.default_title') }}</h2>
</x-slot:header>
<x-slot:content>
{{ $slot ?? $body ?? '' }}
@if (isset($preview))
<div class="content-preview">
{{ $preview }}
</div>
@endif
</x-slot:content>
<x-slot:footer>
<p>{!! $formatter->convert($translator->trans('core.email.informational.footer', ['userEmail' => $userEmail, 'forumUrl' => $url->to('forum')->base(), 'forumTitle' => $settings->get('forum_title')])) !!}</p>
</x-slot:footer>
</x-mail::html>

View File

@ -1,22 +0,0 @@
@extends('flarum.forum::email.html.base')
@section('header')
<h2>{{ $title ?? $translator->trans('core.email.informational.default_title') }}</h2>
@endsection
@section('content')
@if(isset($infoContent))
<p>{!! $formatter->convert($infoContent) !!}</p>
@else
@yield('informationContent')
@endif
@hasSection('contentPreview')
<div class="content-preview">
@yield('contentPreview')
</div>
@endif
@endsection
@section('footer')
<p>{!! $formatter->convert($translator->trans('core.email.informational.footer', ['userEmail' => $userEmail, 'forumUrl' => $url->to('forum')->base(), 'forumTitle' => $settings->get('forum_title')])) !!}</p>
@endsection

View File

@ -0,0 +1,3 @@
<x-mail::html.information>
{{ $infoContent ?? '' }}
</x-mail::html.information>

View File

@ -0,0 +1,20 @@
<x-mail::html>
<x-slot:header>
<h2>{{ $title ?? $translator->trans('core.email.notification.default_title') }}</h2>
</x-slot:header>
<x-slot:content>
{{ $slot ?? $body ?? '' }}
@if (isset($preview))
<div class="content-preview">
{{ $preview }}
</div>
@endif
</x-slot:content>
<x-slot:footer>
<p>{!! $formatter->convert($translator->trans('core.email.notification.footer.main_text', ['email' => $user->email, 'type' => $type, 'forumUrl' => $url->to('forum')->base(), 'forumTitle' => $settings->get('forum_title')])) !!}</p>
<p>{!! $formatter->convert($translator->trans('core.email.notification.footer.unsubscribe_text', ['unsubscribeLink' => $unsubscribeLink])) !!}</p>
<p>{!! $formatter->convert($translator->trans('core.email.notification.footer.settings_text', ['settingsLink' => $settingsLink])) !!}</p>
</x-slot:footer>
</x-mail::html>

View File

@ -1,20 +0,0 @@
@extends('flarum.forum::email.html.base')
@section('header')
<h2>{{ $title ?? $translator->trans('core.email.notification.default_title') }}</h2>
@endsection
@section('content')
@yield('notificationContent')
@hasSection('contentPreview')
<div class="content-preview">
@yield('contentPreview')
</div>
@endif
@endsection
@section('footer')
<p>{!! $formatter->convert($translator->trans('core.email.notification.footer.main_text', ['email' => $user->email, 'type' => $type, 'forumUrl' => $url->to('forum')->base(), 'forumTitle' => $settings->get('forum_title')])) !!}</p>
<p>{!! $formatter->convert($translator->trans('core.email.notification.footer.unsubscribe_text', ['unsubscribeLink' => $unsubscribeLink])) !!}</p>
<p>{!! $formatter->convert($translator->trans('core.email.notification.footer.settings_text', ['settingsLink' => $settingsLink])) !!}</p>
@endsection

View File

@ -1,14 +1,14 @@
@yield('header')
{{ strip_tags($header ?? '') }}
@if(!isset($greeting) || $greeting !== false)
{{ $translator->trans('core.email.greeting', ['displayName' => $username]) }}
@endif
@yield('content')
{{ strip_tags($content ?? '') }}
@if(!isset($signoff) || $signoff !== false)
- {{ $translator->trans('core.email.signoff', ['forumTitle' => $settings->get('forum_title')]) }} -
@endif
@yield('footer')
{{ strip_tags($footer ?? '') }}

View File

@ -1,13 +1,13 @@
@extends('flarum.forum::email.plain.base')
@section('header')
<x-mail::plain>
<x-slot:header>
{{ $title ?? $translator->trans('core.email.informational.default_title') }}
@endsection
</x-slot:header>
@section('content')
{{ $infoContent ?? '' }}
@endsection
<x-slot:content>
{{ $slot ?? $body ?? '' }}
</x-slot:content>
@section('footer')
<x-slot:footer>
{!! $translator->trans('core.email.informational.footer_plain', ['userEmail' => $userEmail, 'forumTitle' => $forumTitle]) !!}
@endsection
</x-slot:footer>
</x-mail::plain>

View File

@ -0,0 +1,3 @@
<x-mail::plain.information>
{{ $infoContent ?? '' }}
</x-mail::plain.information>

View File

@ -1,17 +1,17 @@
@extends('flarum.forum::email.plain.base')
@section('header')
<x-mail::plain>
<x-slot:header>
{{ $title ?? $translator->trans('core.email.notification.default_title') }}
@endsection
</x-slot:header>
@section('content')
@yield('notificationContent')
@endsection
<x-slot:content>
{{ $slot ?? $body ?? '' }}
</x-slot:content>
@section('footer')
<x-slot:footer>
{!! $translator->trans('core.email.notification.footer.main_text_plain', ['email' => $user->email, 'type' => $type, 'forumTitle' => $forumTitle]) !!}
{!! $translator->trans('core.email.notification.footer.unsubscribe_text_plain', ['unsubscribeLink' => $unsubscribeLink]) !!}
{!! $translator->trans('core.email.notification.footer.settings_text_plain', ['settingsLink' => $settingsLink]) !!}
@endsection
</x-slot:footer>
</x-mail::plain>