Send Email with PHPMailer from Localhost

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so for this particular video demonstration i just want to do a walkthrough of all you can use php mail to send email from your php application so i'll walk you through how to generally set up php email in your project and also show you the necessary things that are needed to be done in order to send your email all right so first things first if you are at this point in in learning php and using php you should know that your apache server should be running so i have my apache server running via zump all right now the next thing i want to talk about is the fact that i'm going to use an application called composer to basically install php miller in my project right so what you can do is you can just type composer let's say php search for that all right this little icon here symbolizes composer your link is here so you can just click on composer and it tells us that composer is a dependency manager for a php meaning if you are working with libraries or frameworks in php then composer can easily allow you to um install those packages are those those libraries those frameworks all right so composer is good for that now the next thing i want to go ahead and do is to just generally take you to php miller and you can get php mailer from github it's free for use all right it's a very good library to use you know for sending emails but it is also free it is free and good all right so click on this github link and you can simply grab these files and place them in your in your application so you can simply just download these and place them in your in your php application now what we will be doing though is we will be using composer to install php email in our application and this is the instruction here in terms of the command that we should type if we are going to be using composer to install php mela which is what we will be doing all right so what i'll do first off is i'm going to go to zump my exam folder that is then go to hd docs i i'm going to create a folder within hd dash card my email and within that i'm going to create a file called email.php where we'll be putting the php code all right all right so i am in my c drive going into my exam folder going into hd docs and i am going to create a folder called my email all right so i created my folder and i'm going to create a file in this folder called email.php alright so i went ahead and created this email.php file so the next thing i'm going to do is just open visual studio code and then i'm going to open the folder inside visual studio code all right so i need to find the my email sorry my mail folder which is this one that we created i'm going to open this inside vs code all right so the folder is here so what i'm going to do now is i'm going to use composer to install or at least bring over the php mail files inside this folder here so to do that i'm going to open a new terminal inside vs code and the thing is i already have composer installed on my computer so again you can just go online download and install composer or you know follow the instructions in terms of auto install composer it's very useful if you're going to be working as i said with php frameworks like laravel and if you're going to be working with certain um php libraries for example php mail all right so the location where we want to install the php miller or at least of the php mela files is in our folder here the miami and folder so my instructions will be composer require php mailer slash php mailer all right and this should do the trick in terms of setting up php mila in our project i'm just going to hit enter and just give it some time to do what it needs to do all right so if you realize that over here to our left of vs code the php mail of files were brought in for us now there is a subfolder here called vendor if we should expand or open this folder we should be seeing a folder that is called php mail and if we expand that then it has all the necessary things that we need all right now let's close these well this is the source folder here with the php mila class the smtp and all of that all right so these are all the files that we will will be can use off now if we should close these let me just close this now there's this file called autoload.php which will allow us to load all the files that i just shown you that are in the source full so this auto load will basically allow us to utilize those other files all right now i have my email dot php file here currently there is nothing in it no code i'm just going to bring over some sample code that i had pre-written now what i'll do is i can provide you with the code just in case you want to play around with it and and all of that all right so this is always the sample code here you know importantly we need to ensure that we are using these two classes the phpmla class and the exception class again we can find these inside the php mail or folder inside the source folder you should be seeing exception.php and phpmail.php all right so those classes are included in these two files all right so these must be at the very top of your php script we're also going to require this auto load file which as i said will load all the necessary classes that we will need all right here we are making an object of the phpmla class and we are calling it mail next the actual code to send email is placed inside of a try and just in case there is any issues you know if there is an arrow that is thrown if an exception is thrown then it will be catch and handled here all right now just to do a quick um summary of what is going on here this smtp debug will display various messages and the messages that are displayed or what is displayed is based on what this number is two here signifies that it will display all the debug information as well as any user defined um output or any programmer stated output so for example it will display all the debug information as well as this message right here if it was sent successfully or this message if there was an arrow all right so you can look into the smtp debug for php miller it uses the numbers one two three and four i'm trying to remember zero is used as well but i know one two three and four are used here i can be used here all right this is smtp will basically return to our files true if you are using smtp if you are using an austin service to set up your your php website then you don't generally need to use this because that should be the the platform to send the email would have been set up for you already right so but for us we are using smtp so we'll leave this in now here where it says us you will actually state the smtp server that you are using now a simple smtp server that we can use is gmail all right i'll also mention another service that we can use for smtp server and it's called sendinblue so i'll first send an email using gmail as my smtp server and then i'll change and use sending blue as my smtp server all right now going through the code you'll need to place the username for whichever smtp server you are using the password um once your smtp uses port 587 then you can leave this here you will state the email address that the email is coming from and you can state your recipients here all right this is hello in two recipients i'll probably just comment on this one and send only one recipient all right next you can state your subject for the body you can send your email as an html or with an html body and you can also use an alternate body so let's say for example um html the html body cannot be handled basically based on oh you are sending or whichever platform you are using to send the email let's say html body html body cannot be handled or it cannot be read what will happen is that the alternate body will be shown instead and alternate body is actually in plain text so the message that you placed in the html you can place the same message here but i have different messages just to differentiate between what we are what we are um sending so once my email client can undo the html body then this will be sent if not then the alternate message will be sent but generally for you it will be the same thing all right and then our send function here is used to obviously you know execute the sending of our email if the email was sent successfully then this message will be displayed if there was some error preventing the email from being sent then this message here will be displayed all right so back up here i'm going to use as i said gmail as my smtp server and i would have already configured gmail for this purpose i am going to use my all right so i want to use this gmail account and i'm going to type my password here as well all right so after typing in my password i'm going to leave my smtp secure as tls and we're going to leave this port number i know where i want to send the email i am going to send it to this account and i'm going to say that the person is going to their name is john williams sorry this is where it's coming from all right so this is the person it's coming from all right um let me use this as the recipient so i can easily go here right and let me use this as the sender let me use okay so that's where cinder and this is the person who will be receiving the email i'm going to leave everything just the same leave the message the subject as what it is no what we're going to do now is actually see if this works so we are at localhost my mail slash email.php all right so everything you're seeing here is the actual debug information you can comb through this and just take a look at all the necessary things that are being displayed no because we had use the value of 2 that is why it is showing all of that information all right no it also shows the client defined or the user defined information here which it is saying mail has been sent successfully so i'm going to go to my email as you can see the email was actually synth all right platform that we can use as our smtp server and the reason for this is if we're going to be using gmail as our smtp server then we will have to basically allow our gmail account to become less secure which is not necessarily what we want also a new gmail as plans to change all that is done in terms of them allowing third-party applications to use gmail um i think they'll do that in may of 2022 so i'm not sure exactly what that would mean in terms of us being able to use gmail as our smtp server i guess i'll have to do some more reading on that but for no we can utilize sending blue if we want to test our our email sending abilities using php mail all right asset abilities all right so just simply google send in blue and there's a link here i already created an account so you can simply you know go and create an account sending blue is actually a digital marketing tool that allows you to do things like email marketing what they do provide us a free account where we can use it and use um them as an smtp server so as i said i'm already logged in so i'm just going to go to send in blue well i guess i'm no longer logged in so let me just quickly um go to my dashboard all right yes so i'm still logged in all right so once you set up your account you are logged in um you just simply go to where your profile is you should be seeing smtp and api click on that option then click on smtp now you'll see where the of your smtp server information they tell the port that they are using they tell you the account that you are using or the username and they also provide you with your password here your smtp password all right so i'm going to take this information from here no we had gmail here so we are going to use sending blue instead so we paste that there i'm going to grab my username from here i'm going to paste this here and i'm going to grab the password as well and paste it here all right so everything has been set up to use um sending blue so again i'm just going to place a recipient here name here well sorry again not recipient but the sender of the email so let's say the email is coming from and let's see this person's name is mike watson all right and the recipient no let's use so let's make this person our recipient right and we will leave everything as is all right so we can go ahead and test this out again it's just going back to here let me just copy this so i could have just done a refresh one let me just copy so paste it and go all right it says message could not be sent oh because the email the sent from email is incorrect say should be 2l right that should be fine just refresh um again all right so i'm trying to send an email without putting the dot com so you know yes i'm trying to move a little bit too fast so we should be good now so that should be a valid email address no all right so you cannot send from an invalid and you cannot send to an invalid all right so all right we're good to go now um so all the debug information was displayed and here it says mail has been sent again it should have been sent to this email address and if you look here we are not seeing any email any new email here and i'll tell you why all right so the thing about it is because we are using sendinblue and they are a marketing platform instead of seeing the email in our primary inbox we will see it in the promotions inbox right so if you check it says that this email came in from mike watson which is the name that we used here so if we click on this it's coming from mike what's it was sent via sendingblue.com as the smtp server right it shows that the from address is this one here again which is what we used here alright so generally if you want to use php mail within your application it is very easy in terms of using composer to bring over all the files that you need instead of you having to download them you can just use composer to remember the files that you need i guess the next step from here would be to create a contact form and then you know include php media with that so you can receive and send emails via a contact form so that might be our next video or you can you know try it out yourself see if it works um if time permit though i will do another video on on how to you know use the contact form and to send the email using php mail but for this particular video that is it
Info
Channel: 876WilliamsTechJA
Views: 14,074
Rating: undefined out of 5
Keywords: PHPMailer, Send Email with PHPMailer, PHP, SMTP Server
Id: wgMVmFogQ94
Channel Id: undefined
Length: 24min 17sec (1457 seconds)
Published: Wed Apr 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.