-3

I made a form and my task is to sending mail to the mail server.I tried with mailto HTML attributes.But failed.Then start with php.It was not working also.So I badly need help.

<form method="post" action="form-to-email.html">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="exampleInputName">Your Name (required)</label>
                                        <input type="text" class="form-control"  name="name" placeholder="Enter Your Name">

                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">Your Email (required)</label>
                                        <input type="Email" class="form-control" name="email" placeholder="Enter Your Email">

                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputTextarea">Your Message</label>
                                        <textarea class="form-control" placeholder="Enter Your Message" rows="4" name="message"></textarea>
                                    </div>

                                    <button type="submit" value="Send Email" class="btn btn-default">Send</button>

                                    </div>
                                </form>

And this is my php code.

<?php
if(isset($_POST['name'])&&isset($_POST['email'])){
$name = $_POST['name'];
  $visitor_email = $_POST['email'];
  $message = $_POST['message'];
    $email_from = 'dbgreen71@heidianderson.com.au';
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n".    
                            "Here is the message:\n $message".    
$to = "dbgreen71@heidianderson.com.au";
  $headers = "From: $email_from \r\n";
  $headers .= "Reply-To: $visitor_email \r\n";
  mail($to,$email_subject,$email_body,$headers);
}
?>

Someone please help me.I really don't find any way to do this task.

Priom Sarkar
  • 342
  • 3
  • 15

1 Answers1

0

Try this and tell if you get any error:

<?php
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    $email_subject = "New Form submission";
    $to = "dbgreen71@heidianderson.com.au";
    $subject = $email_subject;
    $email_from = 'dbgreen71@heidianderson.com.au';
    $header = "From:dbgreen71@heidianderson.com.au \r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";

    $retval = mail ($to,$subject,$message,$header);

    if( $retval == true ) {
    echo "Message sent successfully...";
    }else {
    echo "Message could not be sent...";
    $errorMessage = error_get_last()['message'];
    echo $errorMessage;
    }
?>

Also I have added error handling code which will help you in near future if you get any errors related to mail. Another thing, if you need headers then and then only use it, or else it is of no usage. You can even send emails without headers.

Bits Please
  • 877
  • 6
  • 23