Email Form help

EXAMPLE: 'Form to mail' script using PHP

This article shows an example PHP script that takes information from a feedback form and sends it to you as an email message.

Before we start
To prevent spam being sent through our web servers, there are certain conditions that must be met before our SMTP servers will send the email.
Email must be sent to, or from, an existing email address hosted on the Pi Internet platform. This must be a mailbox on the same domain name on which the form to mail script is being hosted.

To stop misuse of your form by third parties the sendmail_from variable should be set to your Pi Internet hosted email address. While access to the php.ini file is restricted on our shared environment, you can sent this variable using the ini_set() command, shown below. A fifth parameter, -f, should be added to the sendmail function. This will set the name of the from email address. In its basic form, a simple sendmail script will look like this:

Provided that your form to mail script meets the requirements above, you should have no problems.

Important: If you don't specify an existing mailbox on your domain as the to or from email address, your script may produce a 554: Recipient address rejected: Relay access denied error.

This example will take the information from a feedback form, send it to you in an email message, then forward the customer to a "thank you for your comments" page. In this example, we will use as the domain name. There is a mailbox hosted with Pi Internet Ltd called
A simple feedback form

First of all, we need to create a feedback form that will receive the data. We will call this form feedback.html. In its most basic form, it can look like this:

Email address:



Not very pretty, but it can be tidied up, and validation can be added at a later date. This form has three fields (email address, name and message) that users can fill in. Once the user clicks the Submit button, it will collect the information contained within the fields, tag the information as "email, name and message", then send the information to sendmail.php.

The send mail script
Now we have a form that sends information to a script, we need to create a script to send the email. In this example, we will name the script sendmail.php as this is the address our form is submitting the data to.

In order to send an email, we need certain information (variables), so let's set them first.

The code above sets the email address you will send your email to and gets the users' name, email address, and message from the previous form. It also sets the variables that contain the email subject, and headers to specify the from and reply to email addresses.

Now lets build the message of the email with the users' name and comments.
$message = "Name: ". $name . "\r\nMessage: " . $message;
Finally, let's send the email. If the email is sent we will go to a thank you page, if there is an error we will display a brief message.
ini_set("sendmail_from", $email_from); $sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from); if ($sent) { header("Location:"); } else { echo "There has been an error sending your comments. Please try later."; } ?>

This script does not have any validation or error checking, so it is not recommended that you copy it directly to your website, however, it does show the basics of sending email from our web servers, and can be used as a framework for your own scripts.



Further help: open a support ticket click here