Last active
February 24, 2020 15:01
-
-
Save Sadicko/b149258fb9cc4fff41803d128f77184d to your computer and use it in GitHub Desktop.
Sending a welcome message after user verify his email using markdown in laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--App/Providers/EventServiceProvider.php --> | |
<?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; | |
class EventServiceProvider extends ServiceProvider | |
{ | |
/** | |
* The event listener mappings for the application. | |
* | |
* @var array | |
*/ | |
protected $listen = [ | |
Registered::class => [ | |
SendEmailVerificationNotification::class, | |
], | |
'Illuminate\Auth\Events\Verified' => [ | |
'App\Listeners\WelcomeMail', | |
], | |
]; | |
/** | |
* Register any events for your application. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
parent::boot(); | |
// | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--App/Mail/SendWelcomeMessage.php --> | |
<?php | |
namespace App\Mail; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Mail\Mailable; | |
use Illuminate\Queue\SerializesModels; | |
class SendWelcomeMessage extends Mailable | |
{ | |
use Queueable, SerializesModels; | |
public $user; | |
/** | |
* Create a new message instance. | |
* | |
* @return void | |
*/ | |
public function __construct($user) | |
{ | |
$this->user = $user; | |
} | |
/** | |
* Build the message. | |
* | |
* @return $this | |
*/ | |
public function build() | |
{ | |
return $this->markdown('emails.welcome') | |
->subject('Congratulations!'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Laravel email verication fires an event. Therefore we will make use of the Verified event listener. | |
When a user is registered a "Illuminate/Auth/Events/Verified event is broadcast". | |
1. Create an event listener "php artisan make:listener WelcomeMail". This will create an event listener locate at "App/Listener/WelcomeMail" | |
2. Register the listener with the verified event in the "EventServiceProvider" | |
protected $listen = [ | |
Registered::class => [ | |
SendEmailVerificationNotification::class, | |
], | |
'Illuminate\Auth\Events\Verified' => [ | |
'App\Listeners\WelcomeMail', | |
], | |
]; | |
3. Since we are using markdown, let create a mailable class and make use of the mailable class and markdown. | |
run "php artisan make:mail SendWelcomeMessage --markdown=emails.welcome" | |
This will create a mailable class located at "App/Mail/SendWelcomeMessage.php" a | |
and the markdown blade found at "resources/views/emails/welcome.blade.php" | |
4. In the build() method of the mailable class "SendWelcomeMessage" add | |
return $this->markdown('emails.welcome') | |
->subject('Congratulations!'); | |
5. Now let modify the handle() method of our listener "WelcomeMail" to the following | |
public function handle(Verified $event) | |
{ | |
\Mail::to($event->user->email)->send(new SendWelcomeMessage($event->user)); | |
} | |
Add the following at the top | |
use Illuminate\Auth\Events\Verified; | |
use App\Mail\SendWelcomeMessage; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--resources/views/emails/welcome.blade.php --> | |
@component('mail::message') | |
<strong>Dear {{ $user->fname}} </strong>, <br> | |
<p>Congratulations and welcome to Our page. | |
<p>Click <a href="{{ url(env('APP_URL').'/home') }}">here</a> to view your profile | |
</p> | |
<br> | |
<hr> | |
<strong> | |
Regards,<br> | |
Sadick Odai<br> | |
Developer<br> | |
increghana.com<br> | |
</strong> | |
@endcomponent | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--App/Listeners/WelcomeMail.php --> | |
<?php | |
namespace App\Listeners; | |
use App\Events\SendWelcomeMail; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Auth\Events\Verified; | |
use App\Mail\SendWelcomeMessage; | |
class WelcomeMail | |
{ | |
/** | |
* Create the event listener. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
/** | |
* Handle the event. | |
* | |
* @param SendWelcomeMail $event | |
* @return void | |
*/ | |
public function handle(Verified $event) | |
{ | |
\Mail::to($event->user->email)->send(new SendWelcomeMessage($event->user)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment