0

I'm trying to send html form data with php in email but it keeps giving me error everytime. Everytime it fill out the form and click on submit it shows error message defined in php I can't figure out what the problem is because i dont't know much about php. Any help would be greatly appericiated.

here is my form code

<form class=""  name="mpsform" action="contact.php" method="post">
<div class="form-group">
    <label class="col-sm-2">Name:</label>
    <div class="col-sm-10 form-group">
        <input class="form-control" type="text" required="" name="name" placeholder="Name">
    </div>
</div>
<div class="form-group">
    <label class="col-sm-2">Mobile:</label>
    <div class="col-sm-10 form-group">
        <input class="form-control input-bg bfh-phone" id="mobile" name="mobile" data-fv-phone="true"  type="tel" pattern="[0-9]{10,10}" maxlength="10"  autocomplete="off" data-fv-numeric="true" data-fv-phone-country="IN" data-fv-notempty-message="This Field cannot be left blank."   data-fv-numeric-message="Please enter valid phone numbers"  placeholder="Mobile No" required="required">          
    </div>
</div>
<div class="form-group">
    <label class="col-sm-2">Email:</label>
    <div class="col-sm-10 form-group">
        <input class="form-control" type="email" required="" name="email" placeholder="Email">
    </div>
</div>
<div class="form-group">
    <label class="col-sm-2">City:</label>
    <div class="col-sm-10 form-group">
        <input class="form-control" type="text" required="" name="city" placeholder="City">
    </div>
</div>
<div class="form-group">
    <label class="col-sm-2">Course:</label>
    <div class="col-sm-10 form-group">
        <select class="form-control" name="course" >
            <option selected="">Select </option>
            <option>option1</option>
            <option>option2</option>
        </select>
    </div>
</div>
<div class="form-group text-center">
    <button class="btn btn-success" type="submit" name="submit"><b>Contact Me</b></button>
</div>

And here is my php script

<?php
$field_name =  $_POST['name'];
$field_mobile =  $_POST['mobile'];
$field_email =  $_POST['email'];
$field_city = $_POST['city'];
$field_course = $_POST['course'];


$mail_to = 'example@gmail.com';
$subject = 'Message from a site visitor '.$field_name;


$body_message = 'From: '.$field_name."\n";
$body_message .= 'Mobile: '.$field_mobile."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'City: '.$field_city."\n";
$body_message .= 'Course: '.$field_course."\n";


$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
   alert('Thank You . We will contact you shortly');
   window.location = 'index.html';
   </script>
   <?php
}
else { ?>
<script language="javascript" type="text/javascript">
   alert('Message Sending Failed . PLEASE SEND an email to example@gmail.com');
   window.location = 'contact-us.html';
   </script>
   <?php
   }
   ?>
Harsh Barach
  • 916
  • 9
  • 17

1 Answers1

-1

You are sending mails from the user account and any user's email account service doesn't allow mails to be sent without authentication you can use customized Email provided by the domain.

On the other hand you can use phpmailer or swiftmailer lib for sending emails through your google account.

Ankit Jain
  • 738
  • 6
  • 12