0

EDIT: This is NOT a duplicate of that question. I dont have any problem sending an email OUT using my PHP script as stated in that question. Im having problems getting the data from my HTML form.

Hi guys I am having a problem that I cannot solve and I hope you can help me out.

I am able to send out an email from my HTML form when I use this php code:

 <?php
    $email ="anymail@gmail.com";
    $to ="myemail@gmail.com";
    $subject ="Test Subject";
    $message ="Message";
    $headers ='From:support@mywebsite.com'; 
    mail($to, $subject, $message, $headers);
?> 

But not when I replace the $message with my message from my HTML form as such:

 <?php
    $email ="anymail@gmail.com";
    $to ="myemail@gmail.com";
    $subject ="Test Subject";
    $message =$_POST['InputMessage'];
    $headers ='From:support@mywebsite.com';
    mail($to, $subject, $message, $headers);
?> 

HTML form code:

    <form role="form" action="send_contact.php" method="post" >
        <div class="col-md-6">
            <div class="form-group">
                <label for="InputName">Your Name</label>
                <div class="input-group">
                    <input type="text" class="form-control" name="InputName" id="InputName" placeholder="Enter Name" required>
                    <span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
                </div>
            </div>

            <div class="form-group">
                <label for="InputEmail">Your Email</label>
                <div class="input-group">
                    <input type="email" class="form-control" id="InputEmail" name="InputEmail" placeholder="Enter Email" required  >
                    <span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
                </div>
            </div>

            <div class="form-group">
                <label for="InputMessage">Message</label>
                <div class="input-group">
                    <textarea name="InputMessage" id="InputMessage" class="form-control" rows="5" required></textarea>
                    <span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
                </div>
            </div>

            <input type="submit" name="submit" id="submit" value="Submit" class="btn wow tada btn-embossed btn-primary pull-right">
        </div>
    </form>

Does anyone know why this is happening?

user3702643
  • 1,465
  • 5
  • 21
  • 48

1 Answers1

0

You can use this code if you are using PHP MAILER.Its works.

<?php
require_once "PHPMailer-master/PHPMailerAutoload.php";
$receiver_mail = ''; //Your Email id
$receiver_name = ''; //Your Name
$sender_name = $_POST['InputName'];
$sender_mail = $_POST['InputEmail'];
$message = $_POST['InputMessage'];

$body  = 'Name: '.$sender_name.'<br/>
     Email: '.$sender_mail.'<br/>
     Message: '.$message.'<br/>';
$mail = new PHPMailer;

//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "" ;    
$mail->SMTPAuth   = false;                      
//Provide username   
$mail->Username = "" ;                                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "ssl";                           
//Set TCP port to connect to 
$mail->Port = 465;                                  

$mail->From = $sender_mail;
$mail->FromName = $sender_name;

$mail->addAddress($receiver_mail, $receiver_name);

$mail->isHTML(true);

$mail->Body = $body;

if(!$mail->send()) 
{
   echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
   header("Location of Your site",true,301);
   exit();

}
?>
Sowmiya P
  • 72
  • 6