0

I want to send an email with PHP when a user has finished filling in an HTML form and then emailing information from the form. I have below code but was not getting form values in mail. Can someone help me regarding this?

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
                    <div class="row">
                        <div class="col-sm-5">
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="First Name" name="name">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="Last Name" name="lname">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="Email address" name="email">
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary btn-lg" name="submit">Send Message</button>
                            </div>
                        </div>
                        <div class="col-sm-7">
                            <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
                        </div>
                    </div>
                </form>


**sendemail.php**

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


    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $subject = "Mail from your website"; 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = 'manish22it@gmail.com';

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

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

    echo json_encode($status);
    die;
?>
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • If you are getting the email and not the values, there happens to be something wrong with the `$name`, $email` and other variables you have defined using the @trim function. How about trying to echo those variables on sendmail.php page? How about trying to echo just the `$_POST['name']`, `$_POST['email']` and those variables to see if they are even being detected using POST? – user5104026 Jun 27 '16 at 20:09
  • yes, I tried that too and was not getting the variable value in $_POST['name'], $_POST['email'] etc. But i did not find the reason behind that. Also, I have tried this without @trim function – Manish Gupta Jun 27 '16 at 20:35
  • Remove all attributes from the form tag except `method="post"` and `action="sendemail.php"`. Also, try renaming the file to .php instead of .html. Also try removing `header('Content-type: application/json');`. From the code you posted, it should work. I see no reason behind these, but no bad in trying. – user5104026 Jun 27 '16 at 20:38
  • Also try changing `` to ``. A quick search suggests that ` – user5104026 Jun 27 '16 at 20:43
  • @Soyuz Rocket, It should be `` – Rohit Jun 27 '16 at 20:46
  • 1
    @ManishGupta, I checked your code and got an email with all the values. Couldn't find anything wrong in this code. do you have any extra code other than you have posted? – Rohit Jun 27 '16 at 20:51
  • Well it could even be the values. Just enter plain alphanumeric characters as values. I remember having issues when the user entered a ' character in one of the boxes. Just saying. – user5104026 Jun 27 '16 at 20:57
  • Thanks for your answers and Tried all the above suggestions but unfortunately, it wouldn't help :( – Manish Gupta Jun 27 '16 at 21:28

0 Answers0