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. |
Showing posts with label listener. Show all posts
Showing posts with label listener. Show all posts
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
Monday, September 26, 2016
File Change Listener Implementation Using Java
package com.pritom.kumar; import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.List; /** * Created by pritom on 25/09/2016. */ public class ChangeFileListener { public static void main(String[] args) throws Exception { startWatch("C:/tmp"); } public static void startWatch(String location) { try { Path directory = Paths.get(location); println("Start listening directory: [" + location + "]"); final WatchService watchService = directory.getFileSystem().newWatchService(); directory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); while (true) { final WatchKey watchKey = watchService.take(); handleWatchTriggered(location, watchKey); boolean valid = watchKey.reset(); if (!valid) { throw new Exception("Watcher failed for listen [" + location + "]"); } } } catch (Exception ex) { ex.printStackTrace(); } } private static void handleWatchTriggered(String location, WatchKey watchKey) throws Exception { List<WatchEvent<?>> watchEventList = watchKey.pollEvents(); for (WatchEvent watchEvent : watchEventList) { if (watchEvent.kind() == StandardWatchEventKinds.ENTRY_CREATE) { println("Created: [" + watchEvent.context().toString() + "]"); File fileCreated = new File(location + "/" + watchEvent.context().toString()); } else if (watchEvent.kind() == StandardWatchEventKinds.ENTRY_DELETE) { println("Deleted: [" + watchEvent.context().toString() + "]"); } else if (watchEvent.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { println("Modified: [" + watchEvent.context().toString() + "]"); } } } public static void println(Object o) { System.out.println("" + o); } }
Subscribe to:
Posts (Atom)