1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

No content in email from form

Discussion in 'PHP' started by LikeISayBeresford, Jul 15, 2010.

  1. #1
    I've been trying to collect data from a form then sending it to an email address using a simple php file (currently the following)

    <?php
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail( "someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    ?>

    and html page with a simple form in

    <form action="comments.php" method="post">
    etc
    </form>

    however all I get is a blank email! It must be something blatently obvious, I've tried all sorts of combinations. It seems like the php file is reading the blank boxes before any input has been entered.

    Go easy on me, I'm only a newbie.
     
    LikeISayBeresford, Jul 15, 2010 IP
  2. moon_gfx77

    moon_gfx77 Active Member

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #2
    maybe your form is incorrect
     
    moon_gfx77, Jul 15, 2010 IP
  3. LikeISayBeresford

    LikeISayBeresford Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Using this form:

    <form action="comments.php" method="post">
    <div class="cont10">
    <p>email address</p>
    </div>
    <div class="cont11">
    <input name="email" size="45" maxlength="50" type="text" />
    </div>
    <div class="cont12">
    <p>subject</p>
    </div>
    <div class="cont13">
    <input name="subject" size="45" maxlength="100" type="text" />
    </div>
    <div class="cont14">
    <p>comments</p>
    </div
    <div class="cont15">
    <textarea name="message" rows="5" cols="43" style="height: 100%;"></textarea>
    </div>
    <div class="cont16">
    <input value="Submit" type="submit">
    </div>
    <div class="cont17">
    <input value="Reset" type="reset">
    </div>
    </form>

    Divs are just to position boxes.

    Hope you can see the problem here, it's doing my nut in!
     
    LikeISayBeresford, Jul 15, 2010 IP
  4. dbsuk

    dbsuk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I just run your script on my test server and it worked perfectly.

    Have you tried to output the content of the $_REQUEST['message'];

    if not try it right at the top of the comments.php page and then submit your form again and see if anything shows up?

    <? die($_REQUEST['message']); ?>
    PHP:
    Do the Subject and Email appear ok on the email or is it completely blank?

    Have you tried replacing $_REQUEST with $_POST ?

    :confused:
     
    Last edited: Jul 15, 2010
    dbsuk, Jul 15, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    I went the extra mile (added some security and cleaned/improved your code - to make it safer and more reliable):

    <?php
    if (!function_exists('mail')) {
        die('the mail() function is disabled');
    }
    
    function strip_spam($content)
    {
        $content = trim($content);
        $bad_stuff = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:', 'mulitpart-mixed:', 'content-transfer-encoding:');
        foreach($bad_stuff as $bad) {
            $content = str_ireplace($bad, '', $content);
        }
        return str_replace(array("\r","\n"), '', $content);
    }
    
    $your_email = "someone@example.com";
    
    if (isset($_POST['submit'])) {
        $_POST = array_map('strip_spam', $_POST);
        
        if (!preg_match('~^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})$~i', $_POST['email'])) {
            echo "ERROR: Email is invalid!";
        } else if (!strlen($_POST['subject']) > 0) {
            echo "ERROR: Subject is empty, please go-back!";
        } else if (!strlen($_POST['message']) > 0) {
            echo "ERROR: Message is empty, please go-back!";
        } else {
            
            $email = $_POST['email'];
            $subject = $_POST['subject'];
            $message = $_POST['message'];
            
            $boundary = uniqid('np');
            
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "From: {$email} <{$email}>\r\n";
            $headers .= "Subject: {$subject}\r\n";
            $headers .= "Content-Type: multipart/alternative;boundary={$boundary}\r\n";
            
            if (mail($your_email, $subject,
            $message, $headers)) {
                echo "SUCCESS: Sent!";
            } else {
                echo "ERROR: Unable to send!";
            }
            
        }
        
    } else {
    ?>
        <form method="post">
        <div class="cont10">
        <p>email address</p>
        </div>
        <div class="cont11">
        <input name="email" size="45" maxlength="50" type="text" />
        </div>
        <div class="cont12">
        <p>subject</p>
        </div>
        <div class="cont13">
        <input name="subject" size="45" maxlength="100" type="text" />
        </div>
        <div class="cont14">
        <p>comments</p>
        </div
        <div class="cont15">
        <textarea name="message" rows="5" cols="43" style="height: 100%;
    "></textarea>
        </div>
        <div class="cont16">
        <input value="Submit" name="submit" type="submit">
        </div>
        <div class="cont17">
        <input value="Reset" type="reset">
        </div>
        </form>
        
    <?php
    }
    ?>
    
    PHP:
    //untested
     
    danx10, Jul 15, 2010 IP
  6. LikeISayBeresford

    LikeISayBeresford Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I just get an empty page when I do that dbsuk. When I try your code danx10 the details I fill out on the html page are forgotten, a new plain page with only the form displays (as in your code). When I fill in the details again on this new page an error message displays:

    Fatal error: Call to undefined function: str_ireplace().

    I've tried a basic version I found on w3 schools which followed the process of ignoring the details you fill in on the original html page, it then correctly sending an email with details from the second. I don't understand why it doesn't collect the details first time round.

    Could it be something to do with the server? This is such a basic thing I feel like a right idiot!
     
    LikeISayBeresford, Jul 16, 2010 IP
  7. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I tried your code yesterday on three different servers (FreeBSD, Windows and Slackware) and it worked on all three. It's not a problem you've made.

    What client do you read the e-mail with? and can you view it in original form? (you can do this in gmail, so you can see the headers)
    Also, if you're actually paying for this server and it doesn't have str_ireplace. Switch providers.

    Have you changed the headers much? The sendmail daemon might be a bit uptight about em.
    
    <?php
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: test@example.co.uk' . "\r\n" .
    ?>
    
    PHP:
    Also, check out the ini setting sendmail_path.
     
    Deacalion, Jul 16, 2010 IP