How To Send Emails In Laravel 8 | How To Email | Laravel 8 Authentication & Mailing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up laravel developers this is dary here and i hope that you're having a great day in this video i want to talk about mail usage in laravel now before i continue on with the video i want to quickly let you know that you can support the channel through patreon you get some pretty cool benefits such as a private discord group where everyone is helping each other out and you can decide a video series that i'm going to create rupauls so if you're interested to join the link is in the description down below if you have watched my previous larval videos you know that we have touched on a lot of topics of laravel but one of the most important ones in my opinion is mailing in laravel in web-based applications sending an email or notification is a very very common requirement and to be honest this can be done in multiple ways there are loads of malfunctionality layers that you can build on top of the default layer functionality which is the swift mailer in order to see your mail configuration you need to go to the convict folder and in here you can see a file called mail.php so let's open it if you want to send actual emails to clients or customers you need something which is called a mail driver and if we scroll down you can find all the drivers that come with largo for mailing we have smtp ses mail gun postmark send mail lock and array now one thing that all of these drivers have in common is that a lot of variables are being called from the emv file since laravel 5.3 a new mailable syntax has been developed before this ever came out there was a classic mailing system which was very easy to use but for the purpose of this video and to not make it longer than it should be i'm going to focus on the mailable now the goal is to create a specific php class that will represent a mill and this needs to be generated in the cli so let's go to the cli now before i show you how we could actually generate a mail let's write down php artisan help make colon mail you can see the usage of the mail function so whenever you try to perform a make column mail command you need to pass in one argument which is the name so the name of the class that you want to create you can see that there are a couple options you probably won't need most of them except for the markdown but we will cover it in a bit what we want to do is to create a new mailable class so let's say php artisan make call in the mail called welcome mail if we hit enter you can see that the mail has been created successfully and if we go back to visual studio code you can see that a new file has been created or a new folder called mail so let's open it and it's right inside of the app folder so let's open welcomemail.php if we look at the class right you can probably see a class that looks familiar to you that imports two traits the first one is queueable and that's used in order to queue your email and the second one is serializes model and this is used so that every eloquent model will be serialized correctly you could also find a constructor which is empty right now so let's skip forward next to the constructor there is one method defined which is the build method now inside this build method you are going to define the view that you want to use as you can see right here you're basically returning a view so this view it searches for a folder called view with a file name of name now besides the view you could also pass in the subject or anything else that you want to show the user if you have properties that you want to use inside your mailable class you need to pass them inside your controller so they will be available inside the template that you're going to create we won't be doing that in this video but i will do it later on there's one thing that is not correct in this file it returns a field of view dot name but we haven't created that file now in order to create the view ourself we could also do that in the cli so what we need to do right now is to remove welcome mail so let's delete it move to trash go back to item hit the arrow up and we're indeed going to create a welcome mill but i want to add a dash m flag to it as you can see in the options menu dash m stands for markdown and it will create a new markdown template for the mailable what i usually prefer to do is to create a folder called emails so let's say space emails dot and the dot stands for the folder like i said before inside the emails folder i want to create the file so let's call it welcome let's hit enter and you can see that the mail has been created successfully as well so let's go to visual studio code we indeed have our welcome mail open it and as you can see in the return markdown you can see that emails.welcome has been called we still haven't created a file but like i said if we go to resources views you can see a new folder called emails which will refer to emails in the markdown and if we open it there is a file called welcome.blade.php which is basically the welcome after the dot let's open it because this is not the typical blade.php file that we're used to if we want to open it in the browser we need to create a new route because we obviously want to change some stuff up so let's open web.php at the bottom let's create a new row so let me add a comment route for mailing so what i want to do is to basically do the same thing as always so route column column get inside the get method we have a first param which is the endpoint so forward slash email i want to pass in a second param so let's say comma which is a function curly braces hit enter what we need to do inside our route is basically to return a new instance of the welcome mail class that we just created we're getting an error message because our web.php file does not know where welcome mail comes from so what we could do is to remove it say welcome mail and you can see that the use statement will be automatically pulled in if we click on the option so let's do that let's add parentheses ourselves and a semicolon and if we go to the top you can see a use statement of app backslash mail backslash welcome mail if we save it it should actually work in google chrome so let's go there let's change the end points to forward slash email and as you can see the predefined interface is generated for us and we can see it in the browser since we created it with the dash m flag now the most important question is probably where is this all coming from because if we go back to the welcome.blade.php file we're seeing stuff on the screen that we haven't seen before as you can see we're using a hashtag introduction if we go to chrome it's this piece of text right here so let's change it up let's change it to welcome to my authentication and mailing course save it go back to the browser refresh it and we just changed that piece of text what about the paragraph so what about the body of your message so let's change that up to you i would like to thank you for watching this video save it google chrome refresh it and it has been printed out as well there's also a button right here which does not look like a clickable button but it's using a component to mail column column button which we will cover in a second but it does accept an url so what we could do is to basically say https column column backslash backslash www.codewithdari.com change the text to visit site save it go back to chrome refresh it and we have a new button right here now what about larval right here you usually don't want to send the email with laravel in it if we go back to visual studio code you can see that it's calling a convict method and is looking for a app.name now this is searching for the name inside the dot env file so let's open it up at the top of our screen you can see app underscore name which is equal to larval so let's change it to odd app save it go back to the browser and larvo has been replaced with audit app next to the template that we just created we can also extend other templates use sections parse variables contain conditional statements so pretty much anything that you can do in a normal blades view what i want to do right now is to send an actual email we can either set up a mailing provider or a local development in this video i want to focus on a mailing provider called mailtrap let's open a new tab in the browser and let's go to mailtrap dot io let's hit enter whenever you want to test your emails before you send it to an actual client you can use mailtrap what it does is basically catching your email in a virtual box so you can text and optimize your email so what i want you to do is to pause the video sign up it's very easy you can use your google account github account or office account or you can just sign in with an email pause the video set up an account and i will see you back in a second whenever you're done with the setup part you will probably land on the screen that i have on right now inside my smtp settings we have a section called integrations right here as you can see it's on c url so if we open the drop down you can see a lot of programming languages obviously the one that we need is laravel 7 so let's click on it and as you can see the box right here has been changed and our smtp data have been printed out and if you're familiar to the emv file you can see that this looks pretty much like that emv variables so what we could do is to copy it and before i continue on don't copy or use mine because it obviously will be different for you so let's go back to visual studio code and let's go right below our mailing let's actually replace it let's hit backspace paste it right here save it whenever you want this to work you need to basically break off your php artisan serve and run it again so let's do that let's go to i term let's go to the php artisan serve tab if you want to break off your serving you need to press ctrl c and as you can see we just broke it off hit the arrow up run php artist and serve again our development server has been started the application that we got knows all right we have a mailing provider called mailtrap as they can see right here so the variable mill underscore mailer and it knows how to interact with it with all the different variables before we make it work we need to define where we're going to mail it to let's go to the web.php file again right inside of our route we need to add one more line of code so let's go right above our return instance let's say mail let's hit enter because we need to pull in the illuminate support vacate mail call a collin then we need to define the receiver so let's say 2 parentheses semicolon inside the 2 method we need to basically pass in a string and this string will be the email of the receiver so let's say single quotes info at darinazar.com this can be any email that you want to use the best way to do this is to have the actual user that is logged in but let's not do that for now outside of the to method we need to add an access operator because we need to define one more method which is where we want to send it to so let's say send and inside the send method we need to pass in the new welcome mail save it once again be aware that you pull in the illuminate mail otherwise you will receive an error now let's save this file let's go back to google chrome go to our localhost refresh the page and as you can see it takes a bit longer so something is going on right there let's go back to mailtrap and as you can see we have received our first email this was a very simple example during the next few videos i want to show you how you could reset passwords add attachments and it will also show you how you could verify your email before you register now this was it for this video in the next video i want to set up a local development for mailing if you do enjoy my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit the subscribe button
Info
Channel: Code With Dary
Views: 10,112
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, complete laravel 8 course, learn laravel 8, how to send emails laravel, email laravel tutorial, how to use email laravel, mailtrap laravel
Id: 10755StTiCo
Channel Id: undefined
Length: 12min 49sec (769 seconds)
Published: Mon May 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.