Sending Emails with Laravel 10 via Gmail Step by Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to programming Fields I'm om Rana in the last video of laral interview preparation series I have covered the databas seting using Factory in which we have covered a stateful factory and callbacks in the today's video we are going to talk about email functionality that we will Implement using Gmail in lar 10 sending emails is very important for any web applications whether it is for user notification password resets or sending News letters and lar will makes this process super simple all right let's get started with the Practical [Music] part first thing first make sure you have L installed on your system so I have navigated to the terminal and firstly let's create our lar application so I will type composer create project prefer dist larl larl email app all right so this is our application name as email app now let's hit enter and this will take a couple of seconds to create this project so application is created now let's open it in vs code editor so I will CD into email app and let's open it yes so the project is open in the vs code editor so now in the next step we will have to configure this so inside this file we have this mail mailer configuration all right so in order to configure this we will have to generate the credentials inside Gmail so in this video we are going to send email using Gmail so we will have to create credentials through Gmail first so let's check out to the browser in your Gmail account you will have to click on manage your Google account first so inside this manage your Google account we'll have the security tab so under this security tab we will search for app password so inside this search Google account we can search app password okay so after the successful authentication here you will have to create a new application here you will have to provide the app name so let's give app name as test email okay and here it generated the app password so simply you'll have to copy this password okay now let's come back to the vs code editor and here inside this username you will have to provide your email address so I will put my email address at gmail.com all right and under this mail password you will have to provide that password that you have generated now so let's add this and under this mail encryption we can add TLS so the mail encryption has two options TLS and SSL and here mail mailer will be SMTP and inside this mail host you will have to pass smtp.gmail.com so this will be basically Gmail server and now we will have to add this port so by default Gmail has Port as 587 so this port will vary based on encryption type all right so if we will be using TLS then we will have to provide this 587 and it will be different for SSL and then we will have to provide this mail from address so we can add the same Gmail address here yes this is done okay so the EnV file is configured now so now in the next step we will have to create one mail class so Lara will provide an option to create mail class so by using that mail class we can Implement that to send email so let's switch back to terminal and now we will create mail class PHP artion make mail all right and here we will have to provide mail class name let's suppose I will give welcome email yes so this created mailable class all right so this aable class will be applicable inside this app and mail folder so inside this we have this welcome email. PHP class all right inside this we have this Constructor invelop content and this attachments option so let's discuss about this construct so if we want to receive any parameters from the controller or any functions then we can receive that directly inside this Constructor okay so this will be invoked automatically when we will call this email class now we have this next function as inv so inside this envelope we can see we have this subject so by default this subject is added based on the given class name we can add from email address and uh reply to address as well inside this inalum and this is content so inside this content we have this view option so from this view option we can render the blade template all right and here we can pass the attachments asual so this can be image or PDF for any file all right so for more information we can check out to the browser and let's open the lar official documentation so we will type lar mail and let's open this mail class okay now let's scroll down and uh let's click on this so we have completed this step we have generated this mevel class okay now we can see we have this invelop function and inside this we can Define from address reply to address subject these things and also we can f these credentials using EnV file we we can configure the email template and we can pass over the data so in the content we have two options like by default we can render the HTML or we can render the plain text file we have these two options and here inside this Constructor we can receive the parameters from other classes where this male class will be invoked okay and this is all about this attachment we can pass this attachment so let's come back to the vs code editor and here in order to call this mail class firstly we will create one controller so let's switch back to terminal and here we will create PHP artion make controller email controller so this generated One controller as email controller now inside this email controller we will create one function so let's define one function as public function send email uh better we can Define send welcome email okay so here we will Define some data firstly we will Define recipient email address to whom we will send and email so I will Define two email and inside this I will Define one static email address so I will add this okay and also we will Define one message Dynamic message as welcome to programming Fields so as of now I have defined this two email address so this will be basically our recipient email address and I have defined one message now in the next step we will have to call the mail facade so by using this mail facade we will call the mailable CL class so I will call this mail to function and here we will have to import that mail class we will write use illuminate support sard and then mail okay and inside this mail 2 class we will have to pass this email address this will be our two email okay and then implies and then we will call this send function inside this send function we will have to pass that mail class that we want to call so we have this welcome email class so let's copy so we will write new welcome email and we will pass this parameter as message now we will have to import this welcome email so we will write use app mail and this welcome email okay now let's come back to this mail class and here we will have to receive that message so we will create one variable as public dollar message and we have to capture that parameter in this mail class Constructor so we will Define one variable as dollar message let's assign this message to this dollar this message okay this is done and we can change this subject as well so we can pass that subject from here itself we will Define subject equals welcome email in L using Gmail you can pass one more parameter as subject and let's capture that inside this Constructor we will write subject let's define one more public public variable as dollar subject let's assign this so we will write subject and dollar subject here this is done now instead of this subject welcome email we will map this with dollar this subject we will write dollar this subject this is done now in the next step we will have to design one basic template so what we will do we will create one Le file inside this resources views folder so inside this views folder I will create one mail template folder and let's create one blade file as welcome mail. blade. PHP okay and inside this welcome email template I will import HTML skeleton so this is done and let's remove these all things and even this CSS as well okay so we required a basic skeleton of HTML only inside this welcome email class whatever we have received inside this Constructor and we assigned this in the public variable these variables will be directly accessible inside this lead now let's come to this welcome email and let's call this template inside this view let's add this template name so we will have to pass the folder name as template do welcome mail so we have rendered this view inside this content now we have to receive these two variables inside this welcome email so in order to receive these two variables we don't need to pass that inside this View because we have defined these two variables in the public access specifier so these two variables will be directly accessible inside this ma class we have called this male template inside this mail class so by default these two variables will be directly accessible inside this maale template as well so let's come back to this welcome bade and here we will Define line okay and uh we will have to pass this subject and message so inside this H4 I will call this dollar subject and then inside this P tag I will call that message so I will write dollar message this is done and let's pass this subject here inside this title as well we have called this mail class inside this controller and we have received these two parameters inside this mail class Constructor and also we have rendered that mail template View and we have captured these two variables inside this mail template now in the next step we will have to create one route in order to execute this controller function so let's switch back to web.php file so inside this web.php we will create one endpoint so we will Define route colon colon get and let's define endpoint as send mail inside this will have to call that controller we will call this email controller colon colon class and after that in the second argument we will have to pass that function name so we have we have the function name as send welcome email yes we have the send welcome email we will have to import this controller so we will write app HTTP controllers and then email controller this is done now let's open the terminal and let's run this application so we'll write PHP artion serve yes application is started let's switch back to browser and let's try to access this application so application is accessible now we have to capture the response from this mail so will Define this response okay as of now let's add dump and die we will add this dollar response now let's try to access this route we have this send mail let's copy and let's navigate to this route so now let's redirect to that route send mail so this thrown error HTML spal car message okay so actually dollar message is related to this illuminate mail message so this is basically predefined what we can do we'll change this variable name as uh mail message okay this is done also we will have to replace this dollar message with mail message okay so this is done now let's switch back to browser let's refresh this yes so it returned sent message response set this we can see return this message ID that means it has been sent let's check the email address so yes we can see have received this email we have this title subject and we have this message here we can see form is coming from larel so what we can do can change this name let's come back to the code here this envelope and add that have to pass this from address and inside this we will pass new address okay inside this function have to pass the address let's give our email address and also the sender I will pass the sender name as programming Fields also we can pass reply to email inside this and we will have to pass array there can be multiple reply to email address we will write address function and inside this I will pass that reply to mail address and the name as well this is done we will have to import this address mailable class so we have to add use illuminate mail labels and then address okay this is done save it and let's switch back to browser refresh this application once again email has been sent let's navigate to that email address so I have switched back to that email address and uh yes we can see now we have the new sender information as name and email address also we have this reply to email address and name all right and there you have it you have successfully implemented a functionality to send email using Gmail in LaRon I hope this tutorial will help you in your real projects in the next video we will extend this and see how we can send file as an attachment in the email so stay tuned with programming Fields thanks for watching don't forget to like comment and subscribe for more larable tutorials like this see you next time until then happy [Music] coding
Info
Channel: Programming Fields
Views: 3,851
Rating: undefined out of 5
Keywords: Programming Fields, programmingfields, laravel, laravel tutorial, Gmail, Email sending, Web development, Email integration, SMTP server, Laravel tutorial, Gmail tutorial, Laravel email, Laravel Gmail integration, Laravel advanced, Email management, Programming tutorial, Laravel tips and tricks, Laravel Gmail Setup, Laravel Gmail Integration, Laravel Send Email using Gmail, Laravel send email, Laravel, Laravel gmail smtp, laravel mail, laravel email, laravel gmail, gmail setup
Id: H5ipv4vu0to
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Sat Mar 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.