php 使用事件的Laravel Mailable电子邮件日志

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 使用事件的Laravel Mailable电子邮件日志相关的知识,希望对你有一定的参考价值。

<?php

namespace App\Listeners;

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

class LogSentEmailNotification
{
    protected $email_notification;
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(EmailNotification $email_notification)
    {
        $this->email_notification = $email_notification;
    }

    /**
     * Handle the event.
     *
     * @param  MessageSent  $event
     * @return void
     */
    public function handle(MessageSent $event)
    {
        $message = $event->message;

        $data = [
            'to' => !$message->getHeaders()->get('To') ? null : $message->getHeaders()->get('To')->getFieldBody(),
            'from' => !$message->getHeaders()->get('From') ? null : $message->getHeaders()->get('From')->getFieldBody(),
            'cc' => !$message->getHeaders()->get('Cc') ? null : $message->getHeaders()->get('Cc')->getFieldBody(),
            'bcc' => !$message->getHeaders()->get('Bcc') ? null : $message->getHeaders()->get('Bcc')->getFieldBody(),
            'subject' => $message->getHeaders()->get('Subject')->getFieldBody(),
            'body' => $message->getBody(),
        ];

        $email_notification_log = $this->email_notification->create($data);
    }
}
<?php

namespace App;

use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class EmailNotification extends Model
{
    use SoftDeletes;
    use Uuid;

    protected $fillable = [
        'to',
        'from',
        'cc',
        'bcc',
        'subject',
        'body',
    ];
}
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEmailNotificationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('email_notifications', function (Blueprint $table) {
            $table->increments('id');
            $table->uuid('uuid');
            $table->string('to')->nullable();
            $table->string('from')->nullable();
            $table->string('cc')->nullable();
            $table->string('bcc')->nullable();
            $table->string('subject')->nullable();
            $table->text('body')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('email_notifications');
    }
}
<?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 使用事件的Laravel Mailable电子邮件日志的主要内容,如果未能解决你的问题,请参考以下文章

Laravel/流明 |未能分发事件

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

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

Laravel 5.3 通知与可邮寄

Lumen 5.4 与 Laravel 可邮寄

Laravel 5.4 更改markdown邮件的主题