PHP Tutorial: How To Send Email From Localhost Using PHPMailer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Do you want to finally learn how to send an email from your localhost? In this video I'm going to show you how to create a contact form, how to use jQuery to validate all inputs and how to send an email from the localhost, so stay with me... before we start with this tutorial please make sure that you have installed some apart - like exam and also make sure that you have one gmail account available for use so first thing that I'm going to do here is outer squeeze the basic layout for our contact form okay so I will say here contact phone after that I'm just going to get the bootstrap okay so we can quickly and easily design our page okay so I'll paste it here and after that I will just create one container in the container we are going to have one row okay maybe let's just just find content to the center then we are going to have one column md6 with md offset 3 and that way we are going to have everything in the center of the screen so maybe here I will set everything to align to the center and then here I will set margin top 200 pixels okay so inside this column we are just going to create the inputs that you are going to need so those will be the basic input for one contact form so let's say name email subject and the body okay so now let us say here name IDs name then placeholder name and that's it okay maybe we can add some class like form control so I will duplicate it three times so this one will be email okay and then this one will be subject so email subject and then we are just going to have one text area and that one will be our body okay let's say email body and then of course we must have some button okay so let's send an email we will set some boost up classes okay [Music] and that's it so let's see what we have created so far okay this is looking pretty solid now the next thing that we need to get is the jQuery so we can quickly and easily verify this for okay so I just like to have it in the one line not necessary but that's what I like okay and then here we are just going to define some function that we are going to call here so maybe let's say send email so I'll say here function send email okay and then what I want to a tree with this function is when we click we will actually verify that each of those inputs for now are just not empty so they have some value okay and then I have some other tutorials that you can find my YouTube channel where I explain how to use some advanced method to validate the inputs but for now we'll just do something simple so we are going to have here name okay so all the same name then we have few others so it's email it's subject okay buddy so I want to create one additional function that I will say like if it's not empty so what I'm trying to achieve here is I want to be able to say like if it's not MQ name and it's not empty email and it's not empty subject and it's not empty body in that case we are going to send an email okay so let me tell it like this you guys can see and then here I will say that this function receives on caller and then here we will test it and see if core value is MQ we are just going to say like call dot CSS order and you can say one pixel solid red and in that case we will return the false else we can say caller CSS border is empty and by default function will return true okay so let's test that part okay sent an email nothing happens why let's see maybe for some reason let's try this okay kick now it works which is pretty much over here is a some name email subject body but now it works with this weird but anyway okay cool we cannot delete this and now what we want to do is we just want to make an AJAX call to our PHP side from which we are going to set an email so let's quickly write one okay now we'll just specify some parameters okay so my URL is just this one the method that I'm going to use first data type that server will return is just let's say JSON and then data that we are sending to the server is let's say name is email subtract an email okay then here we are going to have response okay and maybe for now let's just print the response to them console.log okay so now let's here verify or maybe it will be better if we just create separate file so that it's easier for you guys to understand so just create another file here so I'll go you PHP file it is an email okay and then here I will say if is a post name and maybe let's see and this post email in that case everything is good we are going to send an email so first we need to accept all the data that is sent through a tracks okay so here is okay so now that's all that we need so what we can maybe do is verify if an email is correct if the name has certain number of characters if the subject is proper length etc etc and for all of these there are plenty of tutorials on my youtube channel that I am showing how to do so I'm just going to lose your time here so for now we are looking to send an email and for that reason we are going to download PHP mailer class okay so there are multiple ways on how to download and install but I will just use this the simplest one okay it's gone my another monitor just a second okay it's here so I'll taste it he'll say extract here okay we can delete this okay I will change the name to the PHP mailer and then here in the source we have all the files that we need to help so I will just delete all the other ones paste it here and that's it okay so here now you can see that we have new folder and all the classes that we need to help so here at the top I will define the namespace that you are going to use then I want to include all the necessary files classes that we need ok so since we are going to send through SMTP we will need that one and then of course we will need to include the exception class okay so those are the three basics that we need there are some others that you can use but I'm going to explain that in another tutorial ok so now first thing that we need to do with PHP mailer is of course to create a new object okay and now we need to put some settings so first I will say SMTP settings that we need to set so first I will self is SMTP and for this line later I will explain and show you so that you know when you need to have this line and when you don't need to have that makes the big difference depending on your local host now the next thing that I want to set is host so like I said we are going to use the Gmail so for that reason we need to set it to the Gmail then we need to set SMTP the authentication to the troll then we need to specify the username so I will just use my email then for this email we will need to have the password I will copy/paste it later then we need to have port that you are going to use and now there are two different cases that we can use its SSL and its TLS for SSL its 465 okay and for the TLS is 587 okay and then okay SMTP secure is in our case as a cell but like I said it can be also cos sorry cool and now here we just need to set settings for email so I'll say email settings and those are the basic ones so first one is who is sending an email okay so we can say that the person that is sending is from an email and his name is name so those are the details that we have here okay so now we need to set where we are sending an email so let's say the email where I'm going to read it will be just this one okay and please guys I know this happens all the time with my different tutorials you need to make sure that you change this in your code so that when you test you don't send an email to me but you send an email to yourself I mean this is something that happens all the time my inbox is getting crazy with this okay and then we need to set the subject okay that's our subject from the top we need to set the body and that's the one that we have a bow and what we can do also is to say is HTML okay and I want to set it to the show to make sure that this is going to be an HTML type of the email okay and now the last thing is we need to just send it so what I can do is say F mail send okay we'll say echo email is sent okay else echo something is wrong okay and then maybe we can say mail error info came here we can say here to you once okay and that's it maybe this will be but if we say here because we said in jQuery that we are going to use JSON so for that reason we can set to the variable and here I will say JSON encode and let's say to be an array just say for now response response okay cool so now let's first for me to get this password so I need to go here I will say here password and I will paste it here okay so I have some dough outside for that reason I will just use single quotes okay so let's see what will happen okay cool an email it says something is wrong invalid address root at localhost okay so now so let's see there is live.com now we are going to set send an email okay and now it says that SMTP connection failed now this error we usually get if on our dream ale account less secure apps are not turning on so how we are going to activate those basically you just need to go to the my account google.com slash less secure apps if you are logged in only to the one gmail account at the moment I have two so you can see here I have user 0 okay like this and then I have user 1 okay so as you can see for this one confessing income less secure apps are turn it off so I will just turn them on okay and now sometimes it takes 15 to 20 30 seconds in order for Gmail to activate it but usually it will be done pretty fast so now if we try again let's see what will happen it says email is sent ok and now probably if I go to my gmail account in my inbox you can see now that I have received a new email from my local host ok and now there is one thing more that I need to show you how to fix and like I said it's the problem with this line so as you can see in my case I was able to send an email while this line is here and if I remove it ok let's see what will happen so now I hit send an email it says could not instantiate mail function so that means that I need in my case to have that line but there will be also the cases when you will receive this error because you have that line of the code so it really comes down to your own personal settings that you have so just experiment with that ok so now once we have done this you can see that sending an email from PHP and from your local host is pretty much super simple ok and now the last thing that we can do is maybe just to say here that in this case it will have like status success and then here we are going to have like status faileth okay and then inside our index.php we will just check it for its own sake if response that status equal success you can see our email has sand que eles you can say other please try again like that's the error and then in the console you can print to yourself and see what's wrong okay so guys pretty much that's it I hope that you have enjoyed this video and that finally you will easily be able to send an email from your localhost and if you have any questions I will be happy to answer all of them in the comments below and of course if you like this video please like it and shape your friends they give
Info
Channel: Coding Passive Income
Views: 53,244
Rating: undefined out of 5
Keywords: phpmailer tutorial, mail, phpmailer, email, tutorial, smtp, html email, tutorials, php tutorial, learn, howto, gmail, php, how to, how to send email, how to send email from localhost in php using xampp, how to send email from localhost, how to send email from localhost in php, php mailer, send email in php, php send email, how to send email using php, localhost, send email php, how to send email in php, send email with php
Id: t0zwgJrSHd4
Channel Id: undefined
Length: 22min 59sec (1379 seconds)
Published: Mon Feb 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.