0

I have PHP email form, I want to sent an email using PHP form, but this is not working.

HTML:-

<form action="mail_handler.php" method="post" name="form" class="form-box">
    <label for="name">Name</label><br>
    <input type="text" name="name" class="inp" placeholder="Enter Your Name" required><br>
    <label for="email">Email ID</label><br>
    <input type="email" name="email" class="inp" placeholder="Enter Your Email" required><br>
    <label for="phone">Phone</label><br>
    <input type="tel" name="phone" class="inp" placeholder="Enter Your Phone" required><br>
    <label for="message">Message</label><br>
    <textarea name="msg" class="msg-box" placeholder="Enter Your Message Here..." required></textarea><br>
    <input type="submit" name="submit" value="Send" class="sub-btn">
</form>

PHP:-

<?php
    if(isset($_POST['submit'])){
        $name=$_POST['name'];
        $email=$_POST['email'];
        $phone=$_POST['phone'];
        $msg=$_POST['msg'];

        $to='123@gmail.com'; // Receiver Email ID, Replace with your email ID
        $subject='Form Submission';
        $message="Name :".$name."Phone :".$phone."Wrote the following :".$msg;
        $headers="From: ".$email;

        if(mail($to, $subject, $message, $headers)){
            echo "<h1>Sent Successfully! Thank you, We will contact you shortly!</h1>";
        }
        else{
            echo "Something went wrong!";
        }
    }
?>

Answer will be appreciated!

Lets-c-codeigniter
  • 682
  • 2
  • 5
  • 18
Rohit Verma
  • 3,657
  • 7
  • 37
  • 75
  • What error you are getting? –  Nov 18 '19 at 05:57
  • May be your smtp server is not setup or something, try to use `error_get_last();` – Abhishek Gurjar Nov 18 '19 at 05:58
  • 2
    Not a PHP player, but Just raising a point that PHP's `mail` function is not that good and gives hard luck in sending emails or I say in receiving. I need to switch `PhpMailer` – Satyam Pathak Nov 18 '19 at 05:59
  • What error you got from the server or localhost. Describe the occurred error some times message more than 70 letters you need to wrap it. because each line should be separated with an LF (\n) and Lines should not exceed 70 characters. use the below code to wrap the message. `$message = wordwrap($message,70);` will wrap the message. – Jithin Vijayan Nov 18 '19 at 06:01
  • I'm getting error 'Something went wrong!' – Rohit Verma Nov 18 '19 at 06:11
  • is this all your code ?? is it local or on server ? – Amado Nov 18 '19 at 08:28
  • its working fine now, ignore this question! – Rohit Verma Dec 02 '19 at 05:48

1 Answers1

1

From PHP manual https://www.php.net/manual/en/function.mail.php.

You are not using headers here. Use the below code

// To send HTML mail, the Content-type header must be set. I think you are missing this
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers . 
$headers[] = 'From:  <'.$email.'>'; 

// Mail it
if(mail($to, $subject, $message, implode("\r\n", $headers))) //Multiple extra headers should be separated with a CRLF (\r\n).
{
   echo "<h1>Sent Successfully! Thank you, We will contact you shortly!</h1>";
}else{
  echo "Something went wrong!";
}
Lets-c-codeigniter
  • 682
  • 2
  • 5
  • 18