Reputation: 11
We are sending email notifications using laravel notifications and mailable class.
We have the following process : ** We are using laravel 7 **
We dispatch the JOB
dispatch(new JobEntitiesEmail($arr,$email));
JobEntitiesEmail is a job we specifically wrote to send NOTIFICATIONS
`]
class JobEntitiesEmail implements ShouldQueue
{ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public $tries = 3;
public $arr;
public $email;
public function __construct($arr,$email)
{
$this->arr = $arr;
$this->email = $email;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Notification::route('mail',$this->email)->notify((new EntitiesEmail($this->arr))->locale($this->arr['user_lang']));
}
}
$mailmessage= (new MailMessage)->subject("subject of mail")
->line( __('messages.hello'))
->line(new HtmlString(__('messages.email_message')))
->action(__('titles.click'), url("google.com"));
like this :
$mailmessage= (new MailMessage)
->subject("email_template")
->line( __('messages.hello')..',')
->line(new HtmlString(__('messages.email_message')))
->action(__('titles.link'), url($link));
}
Also want to access the passed variable $logoUrl in file resources\views\vendor\mail\html\message.blade.php
like
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url'), 'logoUrl' => $logoUrl ]) {{ config('app.name') }}
@endcomponent
@endslot
{{-- Body --}}
{{ $slot }}
{{-- Subcopy --}}
@isset($subcopy)
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endisset
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} <a href="https://google.com">{{ config('app.name') }}</a>. @lang(__('messages.copyright'))
@endcomponent
@endslot
@endcomponent `
We have tried
$mailmessage= (new MailMessage)
->subject('New order')
->with(['logoUrl' => $logoUrl ])
It give data as a outrolines.
$mailmessage= (new MailMessage)
->subject('New order')
->markdown('Notification::email', ['logoUrl' => $logoUrl ]);
We get this data in viewData but not able to access this in template files.
Upvotes: 1
Views: 1146
Reputation: 1
Well, I hope it is not too late, but I have had this issue two weeks ago, and it was difficult to find out how to fix it. At the end, doing some debug and use a lot dd()
method. I could solve it like this
with()
methodpublic function toMail(object $notifiable): MailMessage
{
$mail = (new MailMessage)
->subject('Hello World!')
->line('Go to our website')
->with($notifiable);
}
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
resources/views/vendor/mail/html/message.blade.php
Illuminate\Support\HtmlString
this will contain the following info:Illuminate\Support\HtmlString {#1856 // resources/views/vendor/mail/html/message.blade.php
#html: """
# Hello World!
Go to our website
{"first_names":"John","last_names":"Doe","email":"[email protected]","age":0}
"""
}
NOTE: The information at the end of the
Illuminate\Support\HtmlString
object is the $notifiable content we included and this variable is an instance of User.
User::make([
'first_names' => 'John',
'last_names' => 'Doe',
'email' => '[email protected]'
]);
html_entity_decode
and json_decode
to get the data, like this:<x-mail::layout>
{{-- Header --}}
<x-slot:header>
<x-mail::header :url="config('app.url')">
</x-mail::header>
</x-slot:header>
@php
$htmlString = $slot->toHtml();
$startIndex = strpos($htmlString, '{');
$userJson = substr($htmlString, $startIndex);
$htmlStringWithoutJson = str_replace($userJson, '', $htmlString);
$bodySlot = new Illuminate\Support\HtmlString($htmlStringWithoutJson);
@endphp
{{-- Body --}}
{{ $bodySlot }}
{{-- Footer --}}
<x-slot:footer>
<x-mail::footer>
@php
$htmlString = $slot->toHtml();
$startIndex = strpos($htmlString, '{');
$userJson = substr($htmlString, $startIndex);
$userJson = html_entity_decode($userJson);
$user = json_decode($userJson);
@endphp
<p>
<div class="footer-primary">
This email was sent to <span class="email">{{$user->email ?? '[email protected]' }}</span>.
</div>
</p>
{{-- YOUR CODE ... --}}
NOTE: If you pay close attention, I used two PHP code block in the view
$notifiable
variable, since this is an Illuminate\Support\HtmlString
it will be shown on the email structure, which is what we do not want.I hope it helps, thanks for reading!
IF THERE IS A BETTER WAY TO DO IT, PLEASE LET ME KNOW, I WILL BE HAPPY TO READ IT
Upvotes: 0