Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you create a form to capture online data, the information you get usually has one of two destinations. The data gets sent to some email address where a human takes care of the request, or it's stored into a database. In this video, we'll look at what it takes to send an email with the result of your form. PHP has a built-in mail function which is perfect for sending the data. Here's a documentation page for mail in the php website. The mail function takes a series of strings with where to send the data, who to send it to, a subject, a message, and additional headers and parameters. PHP sends an email using the server's mail software. In Linux, this is usually called send mail.
If the server is able to send the email message, then the function returns true, otherwise, it returns false. The data, as well as additional headers, adheres to certain formats as dictated by the mail protocol. Additional headers can include traditional mail features like the from address, who to replyto, cc, etcetera. This type of mail command is probably one of the most attacked scripts on the internet, so make sure you take extra precaution validating and sanitizing your form input before you use this command.
This also means that your hosting company might be placing a limit on how many emails you're allowed to send through this command. Your PHP configuration files might also be locked down, or need some adjustments as well. So make sure you talk to your host if things aren't working smoothly. If you want more information about how to set up and debug your PHP, make sure you check out PHP Advanced Techniques or PHP and MySQL Essential Training. So, let's implement this into our form. We've been placing the PHP in the same file as our form. It let's us grab PHP variables and insert them into the form, but it's making our page a little long. So I'm going to move things into our process PHP file. So I'll grab this code right here and I'll cut it out of this page. And then I'll move it into the separate document.
So one thing you don't want to do is mail anything unless our form is error-free. So I'm going to create a form error variable and set that to false. I'll place that right here. Now, if any of our validation or sanitizing rules generates an error, we'll set this variable to true. Next, we'll create an if statement that verifies that there are no errors and place our mail function inside that. So, the next step is to prepare the information you want to send. We'll need an email to send to, a subject, and a message. If you want to, you can also pass along some other information, like a from and reply to address.
I'm going to create a series of variables for this. When I'm doing this subject I'm going to use the my name variable here to print out the users name when I get an email from this page. I usually like to put a title for the page that this form is coming from, otherwise, I might get a bunch of emails without knowing which page they come from. The message can really be just about anything you want, including any of the variables from the form. For right now, we'll just put in a simple text message. Now, we'll also add some additional header information into the message. Now usually in here if you've asked for an email address, you can add that person's email address, usually in a variable called like email, to the message.
That way the person who receives emails from this form can just hit the reply button on their email program. And here I'll just identify that this is from my form processor. This is not a real email address. Now, some headers have to be separated by slash r and slash n's. In reply to, you may want to put an email so that if the person reading the mail from the form decides to reply to the user when the user receives that message, they're able to hit their reply and see the originator's email address.
Now we can issue the mail command. I'm doing an IF statement because the mail returns a boolean value. So if your server is successful in emailing the form, it's going to return true, otherwise, it'll return false. And then here, I'm just passing the variables over to the mail command. Now, in my form, I want to output a message that will say, thank you for filling out our form, if the form is successful. So I'll create this MSG variable. If the mail program is not successful, I'll send a different message. So as I mentioned before, PHP doesn't actually mail anything. Mailing is handled by your server's send mail command. So if there's any errors all you're going to get is this message saying that there was a problem.
If you want to look for actually errors you have to do it in your server's logs. Now we need to add something on the index.php page that outputs a message variable in the page if our form email is successful. So let me go back to my index.php document in here right after the body tag. I'm going to add a message. So if the message variable is set, then we're going to print out a message to the user. So I'm going to save this and I'll refresh the page. And I'll fill out my form really quick. Just a couple of, fields here, just the ones that I need. And I'm going to hit the send button.
And it says, thanks for filling out our form, so this message is printing out, which means that our email should have been successful. If I check my email application, you can see the message from the user. And if you open that up, you can see that the message has been sent and it has the user's name, plus the little message that I typed in the PHP file. So, let me go ahead and add the rest of the data to the email and usually you end up printing out all the variables just like you did here, but I want to do something different. This might make it easier for you to access the data in the future later on. I'm going to add the data to the form and usually you end up printing out variables along with some text, but I'm going to do something a little bit different. I'm going to create an associative array out of the form elements and then use the function to convert them to the JSON format.
Now I don't want to type all of that in, so I've created a closed image file. And I'm going to grab this associated array and switch back into my main program. And right here I'm going to paste this and this is just a regular PHP associated array. You create an array, and you create a key for each variable, and then you use this equal and greater than sign and then you put a name of a variable. You separate them with commas, this is a little bit like the JSON format. And then, all I have to do is change the message variable to encode our array as a JSON file. So, in message, I'm going to replace this with the PHP method JSON encode and then pass it along the variable of our array.
now I'm going to save that, I'll go ahead and reload the page. I'll type something in again, and I'll hit the send button. We get a thank you notice and let's go check our email. So, you can see I got the new email and if I click on this new one you'll see that I have a JSON object that I received as an email message. So you can see that emailing stuff with PHP is pretty easy. It's just a function that accepts some inputs. Do remember that you may have some problems depending on how your host is set up. So, if things aren't working just right, make sure you check with your host provider.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101182 Viewers
61 Video lessons · 87935 Viewers
71 Video lessons · 71827 Viewers
56 Video lessons · 103675 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.