Why should I rely on PHPJabbers.com for all my PHP scripting needs? What do you offer me when buying some script?
  • Top quality scripts
  • Full demo before buying
  • Best pricing
  • Script customization
  • 7 days a week support
  • Installation support
  • Secure buying
  • Make contact form and send email in PHP

    posted on 2006-07-03 00:04:12

    NEW Contact Form script NEW

    Having a contact form on your web site is vital when you need to know what your site visitors think about your web site. We will first create a simple contact form with 3 fields - Email address, Name, Comments. I will use a table to align the 3 fields and the Send button. Create a new file and paste the code below in it. Save it as test.php and upload it to your web server. Now, you have a web page (http://www.yourdomain.com/test.php) with a simple contact form on it.

    <form action="test.php" method="post">
    <table width="400" border="0" cellspacing="2" cellpadding="0">
    <tr>
    <td width="29%" class="bodytext">Your name:</td>
    <td width="71%"><input name="name" type="text" id="name" size="32"></td>
    </tr>
    <tr>
    <td class="bodytext">Email address:</td>
    <td><input name="email" type="text" id="email" size="32"></td>
    </tr>
    <tr>
    <td class="bodytext">Comment:</td>
    <td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
    </tr>
    <tr>
    <td class="bodytext">&nbsp;</td>
    <td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
    </tr>
    </table>
    </form>

    Then we will need the actual PHP code which will send the email when the above form is submitted. We will need to define the email that the message should be sent to ($ToEmail) and also the subject for the message that will be sent ($EmailSubject). Change youremail@site.com to your email address where the message should be sent and also add an appropriate subject for you message. The $mailheader variable is used to define the email message header. We set the From, Reply-To and Content-type fields for the message. There are some more fields that can be used but for this example we will only use these 3. Depending on your server configuration you may need to have the From and Reply-to fields be a valid email address from your server.If you have a domain name mysite.com, then you should use a valid email address such as contact@mysite.com. In this example I am sending the email using the actual email address that is submitted via the form on site. Next all the data submitted via the web form is taken from the $_POST variable and saved in the $MESSAGE_BODY variable. Using the nl2br function you will make all the new lines in your comments box appear as new lines in your email message too. Having all the needed data for our email message we will use the mail() function which will send that email for us.

    $ToEmail = 'youremail@site.com';
    $EmailSubject = 'Site contact form ';
    $mailheader = "From: ".$_POST["email"]."\r\n";
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
    $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

    All we have to do now is to combine the web form and email sending code into a single page. We will use an IF statement to check if the form has been submitted and if so it will send that email and will show a "Your message was sent" message on the screen instead of the web form.

    <?
    if ($_POST["email"]<>'') {
    $ToEmail = 'youremail@site.com';
    $EmailSubject = 'Site contact form ';
    $mailheader = "From: ".$_POST["email"]."\r\n";
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
    $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
    ?>
    Your message was sent
    <? } else { ?>
    <form action="test.php" method="post">
    <table width="400" border="0" cellspacing="2" cellpadding="0">
    <tr>
    <td width="29%" class="bodytext">Your name:</td>
    <td width="71%"><input name="name" type="text" id="name" size="32"></td>
    </tr>
    <tr>
    <td class="bodytext">Email address:</td>
    <td><input name="email" type="text" id="email" size="32"></td>
    </tr>
    <tr>
    <td class="bodytext">Comment:</td>
    <td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
    </tr>
    <tr>
    <td class="bodytext">&nbsp;</td>
    <td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
    </tr>
    </table>
    </form>
    <? }; ?>

    You can combine that contact form example with the CAPTCHA verification to prevent spammers from flooding your mail box. NEW Contact Form script NEW

    Comments
    paula posted on 2008-06-30 21:00:35
    How do you combine this code with the captcha code? I can get them to work successfully separate but not combined. Thanks.

    Justin posted on 2008-06-22 21:53:36
    It would be easier to make this a two part script using a Html form to "post" info to a test.php file located elsewhere on the server. So that way there is no need to use code to intergrate the form into a Html website or the "include" feature for PHP..
    Just my thoughts great work though

    Lisa Erez posted on 2008-04-02 17:08:55
    This worked great for me as a test run according to the above instructions. BUT, I now want to combine this with my designed web page and it doesnt work. Where does the code get pasted? It shows in my browser not matter where I try, 'head' or 'body'.

    Em posted on 2008-04-01 05:14:19
    Thanks so much for this, it worked great!

    Sarah posted on 2008-02-12 10:44:31
    hye dudes....plase help me to install the script above...i try to copy and paste it in site but thr is error..the error is: '') { $ToEmail = 'youremail@site.com'; $EmailSubject = 'Site contact form.......until Your message was sent ...thn form table....

    What must i do..... anyway i am using linux server but i check my server admin....he said the host supporting php codes...pls help...i hv no any idea....


    Andrew posted on 2008-01-10 20:13:18
    Where to put $access_ip = (getenv(HTTP_X_FORWARDED_FOR)) ? getenv(HTTP_X_FORWARDED_FOR) : getenv(REMOTE_ADDR);

    Can you give example for capture ip sender for this form?

    Thanks

    abhilash posted on 2008-01-08 07:39:51
    I run this but there is no error but the problem is the mail is not receieved What to do am using
    Ubuntu 7.10

    Test posted on 2007-12-03 09:28:46
    i got a message \" Failure\"

    Jody posted on 2007-11-29 03:07:40
    Works great! No problems. THANKS!!

    ariel posted on 2007-11-23 11:34:38
    cant send messages

    Pages:   1 2 3 4 5
       
    If you have any additions, suggestions or modifications for this example please send an email to or use the form below.
       
    Your name:
    Your email: (email address will not be posted on the web site)
    Comment:
    Verification