1

I want to get customers details through contact form and that would be sent to my mail id. so I am trying to code but its not working. I have tried by watching YouTube videos

This is my HTML Code

<!DOCTYPE html>
<html>
<head>
    <title>Form</title>
</head>
<body>
<form action="send.php" method="POST">
Name:   <input type="text" name="name" required=""></input><br><br>
Mobile: <input type="text" required="" name="mobile"></input><br><br>
Message: <textarea rows="10" cols="50" name="mes" required=""></textarea><br><br>
<input type="submit" value="Send Mail"></input>

</form>
</body>
</html>

This is my PHP Code

<?php
$name =$_POST['name'];
$mobile =$_POST['mobile'];
$mes =$_POST['mes'];
mail("imjeevajk@gmail.com","Contact From Site",$mes,"From: $name\r\n","Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";    
?>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Jeeva JK
  • 41
  • 1
  • 7

4 Answers4

0

mail function as described here http://php.net/manual/en/function.mail.php

Example:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

So the header "From" must be an email address while you are putting a custom name. How to get mail function delivery mail to 'to' address please read here

CASH
  • 93
  • 5
0

It is mostly correct.

You can't pass Mobile as the additional parameter, that will do nothing. I have changed the script to append the mobile to the message body.

To set a from address, you need to have a from email, just a name by itself won't work.

Also depending on your php configuration and mail server configuration, you may not be able to change the from address and it will result in an error, or be ignored.

<?php
if ($_POST) {
    $name = $_POST['name'];
    $mobile = $_POST['mobile'];
    $mes = $_POST['mes'];

    // append mobile to message
    $mes .= "\r\nMobile: $mobile\r\n";

    $from_email = "imjeevajk@gmail.com';

    mail("imjeevajk@gmail.com", "Contact From Site", $mes, "From: $name <$from_email>\r\n");

    // mail("imjeevajk@gmail.com", "Contact From Site", $mes, "From: $name\r\n", "Mobile: $mobile\r\n");
    echo "Thanks for Contacting Us";
    exit;
}
bumperbox
  • 10,166
  • 6
  • 43
  • 66
0
Please set submit name field and update the php mail function as mentioned below.
<input type="submit" name="sendMail" value="Send Mail"></input>


    <?php
    if (isset($_POST['sendMail']))  {
    $to = 'admin@dummyemail.com';

    $subject = 'Contact From Site';
    $from = "From: ".$_POST["name"]."\r\n"; 
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: '.$from."\r\n".

        'Reply-To: '.$from."\r\n" .

        'X-Mailer: PHP/' . phpversion();
    $message = '<html><body>';
    $message = "Name: ".$_POST["name"].""; 
    $message .= "Mobile: ".$_POST["mobile"]."";   
    $message .= "Message: ".nl2br($_POST["mes"]).""; 
    $message .= '</body></html>';
    if(mail($to, $subject, $message, $headers)){

        echo 'Thanks for Contacting Us.';

    } else{

        echo 'Unable to send email. Please try again.';

    }
  }
    ?>
0

Better use PHP Mailer some servers providers block mail function and that can be reason why it doesn't work.

Widziks
  • 72
  • 1
  • 9