PHP Tutorial: Part 6 – Forwarding Forms in Emails Using PHP

October 4, 2021 / Web Hosting

In part six of our series of PHP tutorials, we will explain how to use PHP to collect data submitted in a website form and forward it by email to a website admin.

Setting up a form

Setting up a form so that its information can be used by a PHP script is exactly the same as creating any form in HTML. For this example, let’s imagine we are creating a simple form to send a message to a website. Such a form will need four elements:

1. A text input box for the sender’s name. This will have the sample text ‘Your Name’ displayed in it by default. The name of the text box needs to be unique, so choose something like ‘username’.

2. A text input box for the sender’s email address. This will have the sample text ‘Your email address’ displayed in it by default. Again, the name of the text box needs to be unique, so choose something like ‘email’.

3. A large scrolling text box where the user can type their message. This will have the default text, ‘Please write your message here.’ And should also have a unique name, such as ‘message.’

4. Finally, you will need a submit button so that the user can submit the form once the message has been completed. The wording on the button can be created by changing the button’s value.

Form elements and formatting

To use PHP with the form so that its contents can be forwarded in an email, you need to set the form’s action so that it knows how to submit its data to the PHP script. For this example, we are going to use the process.php script, so when setting the action, you can do this by typing the PHP script’s name or its full URL, e.g., http://www.mysite.com/scripts/private/processors/process.php

There are two ways to submit the data entered in the form. These are POST and GET. When you used the POST method, (e.g., <form method=”POST” action=”process.php”>) the data is sent to the script when it is requested. When you use the GET method, (e.g., <form method=”GET” action=” http://www.mysite.com/scripts/private/processors/process.php “>) GET will send the form data in the contents of a URL, with the submitted information appearing after a question mark at the end of the URL. For example, if the username field was filled with the name David, the URL would appear as:

http://www.mysite.com/process.php?username=david

Collecting the form information

The next step is to create the PHP script (process.php) to collect the data contained in the form. How this is achieved depends upon whether you have used the POST or GET methods of submission.

From a PHP perspective, each field in the form (i.e., username, email and message) are considered as different variables. To get the data from a variable to the PHP script, using the POST method, you would use the following code:

$variablename=$_POST[‘username’];

This takes the variable (i.e., the name of the form field) from the POST and assigns it to $variablename.

Alternatively, when using the GET method, simply replace POST with GET in the code, for example:

$variablename=$_GET[‘username’];

This should be done for each variable from your form. So, for our example, we’d also need to use $variablename=$_POST[‘email]; and $variablename=$_POST[‘message’];

Once the script is finished, save it as process.php and upload it to your server.

Email the form to a website admin

After you have created your form, you now need to create the PHP script. For our example the script is as follows:

$username=$_POST[‘username’];

checkOK($username);

$email=$_POST[’email’];

checkOK($email);

$message=$_POST[‘message’];

checkOK($message);

$to=”[email protected]”;

$message=”$username just filled in your comments form. They said:\n$message\n\nTheir e-mail address was: $email”;

if(mail($to,”Comments From Your Site”,$message,”From: $email\n”)) {

echo “Thanks for your comments.”;

} else {

echo “There was a problem sending the mail. Please check that you filled in the form correctly.”;

}

?>

Remember to replace [email protected] with your own e-mail address. This script should be saved as mail.php and should be uploaded to your server. Now, all you need to do is to fill in your comments form.

Note: the lines:

checkOK($username); etc.,

are a check to ensure each form field has a valid entry.

Conclusion

Hopefully, from reading this tutorial, you’ll know how you can set up a website form to work with a PHP script and use the script to send the contents of the form in an email to yourself or another website admin.

For more information about our hosting solutions for PHP based platforms, like WordPress, visit our homepage.

Spread the love