Send emails from localhost server in PHP via Gmail or yahoo mail | Quick programming tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to a tutorial where i teach you how to send email from your local host using phpmailer your gmail account or your yahoo account let's get started so if you have gmail or yahoo we are good to go so open up your browser your web browser here and what you need is also zamp here so make sure that you've downloaded xampp or you have another server of some kind if you're using uh zamp just open the control panel and start apache and mysql now if you don't have xampp just go to the internet google it and you can download and install just a normal installation install to drive c okay great once you're done open the control panel and start both apache and mysql okay now in order to start go to your google and google php mailer because we're going to use php mailer to mail our to send our emails so find this github link and go to github once you get there click on code and download the zip file here if you are good with composer just use composer to download it i'm sure there are instructions down here okay so once we are done with that that is good you can now open up your file system go to drive c zamp htdocs forgot that's the the folder i created called forgot inside the htdocs folder so find the htdocs folder create forgot and then paste your zip file here and then extract it so select extract here not extract to another folder but just say extract here because there exists a folder already so the file system should be php mailer master and then immediately inside there should be the source folder otherwise things won't work okay so if the file structure is like this we are good to go so i have a file here called mail.php which has a boilerplate or template that we can use to send emails so now what you do is grab this folder the forgot folder that you created and added your your folder here your php mailer and drag it drag it to your text editor so i'm using sublime text as my text editor but you can use any text editor of your choice so this is the folder right here and here the file so in here there's mail.php which has the boilerplate like i said and this is the code right here so in case you can't download the file will be in the the link in the description where you can download this file but in case you can't download this file for some reason you can just copy the code right here so no problem what we are simply doing here is we are requiring the files that are inside php mailer which are these files in the source the other files are pointless the only folder that is important is the src folder and these are the classes in there so those are the classes that we are that we are including here so once we include them then there's a function here which creates a new mailer object and then it sets a few parameters here and at the end of the day if the email is sent we we get email sent successfully if it is not we get error while sending email and then we'll get some error messages somehow now the important part here is this section right here so it's very important that if you are using gmail you put the host as smtp.gmail.com okay and then if you're using yahoo you're going to put smtp.mail.yahoo.com on this one here okay so either of those is fine and then you put your email address and then you put your password okay so now how do you get this password here the password that you use to log in to your account will not work you have to create this password separately so let's see how we can actually create that password okay so let's go to our email addresses and see how we can go about doing that okay so if i let me start with uh yahoo so this is my yahoo account over here and if you go on the top right corner here there's account info there's a drop down here just select account info or wherever you can find it and hit go and then it will take you to this page where there is personal info account security and recent activity so just click on account security and then scroll down here and once you reach down here there's this part which says app password and then when you get here uh click on generate and manage app passwords and just type an app name of your choice here doesn't matter what it is and then just click generate password it will create a password and then just copy that password and that's the password you're going to add here okay great so that's for yahoo let's go to gmail and see what we can do about that so in gmail you have to go to [Music] myaccount.google.com so you go to your account settings here so you can do this with your mobile app the youtube settings the youtube settings app or something so once you get here go to the security tab and in here if you scroll down now the options will be different depending on what whether this is set or not if this two-step verification is set to off then it means just scroll down to the very bottom of the page and at the bottom there you're going to find a setting called allow less secure apps and when you click on that one you can switch it on so it's just a toggle button you switch on and off but if your two-step verification is on just like in my uh channel here in my account all you have to do is click uh app passwords here so once you click here you'll be asked for your username password and all that and then you can generate a password just like on the other side now if you don't generate a password and you you are the type that doesn't have two-step verification if your two-step verification is off and then you go down and switch on allow less secure apps it means the password you have to add is your login password the same password that you use to log into your email so you put your email there and then you put your password down here okay good but if you have these two steps switched on you add an app password that's the password you're going to add here okay so now that we are done with all this uh preparation let's actually try and send an email and see if we can receive it so in order to send an email we have to create an index page at least so let's go back to our folder in the forgot folder right click and click new file and in here uh we're going to set this as index.php so i'm going to save this as index dot php save there okay so once i save here i can now put some php tags there actually let me use html like so i'll just say website or home but at the top here what i want to do is add some php tags do that and then include my include mail dot php like so okay great so once i've included mail.php here then this is going to be included and there's a function here which i just need to provide these things there and that will be it so one more time in case you didn't see all the code here here it is hopefully you can see it clearly and copy it okay so once we do that i just need to run the function like so so i'm going to mute this one duplicate it just so i can see what comes first here so the recipient is i'm sending to myself so okay so i'm sending to myself and what is the subject subject is a testing testing from php something like that and the message is just going to be hello there okay just like that and then here we're just going to add an if statement and then if it is true then uh we're going to echo out let's say uh message sent like so otherwise we're going to say error message not sent something like this okay so ready let's go back here i'm going to refresh this page the moment i refresh it's going to load the index page and it will send an email so refresh there we go ready ready ready so it takes a while to send okay so email send successfully message sent which is good so if i go to my gmail now i've seen the number has changed and i see my email here testing from php hello there so if i click here i do see my content over there so you can see this one is set from name so which if i come back here that text is this one so you can put a recipient name here if you want and actually this doesn't work well because unless uh unless you up add it here so that you can change it on the fly otherwise it's better set from name here you can set your own name here or if in case you have your website you can set your website name so let's just say my my website com like that okay i think that's it recipient name mywebsite.com so you add your company name here so that once you send it has that officiality so let's try again and let's see if that changes so i'm going to refresh the page one more time ready okay taking a while but it actually went through so there we go testing from php again hello there but this time you see it says mywebsite.com at the top there which is good all right makes it look a little bit more official okay great so that's how you send email using phpmailer and gmail or yahoo account so now what we need to create is a form of some kind so that we can just type our message and send it as email okay so here what i will do is i'm going to add an if statement to check if there's something posted using the post variable like that so we're going to say if count uh the number of items in the post variable is greater than zero then it means something was posted so something was posted let me put two double slashes there and then let's close this tag here like so okay so this only runs once we actually post something and then here i want to send uh to set these variables so there's recipient there's a subject and there's message so i'm just going to replace these here like so okay and then i'll paste them here again just replace the the commas with the carriage return so i'm going to put an equal sign here and equate this to post like so and obviously there's going to be email and then there's going to be subject and then there's going to be a message like so all right so great so we have a recipient we have a subject we have a message these are the variables here we send to the function all right so once we do that now uh we need a form here so i'm just going to create a form let me zoom in a little bit and i'm going to give it a method so the method is going to be post like so and then i will add an input here of type text of course the name is uh email like so and then let me add a placeholder receipt how do you write recipient let's just write receive by email like so because i just can't remember how to write recipient for some reason anyway there we go so now let's duplicate this so we have email we have a subject so we have to match these names with these ones right here so email subject message so one more for message now the message should be a text area because we need to add quite a bit of data so name is going to be a message like so and uh yeah that is it uh receiver email subject like so and then finally let me put a break tag here and another one finally we put an input that is of type submit we don't need a name we need a value and the value is uh send like so let's put capital s there send great okay so this is good uh let me put an h3 here and just say send email like so and then i'm just going to put a message here saying uh [Music] actually no need for a message here what i will do is put a div that will enable us to display errors in case there are some okay instead of echoing the message here we're going to set it to error so here at the top i'm just going to set error is equal to empty string like so so it's empty at the top here but then down here it's going to be error is equal to message not sent or error is equal to message sent not really an error here but i just want to use the same variable so i'm just going to say if i'll add an if statement so let me put my php tags here and say if error is not empty like so then of course we are going to do something so let me close that like so put some php tags again and they say end if and close that tag as well and then here what i will say is i don't know i'll put a span here and just say instead of using the php and then adding echo i'll just put an equal sign and just say error like so close that tag as well okay so here we get to see the error and then i'll add the style here and i'll say color is equal to red just so it shows that it's a message over there okay great so let me come back to the website here and things will not run here because so we have a syntax error on line 22 unmatched where is 22 this one it's saying it's not matched by anything so i forgot to add a one on top here like so okay great so refresh okay so this is what we have send email a receiver email subject and the email itself so let's add a few a bit of styling here so that it looks more presentable so what i will do here is on the body itself i'm just going to add a style for font family so i'll add font family which is tahoma that's good enough okay and then i will add some styles here style tag like so and let's add a little bit of styling here so i'm going to start with the form and in the form here i am going to uh in the form i'm going to add let's see width give it a limited width of 300 pixels and then i'm going to add some padding padding of 10 pixels so that items are not too close to the wall i'm going to add a box shadow 0 on the x 0 on the y 10 pixel blurriness and then add a gray color like so then i'm going to add imagine of auto that way it's in the center i'll add a margin top just so there's more space at the top and what else a border radius for rounded corners so border radius of 10 pixels something like this okay so let's see what we've got refresh and there we go it's looking much better and now i want to uh to style these boys here so for these guys i will do input like so any inputs but i want the ones in the form so say form then input and here i will give them a width of let's say 290 pixels since the owner is 300 pixels i'll give them a padding of 10 pixels and then i'll give them a border i don't want them to have a border here so i'll say solid oh i don't want that 3d border i mean and then put that gray color as well and let me give them a border radius as well over 10 pixels and that should do so let's come back here and see what we've got okay there we go let's give them a margin so say margin uh 5 pixels something like this okay very good not exactly what i needed so let me reduce the width here so the width to 280 maybe so i'm just looking at the text boxes maybe 270. yeah something like that this looks much better and then if you don't want those outlines you can say outline none and then this first one obviously here i want it to autofocus to be the first thing to be focused so i will put autofocus to true okay refresh and there we go so no no more outlines here great now these are different lengths so what i want to do is set that one separately so what you can do is you can create a class for each separate item but here i just want to use form input again oops let me close that and put a width of [Music] maybe 290 pixels this is just for the button so the type here i i will say type is equal to submit like so and close that because the button is the only one with submit so if this doesn't work for you uh just create a class with a dot just create a class like dot btn like so then delete everything else here and then put that class here just give it a class say plus btn like so that is bound to work so refresh that's looking much better now to uh so maybe i can use the same class here for the button here let's see how that works not bad but not really so let me put a class of text there dot text and just say uh i'm going to copy some of these things like the border radius margin copy and also the border itself where is the border here let's see that okay that's much better and let's get the width correctly okey-dokey i don't know why it goes on to i don't know why these um these controls are so different when it comes to positioning it's just weird okay so let me just put a margin on the left maybe just so i don't have to suffer i'll say 10 pixels okay a bit too much maybe five i want them to align a bit better so let's try to 85 here okay we can continue tinkering the whole day and we won't get anywhere so instead oh actually it's because it doesn't have padding that's probably why so let me get that padding here and put it in there ah there we go that was why so then i can copy the same width here like so and remove this margin left so maybe it's working yeah the margin left is working so let's leave it there okay great all right so at least we have something that resembles um the button here let me put a cursor to a pointer like so okay so now we should be able to send an email okay great so if i do send an email here uh oh let me come back here to the mail mailer and i'm going to remove the echo here and echo there there's no need for us to have that even the va dump like so remove all that and refresh now we try to send message not sent okay so you can add specific errors here for example if i come back to the index here i can say if so i can say if let me do this if error is equal to empty this is the only time we should run this so let me move this something was posted to the top and then let me push that in there like so open and close that okay so if there's no error then we run this but here we have a chance to create an error so i'm just going to say if empty if empty recipient then we can send an error there so i'll copy this error message and put it here recipient cannot be empty like that and then let me uh do that in that like so now i can simply add to the error message here like so by adding a dot like that dot dot but then i'll have to put break tags on here so that they these messages are on separate lines break tag like so okay but that should work then i can change recipient here to uh subject for example and then here message like that okay great very nice very nice so now if i refresh the page i get a recipient cannot be empty subject cannot be empty okay so once i add those then i can uh try and send let me add that and then the subject is uh some subject and then this is a test message to you something like this then let's hit send okay seems like it's sending after all okay message sent which is a very nice okay so if i go to my gmail now to my account i should see it come here any second now since the message was actually sent maybe takes a while okay seems like it's going to take a while so i will pause the video and confirm a bit later if it actually came in or not all right so it actually arrived some subject uh this is a test message to you so there we go that's where it is okay so that's how to send an email from your local host system hopefully you have learned something new and i will see you in other videos
Info
Channel: Quick Programming
Views: 7,587
Rating: undefined out of 5
Keywords: Web Design Programming Tutorials, web development, quick programming, PHP, MYSQL, HTML, CSS, tutorials, Learn, learn to code, course, coding, json, javascript, svg, website, programming, php tutorial, php login tutorial, how to create a login system in php
Id: gortnFd7hgU
Channel Id: undefined
Length: 31min 14sec (1874 seconds)
Published: Thu Jun 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.