08 - Mail and Notifications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] almost all applications require some sort of communication with their users while they are offline whether it is to verify their data to warn them about certain events or to ask them to perform certain critical actions and larval ships with two components one for sending emails and the other for sending generic notifications they allow us to perform communication with our users while they're offline easily and it provides support for sending email via smtp via amazon ses via mail gun and several other third-party providers and as for notifications it allows us to send notifications across a variety of delivery channels like sms like mail like slack telegram and it also supports several other communication or notification channels that are maintained by the community before we continue with this tutorial here is an answer to a question that you are going to have if the notification component can send email as well why have two separate components one for sending email and one for sending notifications and the answer is simple you can send a notification to a variety of notification channels at once however for sending an email it's just an email so if your application or of the action that you want to notify the user about requires notifying them on several channels go for notifications if you just want to simply send an email to your user you will need to use the mail component creating and configuring emails on your laravel application are quite easy let me show you we will head to the terminal and create our first mail using sale artson make mail and we are going to call our mail order shipped now if we head to phpstorm we can see the newly created email inside app mail and the email is called order shipped inside this class there are two methods the constructor and a build method inside the belt method we can build our email message for example we can set the from address using the from method here so muhammad at laravel.com and we can also set the to address customer at customer at mail.com you can also set the subject of the email your order was shipped and we can attach any file from the storage using the attach from storage method for example if we have a file located at images image.png we can attach it with the main and finally you can provide the view that will be used as the body of the email message for our example here we are going to use a view located inside emails order shift let's fix the table first and then head over to the resources directory under views and create a new directory call it emails and under this directory you are going to create a new file called order shipped dot played dot php inside this new file we can just say your order was shipped for the sake of this demo now to be able to send this email we need to configure the mail component first and to do that we will go to the mail.php configuration file inside this file there are several mailers that you can use for example you can use smtp ses mailgun postmark and many others in this demo we are going to use smtp and to configure that we will go to the environment file and configure our mail environment variables for our example i'm going to use a service called mail trap that catches the emails that we are going to send from this demo application and all i need to do is to provide the username and the password and now our mail is configured let's go back to them mail class that we just created and let's remove this attach from storage because that file doesn't exist now if we go to the web.php routes file inside the root view let's send our email and to send the email we need to use the mail facade so we're going to call mail send and then provide the instance of the mail that we are going to send new order shipped now let's go to the browser and i am visiting my account at mailtrap.io any messages that are sent from this application will land here so if we go to localhost to trigger the root route to send the email let's see what happens we get an error view email.ordership not found and that's because inside the class we provide the directory name wrong it should be emails instead of email now let's refresh and here we go it continued successfully and if we go to mailtrap we can see the email was sent it has a subject that we provided the from address the two address and the body as well which is the blade view now all that's remaining here is that you build beautifully looking emails using html and online css and laravel understands how difficult this can be so it provides templates that you can use with your application with a fresh larval application to provide your users with beautifully looking email addresses while your email views are just going to be sample markdown laravel is going to take those markdown files that you create and it will generate beautifully looking emails as well as plain text emails for email clients that prefer the plain text version now let's switch to using those markdown mails inside the email class we are going to switch the view method here and use the markdown method instead and then inside the view inside the order shipped view we are going to replace the content with this markdown so this is going to be the body of our email it will have a title your order was shipped some details about the order and a patent component that ships with larval that has a button that says more details and then thanks and then the name of the company which is laravel in our case now let's go to localhost and send this email instead of the old email and if we go to mailtrap and check the new email we can see that it has a beautiful template instead of just this plain view and if we check again we can see that it also generated a text a plain text version of the email for clients that prefer this and you can also easily customize the templates that laravel ships the markdown templates by calling the arts and vendor publish command so if we go to the terminal and call sale artisan vendor publish and provide the tag laravel mail hit enter this will publish the template files under resources views vendor and mail here are the html templates and also the plain text templates and you can customize them however you want now that we have seen how to create and send mail from our laravel applications it's time we look into notifications and creating notifications is similar to creating mail using the arts and command line tool so let's head to the terminal this time we are going to call sail arts and make notification and we will also call this notification order shipped notifications are created inside app notifications and here is the notification that we just created by default the notification will be configured to send to the email channel only but you can add different channels if you wish let's send an sms besides the email and to do that laravel shops with an xml channel that we can use to send sms notifications using the nexmo sms provider but to do that we need first to add a couple of dependencies to our application so we will head to the terminal and call sale composer rig wire laravel nexmo notification channel and nexmo slash laravel and once the command finishes we should go to our environment file and provide the next smokey and next more secret environment variables and then we should also go to the services configuration file and add an entry for nexmu so here we'll add nextmo provide an array and then we provide the sms from attribute and inside it we provide the number that we are going to use to send sms notifications from you should generate a phone number for your application in the vonage or previously nexmo control panel and now that nexmo is ready to use let's configure our sms message inside the notification class so we'll go to the order ship notification and then add a method similar to the two main method that shows larval how we wanted to build the main message we are going to add a two-nexmo method as well this method is going to accept the notifiable and it should return a new next more message a new nexmo message the next more message class allows us to tell the sms message for example we can provide the content of the sms message here we are going to say order was shipped and now the notification class is ready it's going to send one notification to the mail channel another to the nexmo channel and we told it how to send the mail notification and how to upheld the sms notification the only remaining part is to configure how the notification component will extract the phone number to send the notification to and to do that we will go to the user model and then provide a new method called route notification for next inside it we are going to return the phone number that is stored in the database record of the model and sending notifications is similar to sending mail we are going to use the notification facade and call the send method the first argument of the send method here is an array of notifiables for example if i want to send this notification to all the users in our system we are going to call user all so the first argument is a collection or an array of the users that we want to send the notification to and the second argument is the notification instance that we want to send which is the order ship notification and now laravel is going to send two notifications to each user in your system one email notification and one sms notification but this can take a lot of time it's better that we utilize the queue system that ships with laravel to send those notifications in the background instead of sending them in the foreground and have the user wait for the notifications to be sent and to do that the laravel is really easy we only need to implement the should queue interface and that's it laravel takes care of dispatching these messages to the queue and handled sending the notifications in the background you can implement the shoot queue interface on mailables as well so laravel will send the mail in the background using the queue component instead of sending it in the foreground there is a lot to learn a lot more to learn about the notification system and the mail component inside laravel so head over to the official larval documentations to learn everything about it i'm gonna see you in the next [Music]
Info
Channel: Laravel
Views: 3,974
Rating: undefined out of 5
Keywords:
Id: F1NPG3nKxrQ
Channel Id: undefined
Length: 12min 31sec (751 seconds)
Published: Mon Sep 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.