将自定义数据传递到laravel通知类的电子邮件模板

问题描述 投票:0回答:1

我们使用 Laravel 通知和可邮寄类发送电子邮件通知。

我们有以下流程: ** 我们使用的是 Laravel 7 **

  1. 我们派遣工作

    dispatch(new JobEntitiesEmail($arr,$email));

  2. 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']));


}
} 
  1. 我们有 app\Notifications\EntitiesEmail.php,我们在其中创建 MailMessage 对象。

$mailmessage= (new MailMessage)->subject("subject of mail")

->line( __('messages.hello'))

->line(new HtmlString(__('messages.email_message')))

->action(__('titles.click'), url("google.com")); 

  1. 我们在 Resources\View\Vendors\mail 中有一个电子邮件模板
  • 电子邮件和 Markdown ** 我们想要将徽标传递到位于资源 iews endor\mail\html\header.blade.php**
  • 的邮件头文件

像这样:

$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 `

我们已经尝试过

  1. 配合方法使用

 $mailmessage= (new MailMessage)

 ->subject('New order')

->with(['logoUrl' => $logoUrl ])

它提供数据作为大纲。

  1. 使用Markdown

 $mailmessage= (new MailMessage)

 ->subject('New order')

 ->markdown('Notification::email', ['logoUrl' => $logoUrl ]);

我们在 viewData 中获取此数据,但无法在模板文件中访问此数据。

laravel laravel-7 mailmessage laravel-notification
1个回答
0
投票

嗯,我希望还不算太晚,但是我两周前就遇到了这个问题,而且很难找到如何解决它。最后,做了一些调试并使用了很多

dd()
方法。我可以这样解决

  1. 将 $notificable 变量直接传递给
    with()
    方法
public function toMail(object $notifiable): MailMessage
    {
        $mail = (new MailMessage)
            ->subject('Hello World!')
            ->line('Go to our website')
            ->with($notifiable);
    }
  1. 然后在您发布默认通知模板后
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
  1. 然后前往
    resources/views/vendor/mail/html/message.blade.php
  2. 槽变量将包含一个
    Illuminate\Support\HtmlString
    ,它将包含以下信息:
Illuminate\Support\HtmlString {#1856 // resources/views/vendor/mail/html/message.blade.php
  #html: """
    # Hello World!
    
    Go to our website
    
    {&quot;first_names&quot;:&quot;John&quot;,&quot;last_names&quot;:&quot;Doe&quot;,&quot;email&quot;:&quot;[email protected]&quot;,&quot;age&quot;:0}
    """
}

注意:

Illuminate\Support\HtmlString
对象末尾的信息是我们包含的$notifying内容,该变量是User的实例。

User::make([
  'first_names' => 'John',
  'last_names' => 'Doe',
  'email' => '[email protected]'
]);
  1. 总而言之,你只需要使用正则表达式和 PHP 内置的
    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 代码块

  1. 在第一个中,我刚刚删除了来自
    $notifiable
    变量的所有数据,因为这是一个
    Illuminate\Support\HtmlString
    它将显示在电子邮件结构上,这是我们不想要的。
  2. 在第二个中,我只是通过删除 html 实体来提取数据并将其解码为 JSON 格式,以便我可以轻松访问它。

希望对您有所帮助,感谢您的阅读!

如果有更好的方法,请告诉我,我很乐意阅读

© www.soinside.com 2019 - 2024. All rights reserved.