Laravel 8 Send Mail , Event And Listener Example



php artisan make:listener WelcomeEvent
php artisan make:listener WelcomeListener --event=WelcomeEvent
php artisan make:mail WelcomeMail -m emails.welcome
******************************************
Event: WelcomeEvent
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class WelcomeEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $email = '';
    public function __construct($email)
    {
        $this->email = $email;
    }
    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
//     public function broadcastOn()
//     {
//         return new PrivateChannel('channel-name');
//     }
}
**********************************************************
Listener: WelcomeListener
<?php
namespace App\Listeners;
use App\Events\WelcomeEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeMail;
class WelcomeListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    /**
     * Handle the event.
     *
     * @param  \App\Events\WelcomeEvent  $event
     * @return void
     */
    public function handle(WelcomeEvent $event)
    {
        Mail::to('rampukar@gmail.com')->send(new WelcomeMail());
    }
}
***************************************************************
Mail: WelcomeMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.welcome')
        ->subject('To Apply For Job PHP')
        ->attach(public_path('filebox/profile/god.jpg'),[
            'as' => 'some_think_app.jpg',
            'mime' => 'application/jpg'
        ]);
    }
}
**************************************************************
views
emails:welcome.blade.php
@component('mail::message')
# Introduction
The body of your message.
@component('mail::button', ['url' => 'https://yig.sakura.ne.jp/honeytry/'])
Button Text
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
**************************************************************
EventServiceProvider
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Events\WelcomeEvent;
use App\Listeners\WelcomeListener;
use App\Events\ThankYouEvent;
use App\Listeners\ThankYouListener;
class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        WelcomeEvent::class => [
            WelcomeListener::class,
        ],
        ThankYouEvent::class => [
            ThankYouListener::class,
        ],
    ];
    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
*************************************************************
Route::get('/flight-send-mail',[FlightController::class,'sendMail'])->name('flight.send-mail');
Route::get('/flight-thank-yout-send-mail',[FlightController::class,'thankYouSendMail'])->name('flight.thank-you-send-mail');
**************************************************************
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
x
Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment