laravel 自动记录邮件发送记录

首先,我们需要知道的是,不管是Notification 还是Mail,laravel是默认有相关的监听器的,例如:

1、Notification的监听器:

protected $listen = [
    'Illuminate\Notifications\Events\NotificationSent' => [
        'App\Listeners\LogNotification',
    ],
];

2、邮件监听器:

protected $listen = [
    'Illuminate\Mail\Events\MessageSending' => [
        'App\Listeners\LogSendingMessage',
    ],
    'Illuminate\Mail\Events\MessageSent' => [
        'App\Listeners\LogSentMessage',
    ],
];

我们需要做的就是把监听器加入到,
D:\phpStudy\WWW\xxx\app\Providers\EventServiceProvider.php

然后运行:

 php artisan event:generate

然后就会生成相应的listener文件,这里,我们可以在handle里卖弄打印三个事件的属性,你会很惊喜的发现,这里面有你需要的东西。然后在这里就把记录的动作完成。

public function handle(NotificationSent $event)
{
    // $event->channel
    // $event->notifiable
    // $event->notification
}

我的代码:

public function handle(NotificationSent $event)
{
    $attributes = [
        'send_type' => 2,
        'customer_id' => $event->notifiable->id,
        'message_type' => 2,
        'message_type_value' => $event->notifiable->email,
        'message_params' => json_encode($event->notification->notifiable, JSON_UNESCAPED_UNICODE) ?? '',
        'status_code' => 200,
        'status' => 1,
        'creator' => $event->notification->notifiable['admin_id'],
        'updator' => $event->notification->notifiable['admin_id'],
    ];
    Message::create($attributes);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SHUIPING_YANG

你的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值