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.

Requirements

  • 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

Example

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Example Form-To-Email Script</title>
</head>
<body>
<?php

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

//Defaults
$myname = "John Smith"
$myemail = "email@example.com"
$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)) {
    $message.=$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" >
    <fieldset>
    <legend>My Form</legend>
    <dl>
    <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>
    </dl>
    <input type="submit" name="submit" value="Submit"/>
    </fieldset>
    <!-- End fieldset -->
    </form>
    <!-- End form -->'
}
?>
</body>
</html>