Welcome to WebDesignForums.net!
You're currently viewing WDF as a guest. By registering for a free account, you'll be able to participate with other members in our friendly community. Being a member allows you to ask questions and get answers for those troublesome web development tasks!

In addition, as a member you'll be able to post your websites up for review. Using our unique website review system you can gain some amazing feedback from some of the best web developers around. This is a completely free service to all registered members.

Ready to register yet? Registration is 100% free. Click Here To Join Now!

Make an e-mail form using PHP

Discussion in 'Free PHP Scripts & Snippets' started by filburt1, Oct 31, 2002.

  1. Offline

    oracus New Member

    Message Count:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Sorry..The only think that we must do is to put these line in a HTML editor??And thats it?


  2. Offline

    raja New Member

    Message Count:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Help needed from a novice

    Hey iam a novice designer from india aged 19 .one of my friend hosted my site free of cost which supports php mysql cgi n perl.pls any on can help me how to use php mail form plzz. iam a novice plzz. help me iam greatful to u people.Thanks in advance
    raja :surprised:


  3. Offline

    Wired WDF Moderator and Alien Overlord

    Message Count:
    7,597
    Likes Received:
    133
    Trophy Points:
    63
    Can you be more specific?


  4. Offline

    raja New Member

    Message Count:
    5
    Likes Received:
    0
    Trophy Points:
    0
    sure i want to add a form to mail to my web page using php or cgi any one can help me plzzz.
    cheers
    Raja


  5. Offline

    zionlocke Master of the Fu!

    Message Count:
    736
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    .: l33t Basement :.
    I decided to take a whirl at this and also fell in love with tekp's beautiful use of fieldset and legend. Here a little bit better code so the whole thing runs on one page as intended. The redirect page is used to clear the server's $_POST vars.

    I still have one question. How do I keep people from adding a BCC to the mail function. I didn't think it would be possible, but someone was able to send an email with the BCC column filled in. Is there a way to ensure more protection into email forms?

    source of the contact page
    PHP:
    <?php

    function validate_email($str) {
        
    $str strtolower($str);

        if(
    strlen($str) == 0) {
            return 
    0;
        }

        if(
    ereg("^([^[:space:]]+)@(.+).(ad|ae|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|fx|ga|gb|gov|gd|ge|gf|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nato|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$",$str) && strlen($str) <= 150){
            return 
    1;
        } else {
            return 
    0;
        }
    }

    function 
    validate_input($str) {
        if(
    strlen($str) == 0) {
            return 
    0;
        }

        if(
    strlen($str) <= 25) {
            return 
    1;
        } else {
            return 
    0;
        }
    }

    function 
    validate_message($str) {
        if(
    strlen($str) == 0) {
            return 
    0;
        }

        if(
    strlen($str) <= 1200) {
            return 
    1;
        } else {
            return 
    0;
        }
    }

    function 
    not_valid() {
        if (!
    validate_input($_POST['name']) || !validate_email($_POST['from']) || !validate_input($_POST['subject']) || !validate_message($_POST['message'])) {
            return 
    1;
        }
    }


    if (
    validate_input($_POST['name']) && validate_email($_POST['from']) && validate_input($_POST['subject']) && validate_message($_POST['message'])) {

        
    $subject $_POST['subject'];
        
    $from $_POST['from'];
        
    $name $_POST['name'];
        
    $message $_POST['message'];
        
    $message "$message\n\n$name\n";

        if (
    mail('you@email.com'$subject$message"From: $from\r\nReply-To: $from\r\n")) {
            
    ob_clean();
            
    header ("Location: redirect.php");
            exit();
            echo 
    '<b>Your mail was successfully sent.</b>';
        } else {
            echo 
    '<b>There was an error while sending the mail.</b>';
        }

    } else {

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Contact</title>

    <?php
        
    include('header.php');
    ?>

    <form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
    <legend>Compose</legend>
    <p>
    <label for="name">Name: </label> <small>(25 character limit)</small> <br/>
    <input type="text" value="<? if($_POST['name'] != "") echo $_POST['name'?>" id="name" name="name"/>
    </p>
    <p>
    <label for="from">Email: </label> <small>(150 character limit)</small> <br/>
    <input type="text" value="<? if ($_POST['from'] != "") echo $_POST['from'?>" id="from" name="from"/>
    </p>
    <p>
    <label for="subject">Subject:</label> <small>(25 character limit)</small> <br/>
    <input type="text" value="<? if($_POST['subject'] != "") echo $_POST['subject'?>" id="subject" name="subject"/>
    </p>
    <p>
    <label for="message">Message:</label> <small>(1200 character limit)</small> <br/>
    <textarea name="message" id="message" rows="8" cols="30"><? if($_POST['message'] != "") echo $_POST['message'?></textarea>
    </p>
    <p>
    <? if(not_valid()) echo '<small>The composed letter is not yet valid.</small>' ?>
    </p>
    </fieldset>
    <input type="submit" value="   Send   "/> <input type="reset" value="   Clear   "/>
    </form>

    <br/>

    <?php
        
    include('footer.php');
    }
    ?>
    source for redirect.php
    PHP:
    <?php
            ob_clean
    ();
            
    header ("Location: http://www.zioncreation.com/");
        exit();
    ?>


  6. Offline

    eKstreme New Member

    Message Count:
    444
    Likes Received:
    0
    Trophy Points:
    0
    Well a mail BCC header is just "BCC:" followed by the addresses. So just replace BCC: with an empty space and that will do it. You can be crafty and remove everything from the BCC: till the next newline.


  7. Offline

    zionlocke Master of the Fu!

    Message Count:
    736
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    .: l33t Basement :.


  8. Offline

    Karloff Member

    Message Count:
    744
    Likes Received:
    0
    Trophy Points:
    16
    hi,
    trying to use this for a site of mine, can't get it to work. what do i have to change??

    implemented it and added my e-mail but when on the site it give me a
    'error 404: page not found'
    message!?

    any tips please??


  9. Offline

    bfsog Coder

    Message Count:
    2,354
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Could you post your code?

    It sounds like the file you are submitting to (which you specify in the <form action="file">) does not exist.

    Edit: it may be the require/include statements (I forgot which one is used).


  10. Offline

    Karloff Member

    Message Count:
    744
    Likes Received:
    0
    Trophy Points:
    16
  11. Offline

    bfsog Coder

    Message Count:
    2,354
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    You are referencing that page, but it is a .htm file

    Open enquiries.htm and rename as enquiries.php

    As right now your trying to submit to <?php echo... and so on.


  12. Offline

    Karloff Member

    Message Count:
    744
    Likes Received:
    0
    Trophy Points:
    16
    i've done that but no mail is comin through??

    any thought, sorry if its a daft question


  13. Offline

    bfsog Coder

    Message Count:
    2,354
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    So what message do you get?

    Post your answer in the other thread instead of us going off topic.

    If you get the message that indicates success, then maybe it is just a slow one..


  14. Offline

    Karloff Member

    Message Count:
    744
    Likes Received:
    0
    Trophy Points:
    16
    sorry man for the delay, it keep on comin up with the message
    'There was an error while sending the mail.'

    what could be wrong?


  15. Offline

    bfsog Coder

    Message Count:
    2,354
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Paste the line in your file that looks similar to
    PHP:

    if (mail('[b][i]YOUR E-MAIL ADDRESS[/i][/b]'$subject$message"From: $from\r\nReply-To: $from\r\n"))


  16. Offline

    Karloff Member

    Message Count:
    744
    Likes Received:
    0
    Trophy Points:
    16
    cool, but where do i specify the e-mail address to send the info to???


  17. Offline

    bfsog Coder

    Message Count:
    2,354
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Look at my last post, that is where you specify which email address.

    Lets say your joe@bloggs.com, the line of code would look like..

    PHP:

    if (mail('joe@bloggs.com'$subject$message"From: $from\r\nReply-To: $from\r\n"))


  18. Offline

    Karloff Member

    Message Count:
    744
    Likes Received:
    0
    Trophy Points:
    16
    thanks man, in work but will do that when i get home.

    thumbs up! cheers


  19. Offline

    Karloff Member

    Message Count:
    744
    Likes Received:
    0
    Trophy Points:
    16
    sorry, i'm back with another dumb question!

    i implemented this into a site for someone and they have told me that it doesn't send to e-mail address of the sender in the e-mail. thought it did. can anyone tell me why.... not sure if they have messed with the code in some way that i can't see?

    supposidly (spellin!) it only send ip address

    cheers


  20. Offline

    Shadowfiend Code beautifully and honorably

    Message Count:
    4,146
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Atlanta, GA
    (Supposedly) Where are you pulling the `from' address from? If there's no `from' header, I think PHP inserts a default one, likely set to the IP address in your case.


Share This Page