我们使用 Laravel 通知和可邮寄类发送电子邮件通知。
我们有以下流程: ** 我们使用的是 Laravel 7 **
我们派遣工作
dispatch(new JobEntitiesEmail($arr,$email));
JobEntitiesEmail 是我们专门编写的用于发送通知的作业
`]
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"));
像这样:
$mailmessage= (new MailMessage)
->subject("email_template")
->line( __('messages.hello')..',')
->line(new HtmlString(__('messages.email_message')))
->action(__('titles.link'), url($link));
}
还想访问文件资源iews endor\mail\html\message.blade.php中传递的变量$logoUrl
喜欢
@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 `
我们已经尝试过
$mailmessage= (new MailMessage)
->subject('New order')
->with(['logoUrl' => $logoUrl ])
它提供数据作为大纲。
$mailmessage= (new MailMessage)
->subject('New order')
->markdown('Notification::email', ['logoUrl' => $logoUrl ]);
我们在 viewData 中获取此数据,但无法在模板文件中访问此数据。
嗯,我希望还不算太晚,但是我两周前就遇到了这个问题,而且很难找到如何解决它。最后,做了一些调试并使用了很多
dd()
方法。我可以这样解决
with()
方法public 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
,它将包含以下信息: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}
"""
}
注意:
对象末尾的信息是我们包含的$notifying内容,该变量是User的实例。Illuminate\Support\HtmlString
User::make([
'first_names' => 'John',
'last_names' => 'Doe',
'email' => '[email protected]'
]);
html_entity_decode
和 json_decode
来获取数据,如下所示:<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 ... --}}
注意:如果您仔细观察,我在视图中使用了两个 PHP 代码块
$notifiable
变量的所有数据,因为这是一个 Illuminate\Support\HtmlString
它将显示在电子邮件结构上,这是我们不想要的。希望对您有所帮助,感谢您的阅读!
如果有更好的方法,请告诉我,我很乐意阅读