Send email with PHP | Create a Working Contact Form Using PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you'll learn how to send an  email using php we'll develop this contact form   and the php code to process it that  will send this data in an email   all the code developed in this video is free  to download and the link to download it is   in the description let's start by creating the  contact form so let's create a new file called   form.html in here we'll create an html document  starting with the doctype html element head and   body elements inside the head element we'll  add a title and set the character set to utf-8   in the body let's add a heading element  then a form that uses the post method   inside the form we'll add a text input for  the name and an associated label let's also   add the required attributes to this to validate  this as a required field in the browser then an   email type input for the email address again with  the required attribute and an associated label   likewise a text input and label for the  subject and a text area element for the message finally we'll add a line break  element and a submit button let's have a look at that in  the browser and there's the form   this doesn't look great though as we haven't  added any styling to quickly add some styling   without having to make any changes to the html my  preferred technique is to use some classless css   there are many classless css stylesheets  available my current favorite is water.css   to use this we just need to copy this link tag  and paste it in the head element of the html now in the browser the form has some suitable  styling and we can concentrate on processing the   form on the server so let's add another file to do  that called send email.php before we add any code   to this let's add an action attribute to the form  tag so that it submits its data to this new file back in the send email.php script let's add the  php opening tag then we can get the values from   the form as the form is using post these will be  in the post super global to keep this video simple   as we're focusing on sending the email i won't  do any validation of these values on the server   however i do recommend you do this now we can  use this data from the farm to send the email   to send email we'll use an smtp or mail  server just as a web server serves web pages   an smtp server sends emails now just as with  a web server you could install one locally   but this is more hassle than it's  worth especially when there are free   alternatives you can even use gmail's smtp  server for example to send emails if you like   if you do decide to use gmail's smtp server  however you might need to configure your gmail   account to allow script access there's  a link to the instructions for this in   the description once you have access to an smtp  server we can write the code to send the email   there is a mail function in php but this is  difficult to configure with an external smtp mail   server and sending anything other than very basic  emails is overly complex instead we'll install   the php mailer package which makes it much much  easier to install phpmailer we'll use composer on the command line from the same folder  as our code we'll run the command to   install the php mailer package this will  install the package in the vendor folder   to load the packages classes the simplest  way is to require composer's autoloader   note that this is a composer file and  isn't part of the php mailer package   the php mailer classes we're going to use are  in namespaces so first let's import them into   the current namespace so that we can use them  without prefixing them with the full namespace   then we create an instance of the phpmailer  class passing in true as the first argument   this argument will configure phpmailer to throw  an exception if there is a problem next we can   configure it to send the email using smtp  first we need to tell phpmailer we're using   an smtp server by calling the is smtp method then  unless the server doesn't require authentication   we need to enable smtp authentication by  setting the smtp auth property to true   note how this is a property  and the is smtp call above   is a method then we can configure the smtp  server we're using this is where you'll add   the settings for the smtp server you have  access to as described earlier for example   if you're using gmail's smtp server the host  name is smtp.gmail.com the port is 587 and so on we'll start with the host then the type of  encryption the server uses most likely it   will use tls we'll follow this with the port  number which if it uses tls will be port 587   note that this is a number and not a  string then the username and password   note that these values are just examples you need  to put the values in from your own smtp server   then we can send the email we'll set the from  address to the values from the form the email   and the name then we'll set the recipient which  needs to be an email address you have access to   the second argument the name is optional then we  can set the content of the email the subject and   the body of the message getting the values  from the form finally we send the message   by calling the send method for now let's just  print out a message saying the email was sent let's give that a try if i  enter some values into the form   and submit it we get the email sent message  and in the email client there's the message if that didn't work for you then it could be  a problem with the authentication credentials   for example let's see what happens  if the password is incorrect now when i submit the form we get  an exception could not authenticate   note that as this is an exception you  could catch it by wrapping this call   in a try catch block if you like to get  more details about an error such as this   we can enable verbose debugging output in  php mailer by setting the smtp debug property now if we try that again we get detailed  debugging information printed out   specifically the conversation the web server has  had with the smtp server we can see from this   detail that the password command failed indicating  that the password is incorrect this debugging data   will give you more specific information about  why sending an email failed so it's worth   looking at this output if you have problems  once we've identified and fixed the problem   let's try that again and now  we get the email sent message   we still have the debugging output enabled  but this time it doesn't report any errors   so you should only really have  this enabled when you're debugging   as it does print out detailed server information  so let's disable this by commenting it out there's one more thing we need to do once the  email has been sent if i submit the form again   we see the email sent message however if i  reload this page we get a message asking us   to confirm the form resubmission as this page was  generated as the result of a form being submitted   if i reload it the form will be submitted  again and the email will be sent again   to avoid this we use the post redirect get  pattern so instead of this message we'll redirect   to a page that says the email has been sent to  create this page let's copy the form.html page   and call this copy sent.html in here instead  of the form let's just output some text   that says thank you for your message then back in  the php script we'll remove this echo statement   and instead call the header function with  a location header and the sent.html address   now when i submit the form the message is  sent we get redirected to the sent.html page   and if i reload this there's no form  resubmission warning and the email isn't resent   there's a link to all the codes shown in this  video in the description along with links to   sites shown and relevant documentation the  php mailer documentation is comprehensive   and includes many examples for if you need  to do something more than what we've covered   in this video please don't forget to like comment  and subscribe and as always thank you for watching
Info
Channel: Dave Hollingworth
Views: 114,175
Rating: undefined out of 5
Keywords:
Id: fIYyemqKR58
Channel Id: undefined
Length: 10min 18sec (618 seconds)
Published: Fri Aug 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.