gpt4 book ai didi

laravel-4 - Laravel 邮件监听器(发送前 - 能够取消)

转载 作者:行者123 更新时间:2023-12-02 07:27:00 26 4
gpt4 key购买 nike

有没有办法使用 Laravel 的“Event”类在发送每封电子邮件之前运行一些代码?我还希望能够取消 Mail::send();

当然,我可以在发送电子邮件之前执行此操作:

Event::fire('email.beforeSend');

然后我可以这样听:

Event::listen('email.beforeSend', function()
{
//my code here
});

问题是我必须记住运行 Event::fire() 我不想这样做。就我而言,我会根据退订列表退回电子邮件地址,以确保我不会发送任何垃圾邮件。

最佳答案

对于 Laravel 5,如果您想取消发送消息,您可以监听 Illuminate\Mail\Events\MessageSending 事件并返回 false

将其添加到您的EventServiceProvider

<?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 = [
'Illuminate\Mail\Events\MessageSending' => [
'App\Listeners\CheckEmailPreferences',
],
];
}

这是我的事件监听器,位于 App\Listeners\CheckEmailPreferences

<?php

namespace App\Listeners;

use App\User;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Facades\App;

class CheckEmailPreferences
{
public function handle( MessageSending $event )
{
//if app is in prod and we don't want to send any emails
if(App::environment() === 'production' && ! env('SEND_MAIL_TO_CUSTOMERS')){
return false;
}

//you can check if the user wants to receive emails here
}
}

关于laravel-4 - Laravel 邮件监听器(发送前 - 能够取消),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27806861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com