php Laravel Mailable将额外数据传递给活动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel Mailable将额外数据传递给活动相关的知识,希望对你有一定的参考价值。

<?php

namespace App\Listeners;

use Illuminate\Mail\Events\MessageSent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class LogSentEmailNotification
{
    
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        
    }

    /**
     * Handle the event.
     *
     * @param  MessageSent  $event
     * @return void
     */
    public function handle(MessageSent $event)
    {
        //to get extra email data
        //take note this will not work on queue, below the discussion:
        //https://medium.com/@guysmilez/queuing-mailables-with-custom-headers-in-laravel-5-4-ab615f022f17
        dd($event->message->email_data);

        $message = $event->message;

    }
}
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\Event' => [
            'App\Listeners\EventListener',
        ],
        'Illuminate\Mail\Events\MessageSent' => [
            'App\Listeners\LogSentEmailNotification',
        ],
    ];
    
}
<?php

namespace App\Mail;

use App\EmailTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\File;

class CustomEmailNotification extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;
    
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        //get email subject

        $subject = $this->getEmailSubject();

        //get email from

        $from_address =  $this->getEmailFromAddress();

        //get dynamic data
        $email_data = $this->buildEmailData();

        //open markdown mail template
        $email_template_file = 'emails.templates.' . $this->handle;

        //set mailable event MessageSent data

        $this->withSwiftMessage(function ($message) use($email_data){
            $message->email_data = $email_data;
        });

        //prepare and send email

        $mail = $this->markdown($email_template_file);
        $mail = $mail->subject($subject);

        //if empty from address, mail will use default system from address

        if (!empty($from_address)) {
            $mail = $mail->from($from_address);
        }

        $mail = $mail->with($email_data);

        return $mail;
    }
}

以上是关于php Laravel Mailable将额外数据传递给活动的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 laravel Mailable 测试电子邮件的主题

Laravel - 多个收件人的多个阵列(Mailable / Notifications)

Laravel/流明 |未能分发事件

Laravel 5.3 通知与可邮寄

php Laravel为请求添加额外参数

php Laravel的额外碳日期