Laravel Mail issue using nested @components with markdown

Viewed 539

I am experiencing a problem where the code works but sometimes it fails to display the proper info in the outermost @component; the logo it shows is wrong, like if it's caching the outermost layout and using it on the next call, but with outdated info

The issue happens while sending emails, which are run on queues.

The relevant part of the code is as follows:

1: the mail I'm trying to send (works 100% of the time) markdown.blade.php

@component('mail::message')
# @lang('emails.bookingRequestReceipt', ['user' => $association->person->account->first_name, 'school' => $bookingRequest->school->name])

{!! html_entity_decode($custom_intro) ?? '' !!}

@endcomponent

2: the message component (works 100% of the time)

@component('mail::layout')

<!-- THIS IS THE RELEVANT PART -->
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => $school['url'] ?? config('app.url')])
@if(!empty($school['logo']) && !$school['logo']['default'])
<img src="{{ $school['logo']['url'] }}" class="logo" alt="{{ $school['logo']['alt'] }}" />
@elseif(empty($school))
<img src="https://[redacted]/public/img/logo.png" class="logo" alt="{{ config('app.name') }}" />
@else
{{ $school->name }}
@endif
@endcomponent
@endslot
<!-- END OF RELEVANT PART -->

{{-- Body --}}
{{ $slot }}

@endcomponent

3: the layout (fails)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<table class="wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table class="content" width="100%" cellpadding="0" cellspacing="0" role="presentation">

<!-- THIS IS THE RELEVANT PART -->
{{ $header ?? '' }}
<!-- END OF RELEVANT PART -->

<!-- Email Body -->
<tr>
<td class="body" width="100%" cellpadding="0" cellspacing="0">
<table class="inner-body" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<!-- Body content -->
<tr>
<td class="content-cell">
{{ Illuminate\Mail\Markdown::parse($slot) }}

</td>
</tr>
</table>
</td>
</tr>

</table>
</td>
</tr>
</table>
</body>
</html>

Any idea what I'm doing wrong? The informations regarding the $school inside the message are always correct

Is it beacause I haven' used php artisan make:component to create the layout?

EDIT: removed leftover code

2 Answers

I've added the mail files you provided locally to test whats causing the issues. Without knowing how and what you are returning from your Mail class (return $this->markdown(...) ??), I noticed a few things about the code examples:

  1. About the wrong shown logo: You aren't passing the $school variable to the message blade, so your logo never could be shown and the @elseif(empty($school)) will always be true.

    // message.blade.php
    @component('mail::message', ['school' => $school])
    
  2. Your message.blade.php contains too many @end directives, the last two @ directives causing errors in the blade, remove these:

        ...
        {{-- Body --}}
        {{ $slot }}
    
    @endcomponent
    {{-- @endslot -- }} // Causes problems
    {{-- @endcomponent -- }} // Causes problems
    

If I fix the code snippets you provided like I explained in the steps above, everything is working fine. Please let me know if this fix your problems.

In this case, if you are sure 100% that all code lines are correct. Then it would be best if you tried to restart the queue worker on the server.

Related