Generate & Attach PDF file with Mail in Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everybody so welcome back to another tutorial and in this video i'm going to explain you about how we can attach the pdf file with the mail right so all the steps that i'm going to do right now all from the scratch all from the step to step procedure so just want to go through what i did before so this basically form help us to generate the pdf file so i just want to show you real quick that i once i click on here the customer saved successfully inside the database already i believe i want data and now if i go to my storage and that's where you see that the pdf file is generated now and this could be very much helpful for you once you're generating something that are very important files so make sure you have those files um attached with your mail so these are the steps these are the things that are very important so i'm gonna go all from the scratch so let's jump in and get start inside the code so here i'm going to show you inside app http controllers that we see that we have a customer controller already generated and now right over here this is the customer save that help us to save the data and down here the load view method which actually generate the load view of the pdf and we can save this inside this directory and this is the response that we see right now what the next thing that i'm going to do is a very interesting step so let's just start falling with that i'm going to cut this out right over here and down here i'm going to apply here a condition that if that customer is saved successfully then i'm able to show the view of the customer right basically we are going to do here is we are going to call the customer model right here and this map and this customer model will have the mail functionality implemented inside it so let's write over here i have a customer model let's gonna define right over here and give it the name of send customer email let's give it that name and right inside here i want my customer object that every field that we type it should be right over here and as well as i need the pdf right so these are the two major objects that i need inside my customer email okay so this step is not done yet we have to define the model on top that we just defined which is already there use app slash models as customer i highly suggest that if you haven't watched my video i want to give that link in the description so you can follow along with the step if you want so if you're quite good with it you can directly move on here right so let's go inside the model which should be inside the models folder customer.php and right down here i'm going to define here the mail functionality for here so for that we need some few things we need the use mail that's you have to define right here and as well as we need here the storage um basically that will help us to um when one once we are we are trying to attach that file we must know the specific part of that we need to define that right here so use eliminate spout and right over here next we need the facades and we need to reach to the storage all right so that's all we need for now next we have to go down here inside the customer model which is extend the model and right here i'm going to define here a method with the name of public function and let's give it the name of send customer email you can type whatever you want right so it should be same that whatever you have defined here and let's define here few fields here so i'm not gonna go with the directly to the uh to the functionality that we have implemented for the attach file first we're to show the very basic email that we can get with the very basic feel like name email and the and the first name last name whatever the fields that we have for now so let's create here a view data and let's pause here the key which is the app name and we have to get the value of it so it should be customer um it should be customer that email and the question arrives here where this customer is coming coming from so basically as we have defined right here we have to define the customer and as well as we need the pdf that we are just not working yet so i just want to make this um just just uh make this uh empty for now we don't need uh the pdf for for this functionality so just want to grab it from the top and let's paste it um yeah we just need three times right the first name the last name and here the third field should be email and that's where i'm gonna define here first name and there we go it's a last name and just want to skip that too and the third one should be email and that's all we need so we have very basic three uh properties defined over here and let's define the mail that's what we have implement that's what we need to implement so i wanted this mail to be sent and it's good and it's showing me a lot of you can see we can attach we can give the priority we can attach the cc with bcc2 all those functionality depending on your requirement so for now we just want to cover a lot much things but uh yeah we are going to cover subject we are going to attach and rest of the things are very simple i hope you guys would understand if you understand this topic all right so let's define here that is the mail call and send and that's where i'm going to create here a template a very basic template that's where you could able to see our the mail the the view for the mail you can create something very awesome mail a page where you there which is very appealing but for in this tutorial i'm going to create a very basic template so just an over through idea of that how you can implement that so let's give it the path here so email underscore templates underscore um yeah that should be the name of the folder and right over here next i need here the name of the file which should be email underscore customer oops actually should be customer underscore detail that's all we need so inside over here so that's you see i just created here view data so i just want to copy that and let's pass right here and next we have the function so that function should have the message i just want to make dollar m for my ease um i'm actually not good in the spelling so that really makes things much easier for me so let's give it the from so the message from that i'm gonna pass i'm gonna show it from the av nation double five three at the right gmail.com so that's the email where it's coming from so you need to write like that from it all depend on your email where you want to send basically this from is the is the email of a person from where you get that email so this should be from and next i'm going to pass it with the help of environmental variable which is env and you will be surprised where this coming from app underscore name so if you go to your dot emv file there that's the environment of files so here it's reading app underscore named laravel i'm going to change that ab nation and this basically whatever pertains to here it's going to show right over here so that should be the name of the app underscore name next we need that email i i'm just applying here condition so i just want to make things very simpler i i want actually that next this email should be sent to that person that who is gonna fill this form means whatever i type inside the form that email should be sent to the person okay so that should be the email and that's all i know i want for now so let's pass here the subject as well so i'm gonna pass here the first name depending on whatever you want to pass you can pass here a string as well so i'm gonna pass your first name and that's all i need right so let's go down underneath that but i believe make things very simple uh i didn't i don't need any attachments for now and let's have a look and see but but this is not all enough because we are having an smtp configuration with stanford smart meals transfer protocol we need some kind of uh transfer protocol to send the mail right so if you go to your dot env file you already found there are a few parameter that that are that were required to send the mail so right over here you see here the mail underscore mailer which is smtp now i'm gonna send that email with the help of gmail so there are a few settings that were required so make sure your mail underscore it should be mail underscore driver and just replace that with the mail underscore driver to be smtp and i need the mail underscore host to be smtp.gmail.com and that should be the mail underscore quote which should be 587 and mail underscore user name it should be that email the id where you want to um where you're gonna send that right so ambination double five three at the right gmail.com and that should be the password i'm i'm not gonna type right now and the encryption should be tls and the mail from the address should be av nation double five three it's it's fine that if you can type that but it's not required right so these are the main parameters that you need make sure you pass and i haven't passed any my password because it's because of some security and i'm gonna skip that and i'm gonna write my own password here so just want to skip it and while i type all right guys so i need to fix few things here before we should move ahead first this should be the static method we defined your scope operator so that should be public static function and whenever we define the model that way it always should be a static function and next we have defined we already have uh created that we haven't created any template yet so let's define that i need to fix few things here so it should be templates with the right spelling so templates i hope you guys have rectified on your end and we have to use because we define here the customer inside the two so let's use that so i have to define a customer and that's it so let's go inside my views and we have to create here the the template view for that so i'm gonna first create here a new folder and let's give it the name the same name that we pass in the send email underscore templates so that should be the name of the folder and let's create here another file and that should be email underscore customer underscore detail.blade.php all right that's fine okay so we defined that and it should be very much identical to the download pdf that we just created so i'm going to grab this one and paste everything inside the email underscore customer because all we need is to show that very similar first name the last name and the email and these variables are passed right from here so if i want to show you that customer model and this all data with the help of the use we are able to access all those properties and that's it okay so let's have a look and see that whether everything works fine for us so if i refresh that i write my email and click on the save customers and it's still taking some time it means that there might be some chances of errors i need to yeah its customer is saved successfully if i go to my console i i i think so it might be it works very fine but yeah there should know any response yet so if i go to my my email address let me refresh this and yeah there you go so we are able to get and receive our email the ui looks very much simpler simple but yes it's fine because we it's just for the testing purpose all right so we are receiving the email so another way is that when you have to send emails to the multiple uh to the multiple users then all you need to do is to pass the email inside your two parameter for instance if i want that same email to be passed to my another account which i just want to uh create i just want to type my email here double five through the gmail.com now that same email will be received to the to the to the other end what actually it means that if i go to back to the screen let me refresh this and i'm going to click on the save customers so this is going to take some time you can create some loader if you want yeah customer saved successfully now if i go back to my email address and that's where you see now i have received the two emails and on my second side right if i go to my another gmail account let me refresh this and i hopefully i might get an email which actually i haven't received yet so i need to look and check my code it should be correct yeah it should i forget to bring those brackets in so let me add that first and i hope that this is gonna work for us so let me go back to the browser and write my email and save to the customers and this should be passed to the both ends so the email is successfully sent yeah i received that third email and let's go back to the another one and there we go so you see here how good is that how we can send that same email to the multiple user it all up to the up to you the way you add your emails on your own right so that's all good for now right the main objective of this video is to attach the data attach the file so i'm going to go and jump to it so let's uh do another part which is the attachment part right so i'm going to press enter here and let me define here one more thing which is actually the attach data so let let's go to the attach data and i'm going to pass here the pdf output so basically this should be uh now the question is that how this pdf is accessed so i need to pass here another variable dollar pdf and the same that we are actually getting from the controller so let me pass that dollar pdf right into it all right and i i need to cut this uh i got this part and show it to to my um to the model but so to access it easily so let's go to the customer.php and right next to it i'm going to define that actually it should be inside those parentheses that's fine and next i'm going to create here storage with the help of storage but we are able to access the parts to storage um put and let's define that path into it and i also need that pdf output which is basically the formula of it so pdf dash output all right so that's it i need to end that one so we defined the part we defined where this path is coming from and load the pdf and right into the attached data this pdf is coming from this and we have to show this pdf output so let's make that that to be a method and we have to access that path as well the part where this file is stowed and let me create that and let's end this bracket and end this with the semicolon now we have to define here the meme so this is not actually the memes this would be mine and whatever you pronounce it well so we have the file with the formula application slash pdf and next we have to define here as and which should have the the formatting the name of the file that we actually once it's attached with it the mail we have to show so i want that to be sure with the name so i'm going to define your first name and let's uh let's the ident uh indent the uh add some kind of formatting the name file structure so let me and it should have the pdf format so that's all we know all we need for now so actually showing kind of uh error so we add here the dot and there should be a dot pdf actually should not don't have the semicolon right so we have passed all it and let's have a look and see whether we are able to attach the file on so i'm going to refresh this for here and let me add my email again and let's add the save the customer um now it's showing us the error let me check this out yeah it's showing us because we haven't passed that to the mall use so let's go to the customer and i i have to define two things we have to define here the pdf as well as we have to define here the path and that's all right so now i am very hopeful this will attach our very first pdf file let me refresh this write our email save the customer still have some error on the line number 17 it says undefined variable path need to define that path variable right over here so let me refresh this for the last moment hopefully so refresh this and let's define here the email and yeah it's gonna work you can do here you can pass you can create here a loader the customer the email saved successfully let's go to that email and yeah there we go so you see here with the name we are able to see our file and same case for the other if i go to my new email and there we go so you see here we can also see that pdf file so that's it guys i hope you like and learn something out of it this is something that is very hard to find and i hope you guys uh you you guys able to learn a lot of stuff from here you learn about how we can create and generate the new pdf you're able to fetch that email and attach to the to your mail server and as simple as that so you guys learn a lot of stuff don't forget to like and subscribe my youtube videos um and just share and subscribe my channel and thank you for watching and i want to see you with the next good video
Info
Channel: AB Nation Programmers
Views: 574
Rating: undefined out of 5
Keywords: laravel crash course, generate PDF from form using ajax in laravel, send mail in laravel, send mail with attached pdf, Generate & Attach a PDF file with Mail in Laravel, Attach pdf into a mail, Sending email with a PDF attachment, Laravel Generate PDF and Attach to Email, laravel tutorial
Id: HTP12jWV-Yw
Channel Id: undefined
Length: 19min 10sec (1150 seconds)
Published: Sun Sep 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.