Last active
March 26, 2020 11:04
-
-
Save Sadicko/3286b163180f97b52401b7751b38d8cd to your computer and use it in GitHub Desktop.
Sending Emails using Laravel event and listeners
This file contains 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
Step 1. Creating event and listener | |
There are two ways to creating event and listeners | |
First way: | |
1. Open "App\Providers\EventServiceProvider.php" | |
2. Inside the "protected $listen" array, add the following. Change the event name and listener to your deside name. | |
'App\Events\EmployerWelcomeMail' => [ | |
'App\Listeners\SendEmployerWelcomeMail', | |
], | |
3. Run "php artisan event:generate"; | |
NB: This will generate the event and it listener under the directoery "App\Events\EmployerWelcomeMail.php" and App\Listeners\SendEmployerWelcomeMail.php" respectively. | |
Second way: | |
1. Run php artisan make:event EmployerWelcomeMail | |
2. Run php artisan make:listener SendEmployerWelcomeMail --event="EmployerWelcomeMail" | |
3. Repeat step 1 and 2 in the first way above. | |
Step 2: Making changes in our event: | |
1. Open "App\Events\EmployerWelcomeMail.php" and do the following: | |
a. add "public $employer". This makes the variable public and accessible in the mail markdown. | |
b. Add the variable and initialize it in the constructor as below | |
public function __construct($employer) | |
{ | |
$this->employer = $employer; | |
} | |
Step 3: Creating our mailables | |
1. Create a mailable class with it markdown by running " php artisan make:mail SendingEmployerWelcomeMail --markdown=emails.sendingEmployerWelcomeMail " | |
NB: This will create a mailable class located in "App\Mail\SendingEmployerWelcomeMail.php" and the view at "resources\views\emails\sendingEmployerWelcomeMail.blade.php" | |
2. Open "App\Mail\SendingEmployerWelcomeMail.php" and do the following: | |
a. add "public $employer". This makes the variable public and accessible in the mail markdown. | |
b. Add the variable and initialize it in the constructor as below | |
public function __construct($employer) | |
{ | |
$this->employer = $employer; | |
} | |
c. You can add subject of your email inside the build function as below | |
public function build() | |
{ | |
return $this->subject('Congratulations!') | |
->markdown('emails.sendingEmployerWelcomeMail'); | |
} | |
Step 4: Making changes to our listener class | |
1. Call your Mailable class and send in the handle or "App\Listeners\SendEmployerWelcomeMail.php" | |
\Mail::to($event->user->email)->send(new SendingEmployerWelcomeMail($event->user)); | |
NB: Import the mailable class "use App\Mail\SendingEmployerWelcomeMail;" | |
Step 5: Calling the event | |
The event can now be called at where you want to send an email after an action as below. | |
"event(new EmployerWelcomeMail( $employer));" | |
NB: Import the event on top as "use App\Events\EmployerWelcomeMail;" | |
Final tips: | |
You can change your mailable markdown to your liking. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment