<?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',
],
];
}