Send Email from localhost with PHP using PHPMailer and Gmail SMTP server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys in today's tutorial we are going to see how to use the PHP mailer library and Gmail's mail server to send emails with PHP so let's see how we do this right now on the screen you see an email form the form has an email a subject and a message input field using this form we can send an email to any address that we type in the email field if I click on the submit button we see a message that says all fields are required this means that we will also be validating the input Fields now let's send an email to my Gmail account I'm going to enter my email I'm going to say hi George and as a message hi from Greece I'm going to press the submit button wait for a few seconds and we see the message email sent successfully now let's go to my Gmail account and here is the email that I just sent from my Local Host server nice now before we jump to the editor and start coding we have to do some things first the first step is to download the PHP mailer Library so let's go to GitHub and download it from there I will put a link in the description so you can find the page easily click on the code button and then download zip store for now the zip file in your downloads folder we will use it later when we see the Project's file structure next step is to create a Google app password so we can use Gmail as our mail server the important thing here is that in order to use Gmail's mail server we have to run we have to turn on the two-step verification process don't get overwhelmed it's an easy process go to your Google account click on the security Tab and select two-step verification and complete the process if you already have twostep verification enabled just go to the bottom of the page and create an app password the generated app password is a 16 character code copy and store it so we can use it in our code that's it now we can start coding in my editor and on the right side we see the Project's file structure as you can see I have extracted the PHP mailer zip file that we have downloaded and placed it in the Project's root directory if your PHP mailer folder has a different name like PHP mailer Master just rename it to PHP mailer so your folder has the same name as mine now let's see the files that we need I have a CSS file in which I already typed the CSS code the CSS code is not important for this tutorial so I will not go through and explain it if you like the forms style you can copy it from my web page there is a link to the article in the description next we have a config PHP file in which we will Define some values that we will use in the PHP mailer object next we have a script.php file in this file we are going to write a function which will use the PHP mailer object to send the email and last we have an index.php file which is our starting page and holds the contact form as you can see I have already typed the form so we can save us some time let me explain what's going on here the important things are the action attribute has no value this means that the form will submit to the same page the method attribute is set to post this means that we catch the values in our PHP code fetching the super global Post variable next we have the email field the subject field and a text area to type the message and last the submit button you already know this but let me say it again that we are going to use the values of the name attributes in our PHP code to fetch whatever we type in in those fields okay enough with the introduction it's time to code and we are going to start from the config.php file now as I said in the config file we are going to define the constants that we are going to use in the PHP mailer object the first constant that I will Define is the mailhost and I'm setting the value to smtp.gmail.com .c this will instruct PHP mailer to connect to this to the SMTP mail server hosted by Gmail next I will Define my username which is my Gmail's email address that I use next we're going to find the 16 digigit app password replace the text with your with your 16 digigit code that you have created next we have to define the send from address here I'm setting sa a value an email address from our website that is because I'm already using this code on my live server if you want to use this code on your website replace my website's email with yours next we Define the send from name again I'm using my website's name next we defin the reply to address I will use again my website's email and last in the reply to name constant I'm going to use my name and we're done with a config file now let's go to the script.php file and use all those all those constants that we have created the first thing that I will do in the file is to put the PHP mailers name spaces at the top a nam space in PHP is a way to organize our classes and functions to avoid any naming conflict in general it provide us a structured way to manage our code let me show you let's open the exception. PHP file here is the namespace and here is the exception class so in our script we are saying use from this namespace the exception class okay now in order for the name spaces to work we have to require the path to those classes next we have to require the config.php file so we have access to the constants that we have created next we are going to write a function named sent mail the function takes three parameters the email variables specifies where the email goes the subject holds the subject that we type in the form and the message variable holds the message that we sent inside the function I will create a new PHP mailer object and I will store it in the mail variable next I will call the is SMTP method so I can use the SMTP protocol to send the email next I will set the SMTP o property to true so we can use our Gmail login details to send the email next I will set the host property to the mail host constant that we have defined in the config file in our next step I will set the username property to the username constant and the password to the password constant next I will set the SMTP secure property to encryption start TLS this will tell PHP mailer to use the start TLS encryption when connecting to the Gmail's SMTP server next I will set the port property to 587 we use this TCP port number to connect with Gmail's SMTP server next we use the set from method and pass in two arguments the sent from and the send from name constant again those two constants are defined in the config file next we use the add address method the add address method specifies where the email goes so we pass in as an argument the functions email parameter next we use the add reply to method which specifies where the recipient can reply to again we use the constants from the config file next we use the HTML method and we pass in as an argument the keyword true this is telling PHP mailer that we will include HTML markup in our message next we're going to assign the incoming subject to the subject property and the message to the body property next we are going to assign the message also in the alt body property some email clients may not support or display HTML content so they will display the message as plain text and last we use the send method in an if statement to send the email if the process is not successful we return an error else we return the string success and that is all the code that we need in the script.php file now let's go to the index file and add PHP code to complete the application at the top of the file we need to require the script. PHP file so that we can have access to the code that we wrote there next I'm going to check if the form is submitted if so I will use an if statement to check if every input field has a value I will use the empty function so I will say if empty post email or empty post subject or empty post message then in a variable named response I will assign the string all fields are required else again I will use the response variable and I will set it equal to the sent mail function as arguments I will pass in the email the subject and the message nice now I have to display whatever the response variable returns in my HTML code so I will go under the submit button open PHP tags and check the response variable if the response variable is equal to success I will tell the user that the email was sent successfully else I will display whatever error has occurred and that's it but before we test the code I will go to the config file and add my six digit app password which I'm not going to show you now we can test out the code let's bring back the browser in the screen and navigate to our index file let's click on the submit button to see if our validation Works nice we see the error now let's send the email I will enter my email address in the subject field I will say hi again George and my message will say this is a new message let's see if it works okay we have the success message now let's go to my Gmail account and here it is and if I click on the reply button we see the reply to name and the reply to address nice our code is working and that's it guys we saw how to send emails in PHP using PHP mailer and Gmail's SMTP server I hope you like the video thanks for watching subscribe if you like see you in the next one
Info
Channel: Digital Fox
Views: 9,554
Rating: undefined out of 5
Keywords:
Id: fSfNTACbplA
Channel Id: undefined
Length: 11min 45sec (705 seconds)
Published: Thu Nov 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.