The following is an example of a basic form-to-email script using PHP.

Legal Stuff

Before we get started, please read Ground Rules for sending e-mail from your Web Pages.


  • Web space with PHP support.
  • A valid email address to send the emails from. This must an active account hosted with Ecenica Hosting.

If you’re unsure if you meet the requirements, or wish to upgrade please contact us


Copy & paste the code below into a page called myform.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<title>Example Form-To-Email Script</title>

// If you're hosting this script on your Windows Hosting you may need
// to uncomment the line below.
//ini_set("sendmail_from",  $myemail);

$myname = "John Smith"
$myemail = ""
$subject = "Example Form-To-Email Script using PHP"

if (isset($_POST['submit'])) {
    //send email
    $email = $_POST['email'] ;
    $name = $_POST['name'] ;
    $commments = $_POST['comments'] ;

    //Build message
    if (!empty($name)) {
    $message="Name: ".$name."\n\n"
    if (!empty($email)) {
    $message.="Email: ".$email."\n\n"
    if (!empty($comments)) {

    mail( $myemail, "Subject: $subject",$message, "From: $myemail" );
    echo "Your message was sent successfully!"
} else {
    //if "email" is not filled out, display the form
    echo '<form method="post">
    <input name="redirect" type="hidden" value="myform.php" >
    <legend>My Form</legend>
    <dt><label for="name">Name</label></dt>
    <dd><input type="text" name="name"/></dd>
    <dt><label for="email">E-mail</label></dt>
    <dd><input type="text" name="email"/></dd>
    <dt><label for="comments">Comments</label></dt>
    <dd><textarea name="commments"></textarea></dd>
    <input type="submit" name="submit" value="Submit"/>
    <!-- End fieldset -->
    <!-- End form -->'