How to Send Email Using PHPMailer in PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] email from your PHP server we'll be seeing how to add attachments to the email also for that you have to go to the website github.com phpmailer and you can see this Library I will give the link to this on the description so you can check that out and then you can scroll down and you can see composer require PHP mailer slash phpmailer so let's copy this in order to run this command you need composer installed if you don't have compost installed simply search composer on Google and you have to select the first option and you can see download so click on download and here you can see composer setup.exe so simply download this and you can click next next and complete the installation on Windows so you also have to have PHP so if you don't have PHP simply install samp so for that also you can simply search samp and you can complete the installation by clicking Nest okay so after installing composer you can run this command so copy the command and you can come to the editor and open the terminal you have to make sure that you are inside the exact folder of the project and then you can paste it here so we are running the command composer require PHP mailer slash phpmailer now we have added the PHP mailer to our project if you check the project folder you can see that we are having a folder vendor and composer.json and inside the window we are having Auto load.php and also PHP mailer folder okay so we have to record this Auto load.php to the index.php file so before that you can go to the documentation again and you can scroll down and you can see a sample code you can see the sample code here so if you can scroll down you can see all the necessary code to send an email using this Library so we can copy this so click on here and we can copy the code and we can paste it here so you can see that we are requiring the autoload.php from the vendor folder here and we are also using the classes that is phpmailer SMTP and exception also from the library that we just imported okay then we are creating an instance for the class creating a variable here then remove this line and here you can see that we are specifying that we are using the SMTP then here we have to give the SMTP details if you don't have an SMTP server simply purchase one from the internet and you have to use the details here okay so here we have to give the host name and here give the username and the username will be a email so give the email here then here we have to give the password to this email account okay that's all okay after that the port will not change I guess if the port is different you have to give a different port okay so you have to make sure that the details that you give here are actually matching with the details that your SMTP provider gave okay then you can come here and here we can give the from address so let's change the details here we can give a name for the sender and this is one for adding the receiver so we can give the receiver's email here you can also give the receiver's name here so here we have to give the receivers email and here the name of the receiver then here we can see a line this is also for adding the receiver so let's remove that line we'll be adding one receiver and this one is the replay to email address so you can add any other email address if you want the users to reply to a different email here and we can give a name and if you want to add CC and BCC you can add that here I'm not adding this right now so if you want to add you can add that then here we have to attach the media if you have any attachment to add to the email so for now we'll be sending an image file through the email so let's download the image file you can see that I have added a folder assets and inside that I have placed an image so you can see this is an SVG image okay so we'll be sending this SVG image through the email so you can specify the path here so the path is assets slash the folder name so let's copy the folder name and we can paste the file name here okay then if you want to give a different name for the file you can give the name like this so in this line you can see that we are passing the path and also the name for the file so let's give a name for the file okay we can remove the second line okay so we have attached the file to the email then here we have the content part so we have set the HTML part this will say that the email is set to HTML okay then we can type the HTML here so this is the body of the email and this is the subject if you want to change the subject you can change it so we are going to test this so I won't be changing the content for now okay if you want to change you can change all the content here according to your wish and here we are calling the send function and this will return true or false according to the processes success or not okay then we are simply echoing message has been sent if any exception happens we are showing an error message also okay so that's what this code means okay hope you understand now we can try running this so before running I'll be changing this details here to one that I have so I won't be showing the details of the SMTP server so I have changed the SMTP details of my server and now we can try running this file now you can see that I have opened the project here so I have opened it inside the samp so localhost slash phpmailer so you can see it is sending the email that's why it is taking time so we can see the message message has been sent okay now we can go to the email that is support at code cc.com and check if the email was received so I just noticed that I have made a mistake with the email address so the email address is c-o-d-e-s e a s y okay dot com okay so this is the two address okay let's save it and run it once more so now I have refreshed the page and the email was sent once more so let's go to the email and check it so the email was received you can see the subject here and if you click here you can see from where it was sent and the reply to email address and also you can see the email server and also the send to email address etcetera so here you can see the body of the email and this was in HTML you can see this in bold so this one is the attachment that we added so if you try to download this you can see this is the same file that we just sent through the email okay so hope you understand so that's all with the implementation part so hope you understand how to send an email with an attachment so we have done it on PHP if you have any doubts you can leave a comment below and I will respond to that if you like this video give it a thumbs up and subscribe to this channel for more such videos
Info
Channel: Codes Easy
Views: 50,920
Rating: undefined out of 5
Keywords: codeseasy, send email using phpmailer, send email using php, php send email, send email in php, send mail using phpmailer, phpmailer tutorial, send mail using smtp phpmailer, phpmailer send mail, send mail using php, how to send mail using phpmailer in php, sending emails with phpmailer, learn how to send email using php, send email using phpmailer in php, send email using php smtp, php send email smtp, hostinger smtp, php send email with attachment, send email php
Id: GmCFeLhA-fA
Channel Id: undefined
Length: 8min 36sec (516 seconds)
Published: Wed Jan 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.