Example form to email script in PHP
Hypertext Pre-processor (PHP) is an open-source coding language that is typically used during web development. It is easily embedded into HTML code, and is executed on the server side rather than the client side. Before using PHP, you should have a basic working knowledge of HTML, CSS, and JavaScript, as these are common web languages used alongside PHP.
WARNING – PHP scripts are not covered by the Fasthosts support package, and are used at the client’s own risk. You will be liable for any PHP misuse, so ensure they are written correctly and securely with the following Fasthosts article.
While PHP has many uses, we are going to focus on converting HTML feedback forms into plain text formats that can then be sent via email. This guide will use HTML to create a web form, and PHP will be used to communicate with your email server and send the email message. Sadly, HTML alone cannot achieve this. This is because HTML is a client-side language, and would open an email client each time, requiring manual input. We want to automate this process, so PHP is necessary to perform server-side operations.
When we refer to HTML feedback forms, an example would be the Contact Us form on a webpage. This would prompt users to input a name, email, and telephone number. We are creating this Contact Us form, and telling PHP to forward all responses to our chosen email address.
Pre-requisites before you start
Before you start, ensure you have the following:
- Basic knowledge of HTML and PHP
- A PHP-enabled web server, which you can provision using the Fasthosts Web Hosting service package. You can learn how to enable PHP on your web server with this Fasthosts guide.
- A mailbox will be needed to send the feedback form content. Fasthosts offers email hosting services if you do not already have them.
- A text-editing application, such as the Windows Notepad app, or a free third-party alternative like Notepad++. Notepad will be pre-installed on Windows, and can be opened by pressing Ctrl + R on the keyboard, typing Notepad, and pressing Enter.
Creating a PHP script to email web form content
When creating a PHP script, you can merge HTML and PHP code in the same document. This is because the hosting server knows to execute any PHP code alongside HTML code.
To do this, we will create a .php file using our chosen Notepad application. Navigate to the directory in which you want to save the file using File Explorer. Then right click and create a Text Document. This file will be blank. Right-click the file and select Rename, then change .txt to .php. If you get a warning stating The file might become unusable, ignore this and click Yes.
Now, in our example, we have a file named Test.php.
ADVICE – You may need to enable file extensions in File Explorer. To do this, go to View in the top toolbar, then in the Show/Hide section, tick the File Name Extensions box.
Now, open the PHP file with your Notepad application, and copy the following:
<form method="post" action="test.php">
<textarea name="message"></textarea>
<input type="submit">
</form>
This is HTML code, and will post the information to Test.php, which is the file we just created in our example. The textarea part specifies the heading for the message box, and creates an input box for text. Finally, input type creates a submit button to trigger sending of information in the form on the client side.
NOTICE – This is a bare-bones feedback form template, with no additional formatting. Simply a text box, a header for said text box, and a submit button.
Now, we can use some PHP code to send this information via email. Copy the following into your document, located above the code we just used:
<?php
if($_POST["message"]) {
mail("testreciever@example.com", "Form to email message", $_POST["message"], "From: testsender@example.com");
}
?>
Let us dissect this code.
<?php signals the start of the PHP code, and ?> signals the end.
if($_POST["message"]) { means that if the server receives a post request containing information from the message input box of the feedback form, it will execute the next piece of code. Simply put, if post is message, execute the code contained within the curly brackets { }. This means if no message is posted, it will not send to your email, helping you avoid a lot of alert spam to your inbox.
mail is where we specify the email parameters. First, “testreciever@example.com” is the destination email address. The "Form to email message" will be the subject line. Finally, $_POST["message"], "From: testsender@example.com"); tells the server to send message contents from your chosen sender email address.
Expanding our PHP feedback form
We can expand the number of fields on this email form by using the following
<?php
if($_POST["Send message"]) {
$recipient="testreceiver@example.com";
$subject="Test Website Form";
$sender=$_POST["sender"];
$senderEmail=$_POST["senderEmail"];
$message=$_POST["message"];
$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";
mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
$thankYou="<p>You will see this message if the test is successful, on the website and not within the feedback email</p>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Website Form to Email With PHP </title>
</head>
<body>
<?=$thankYou ?>
<form method="post" action="contact.php">
<label>Name:</label>
<input name="sender">
<label>Email address:</label>
<input name="senderEmail">
<label>Message:</label>
<textarea rows="10" cols="30" name="message"></textarea>
<input type="submit" name="Send message">
</form>
</body>
</html>
Let us dissect this code.
If the web server receives a post from the input field named “Send message”, it will confirm the form has been sent.
Then we have variables. The email you are sending to is first, followed by the email subject line.
Located in the form code, under ?><!DOCTYPE html>, we have labels and input names.
$sender=$_POST["sender"]; will use the text inputted in the Sender input as a name in our feedback email.
$senderEmail=$_POST["senderEmail"]; will use the sender email, as specified in the senderEmail input, in the body of our feedback email. $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message"; will dictate the formatting of the email body. It will put the Name first, and use the information linked to the corresponding Label Name, with a page break for each consecutive variable listed. This is because \n denotes a page break in HTML code. meta charset is set to UTF-8, which is one of the most commonly used methods for encoding alphanumeric characters.
The <title> will be the H1 text in the <head> of our webpage, much like a Header in a word document.
We will skip labels and input names, as we discussed this just above.
<textarea rows="5" cols="20" name="message"></textarea> will specify the height and width of the message box, measured in rows and columns. If you do not specify this, the box may be too small for users to type in.
Finally, we have <input type="submit" name="Send message"> which creates a submit button, which we named Send message.
You can now add this .php file to your web server, and access your associated webpage to test this code.
Web Hosting with Fasthosts
Designing and developing your website may be difficult, but Fasthosts has the answer. We have a dedicated Website Builder tool, which allows you to quickly and easily create professional websites. With our SSD-based Web Hosting servers, you get unlimited bandwidth and lightning-fast storage, ensuring your websites are always performant.
Start building your online presence today. Get in touch with our sales teams on 0808 1686 777, or via email at sales@fasthosts.co.uk to get started!