0

I have created a form for visitors of my website can message me if they want but for some reason it won't send the message and I do not know how to debug it.

Form:

<form method="post" action="sendmail.php">
                                <fieldset>
                                    <div class="form-group">
                                        <label for="firstname" class="sr-only">First Name</label>
                                        <input type="text" name="firstname" class="form-control" placeholder="First Name" id="firstname">
                                    </div>

                                    <div class="form-group">
                                        <label for="lastname" class="sr-only">Last Name</label>
                                        <input type="text" name="lastname" class="form-control" placeholder="Last Name" id="lastname">
                                    </div>

                                    <div class="form-group">
                                        <label for="email" class="sr-only">Email</label>
                                        <input type="text" name="email" class="form-control" placeholder="Email" id="email">
                                    </div>

                                    <div class="form-group">
                                        <label for="message" class="sr-only">Message</label>
                                        <textarea class="form-control" placeholder="Message" id="message"></textarea>
                                    </div>

                                    <input type="submit" class="btn btn-primary" value="Send Message" >


                                </fieldset>
                            </form>

and the sendmail.php for the info to be sent:

    <?php 
if(isset($_POST['submit'])){
    $to = "email@email.tld"; 
    $from = $_POST['email']; 
    $first_name = $_POST['firstname'];
    $last_name = $_POST['lastname'];
    $message = $firstname . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $firstname . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$message,$headers);
    mail($from,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";

    }
?>
Rahul Nikate
  • 5,716
  • 5
  • 35
  • 50
Jon Stroh
  • 13
  • 2

1 Answers1

1

this is just an example format that you can try

<?php 
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$business_name = $_POST['business_name'];
$comment = $_POST['comment'];

$from = 'From: abc'; 
$to = ''; //email
$subject = 'Customer Inquiry';
$message = "From: $name\n Phone: $phone\n E-Mail: $email\n Business Name: $business_name\n Comment: $comment\n ";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: from@example.com' . "\r\n" .
'Reply-To: reply@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
header( "Location: abc" );//if u wish to get redirected
?>
jane
  • 136
  • 2
  • 12