We need to create event listener for "Illuminate\Mail\Events\MessageSending" event. So our listener file would be under "project/app/Listeners" as below. |
<?php namespace App\Listeners; use Illuminate\Support\Facades\Log; use Illuminate\Mail\Events\MessageSending; class MessageSendingListener { public function __construct() { } public function handle(MessageSending $swiftMessage) { $server = env("MODE", "LIVE"); if ($server != "LIVE") { $swiftMessage->message->setSubject($swiftMessage->message->getSubject() . " ($server)"); } Log::info("SENDING EMAIL=".$swiftMessage->message->getSubject()); } } |
The above file will be called before each mail be send. So we can modify or check anything as global option. |
Now we require to register event on EventServiceProvider.php file so, open app/Providers/EventServiceProvider.php and copy this code and put in your file. |
<?php namespace App\Providers; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; 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\SomeEvent' => [ 'App\Listeners\EventListener', ], 'Illuminate\Mail\Events\MessageSending' => [ 'App\Listeners\MessageSendingListener', ], ]; /** * Register any other events for your application. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function boot(DispatcherContract $events) { parent::boot($events); // } } |
This is all. |
Friday, February 8, 2019
Override setGlobalTo of Laravel Mailer| Laravel Message Sending listener | Laravel Mail Sending Listener | How to create Event for Mail sending in Laravel 5
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment