• home
  • forum
  • my
  • kt
  • download
  • Mail Form with PHP and HTML

    Author: 2007-08-11 17:38:17 From:

    Learn how to create a MAIL FORM using PHP and HTML. Before starting you should check that your host has PHP support with the mail() function activated.
    For this you need to create a php file, name it test.php and enter the following code:

    <? mail(" your_email@emailprovider.com This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ","test message","This is a test"); ?>

    Some hosts take a little longer to send the message, thus you should wait a while for the e-mail to arrive in your inbox.
    If you do not recieve the test e-mail you should contact your Hosting Provider, otherwise read further.

    The HTML FORM

    Fireup your favorite text-editor, create a new file and paste in the following HTML code.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>HTML Mail Form</title>
    </head>
    <body>
    <form action="send_mail.php" method="post">
    Name: <input type="text" name="name" size="30" /><br />
    Email: <input type="text" name="email" size="30"/><br />
    Subject: <input type="text" name="subject" size="30"/><br />
    Text:<textarea name="text" name="text" cols="50" rows="10"></textarea><br />
    <input type="submit" name="submit" value="Send" />
    </form>
    </body>
    </html>

    Save it As: contact_us.html
    Now for the code hints.

    In this line you will notice that i introduced send_mail.php that is the name of the PHP file that will process the data sent from the HTML file. The method we use for this form is POST. There are two methods that can be used POST and GET.

    <form action="send_mail.php" method="post">

    In this part of the script we have our general Input Fields were the user introduces it's data (name, email, subject and mail-text). The "name" values that you notice next on every line are required by the PHP script file that is going to process the mail, and should be named carefully.

    Name: <input type="text" name="name" size="30" /><br />
    Email: <input type="text" name="email" size="30"/><br />
    Subject: <input type="text" name="subject" size="30"/><br />
    Text:<textarea name="text" name="text" cols="50" rows="10"></textarea><br />

    This line generates a Button that will submit the form data to the PHP script file.

    <input type="submit" name="submit" value="Send" />

    The PHP script file - processing the data

    Now that we created our HTML form we need to script the PHP file that will process the sent data. Remember the <form action="send_mail.php" method="post">, we now need to create that file.
    Create a new file and insert the following code:

    <?php
    @extract($_POST);
    $name = stripslashes($name);
    $email = stripslashes($email);
    $subject = stripslashes($subject);
    $text = stripslashes($text);
    mail(' youremail@domain.com This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ',$subject,$text,"From: $name <$email>");
    echo("Thank you for your interest, your e-mail was sent.");
    ?>

    Save it As: send_mail.php
    Now for the code hints.

    Remember that we used POST in our HTML form when sending data to the PHP script. Now using the extract function we will recieve the POST data sent and PHP will translate it into single-word variables.

    @extract($_POST);

    In the next lines we use the stripslah function to remove any non-alfanumeric characters from the user inputed data, thus making it user friendly.

    $name = stripslashes($name);
    $email = stripslashes($email);
    $subject = stripslashes($subject);
    $text = stripslashes($text);

    Now by using the mail() function, we are putting it all together. The mail() function collects all the data and sends it to the e-mail address you specify.

    mail(' youremail@domain.com This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ',$subject,$text,"From: $name <$email>");

    After the users hits the Submit button, and the mail is sent, this message will be shown on the page.

    echo("Thank you for your interest, your e-mail was sent.");

    You can also download the php e-mail form files.
    This is just a basic example of the mail() function in php, for more info visit the official php.net website.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot