send fields of form in email with php

Viewed 76

I am trying to get all the fields of a html form and sent them by email when the submit button is pressed. But it doesn't work and I don't know why.

When I test the send button, nothing display in action.php page I don't know why. I made method="post" and action=action.php in form tag.

HTML FORM

<form action="action.php" method="POST">
   <div class="p pb-3"><strong>Feel free to contact me </strong></div>
     <div class="row mb-3">
       <div class="col">
        <div class="input-group"><span class="input-group-addon"><i class="fa fa-user-circle"></i></span>
        <input class="form-control" type="text" name="name" placeholder="Name" required="required"/>
         </div>
           </div>
            </div>
             <div class="row mb-3">
               <div class="col">
          <div class="input-group"><span class="input-group-addon"><i class="fa fa-file-text"></i></span>
     <input class="form-control" type="text" name="subject" placeholder="Subject" required="required"/>
          </div>
           </div>
            </div>
             <div class="row mb-3">
              <div class="col">
      <div class="input-group"><span class="input-group-addon"><i class="fa fa-envelope"></i></span>
      <input class="form-control" type="email" name="mail" placeholder="E-mail" required="required"/>
        </div>
         </div>
           </div>
             <div class="row mb-3">
               <div class="col">
                <div class="form-group">
 <textarea class="form-control" name="message" placeholder="Your Message" required="required"></textarea>
                 </div>
                  </div>
                    </div>
                      <div class="row">
                       <div class="col">
               <button class="btn btn-primary" type="submit" name="submit" value="Send">Send</button>
                        </div>
                      </div>
                    </form>

```.
PHP CODE
``` code
<?php
echo 'bonjour'
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $email=$_POST['email'];
    $subjectF=$_POST['subject'];
    $msg=$_POST['message'];

    $to='boussidan.raphael@gmail.com';
    $subject='Form Submission';
    $message="Name :".$name."\n"."Subject :".$subjectF."\n"."Wrote the following :"."\n\n".$msg;
    $headers="From: ".$email;

    if(mail($to, $subjectF, $message, $headers)) {
        echo "<h1>Sent Successfully! Thank you"." ".$name.", I will contact you shortly!</h1>";
        echo "Message :"." ".$message. , .$subjectF., .$email.
    }
    else{
        echo "<h1>Something went wrong!</h1>";
    }
}
?> 
1 Answers

If you are making website not on local server, php mail will be probably turned off, also try https://github.com/PHPMailer/PHPMailer it is if mail does not work, cuz from your question I did not understand what is not working. Another thing if you are doing your php code into another file, not in the file where your html, you don't have to put

if(isset($_POST['submit']))

because if you are doing it in another file it means you are already pressed button to make action in that file

Related