Search code examples
phphtmlwebcontact-form

PHP post data isn't being sent through


I have tried everything I can think to try, but this is still not working.

I've used different forms and PHP mail scripts but nothing seems to work. The email is being sent but the info is always blank. See screenshot for example.

This is the screenshot link

Here is the form code:

    <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
                  <div class="row-fluid">
                    <div class="span5">
                        First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
                    </div>
                    <div class="span7">
                        <button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
                    </div>

                </div>
                

            </form>

and here is the entire PHP file (sendemail.php):

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Email sent!'
    );


    $name = @trim(stripslashes($_POST['first_name']));
    $lastname = @trim(stripslashes($_POST['last_name']));  
    $email = @trim(stripslashes($_POST['email'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = "Website Contact Form";
    $email_to = '//removed real email address';


$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;

 
    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    $success = mail($EmailTo, $Subject, $Body, "From: <$email_from>");

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');


    echo json_encode($status);
    die;

The web page of the form with the current code you see here running here.


Solution

  • First line of $Body should start without "."

    $Body = "Name: ";
    

    If you defined variable with lowercase you cant call it with upper.

    $test != $Test
    

    Just copy this and replace whit your code:

        $name = @trim(stripslashes($_POST['first_name']));
        $lastname = @trim(stripslashes($_POST['last_name']));  
        $email = @trim(stripslashes($_POST['email'])); 
        $message = @trim(stripslashes($_POST['message']));
    
    $subject = 'Email Subject';
    $headMail = "From: [email protected]\r\n";
    $headMail .= "From: [email protected]\r\n";
    $bodyMail = "Name: ";
    $bodyMail .= $name . ' ' . $lastname;
    $bodyMail .= "\n";
    $bodyMail .= "Email: ";
    $bodyMail .= $email;
    $bodyMail  .= "\n";
    $bodyMail .= "Message: ";
    $bodyMail .= $message;
    
    mail($email, $subject, $bodyMail, $headMail);