-1

I recently made a website using HTML, CSS, and JS. Since I don't know PHP, I am stuck at building the contact form where it is vital on the website. I learned a bit from YouTube tutorials and have the following HTML & PHP code:

<div class="contact_form">
  <form action="/action_page.php">
    <input type="text" id="name" name="name" placeholder="Name*">
    <input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
    <input type="text" id="phone" name="phone" placeholder="Phone No.">
    <input class="contact_even" type="text" id="city" name="city" placeholder="City">
    <textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
    <input type="submit" value="Submit">
  </form>
</div>
<?php
  if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $mailFrom = $_POST['email'];
    $phone = $_POST['phone'];
    $city = $_POST['city'];
    $message = $_POST['message'];

    $mailTo = 'example@something.in';
    $headers = 'From: '.$mailFrom;
    $txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;

    $headers = "MIME-VERSION: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    mail($mailTo, $headers, $txt);
    header("Location: index.html?mailsent");
  }
?>

Why do I need the MIME and content-type headers at the bottom as that bit I added from another tutorial.

When I use the form and try sending the message, I get the "?mailsent" after the URL but I receive no email which is a professional plan by GoDaddy.

They are also hosting my website. I contacted them to know whether the server allows me to make my contact form with the plan I have and they said yes. So, I must be missing something important here.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

You are missing the form action, So PHP doesn't know what to do with your variable data.Try adding method="post" inside <form> tag. Like this

<div class="contact_form">
  <form action="/action_page.php"  method="post">
    <input type="text" id="name" name="name" placeholder="Name*">
    <input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
    <input type="text" id="phone" name="phone" placeholder="Phone No.">
    <input class="contact_even" type="text" id="city" name="city" placeholder="City">
    <textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
    <input type="submit" value="Submit">
  </form>
</div>

And also. If you use your computer as localhost(using xampp , wamp, or something without a hosting service) You have to make some changes to the config files.

Also try this modified php code

<?php
  if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $mailFrom = $_POST['email'];
    $phone = $_POST['phone'];
    $city = $_POST['city'];
    $message = $_POST['message'];
    $title = "replace this";

    $mailTo = 'support@udichi.in';
    $txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;

   $headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL .  'X-Mailer: PHP/' . phpversion();

    mail($mailTo,$title,$txt,$headers);
    header("Location: index.html?mailsent");
  }
?>
Udara Sampath
  • 55
  • 3
  • 16
  • Hi! Thank you for your advice. I tried what you asked me to do and I am getting HTTP ERROR 500 - udichi.in is currently unable to handle this request. But when I remove the headers which contain the Mime-version and content-type, it shows me mail sent. Could you please explain to me what the other two headers are about? Also, how do I solve the HTTP error problem? – Jay Gadekar Jul 19 '20 at 13:41
  • @JayGadekar, Actually I'm not a professional, so I'm not sure, what are they. Anyway you can remove all your headers and use only one header(check the code, I have modified it again.)I know this works because I'm using this currently for 2 projects. I found this working by doing trial and error. – Udara Sampath Jul 19 '20 at 15:50
  • Hey! I contacted GoDaddy and they told me to change some settings and it is working perfectly now! Thanks again. – Jay Gadekar Jul 20 '20 at 08:12
0

refer to the documentation of mail function

there are 3 required parameter : email destination (to), subject and the message, and two additional options are: headers and parameters.

your code didnt respect that, because you missing to add the subjuct as parameter. and you get ?mailsent because you use header("Location: index.html?mailsent") without any test if the email send successfully or not.

i suggest you to replace the last two lines of your php code with this

$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);

if ($result){
  // mail send successfully
   header("Location: index.html?mailsent");
} else {
 // error
}

EDIT: you can get the error message with error_get_last() function. thanks to https://stackoverflow.com/a/20203870/195835

$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);

if ($result){
  // mail send successfully
   header("Location: index.html?mailsent");
} else {
 print_r(error_get_last());
}
GNassro
  • 891
  • 1
  • 10
  • 23
  • 1
    Also, the form doesn't set the `method` to `post`, so `$_POST` will be empty. – Cully Jul 18 '20 at 05:22
  • @Cully yes you right, thats why i add if condition to check if there is an error. – GNassro Jul 18 '20 at 05:28
  • Hi! Thank you for your advice. I added method='post' to the HTML. (Was I supposed to do that?) Then, I am getting HTTP ERROR 500 - udichi.in is currently unable to handle this request. But when I remove the headers which contain the Mime-version and content-type, it shows me mail sent. Could you please explain to me what the other two headers are about? Are they necessary? Also, how do I solve the HTTP error problem? – Jay Gadekar Jul 19 '20 at 13:39
  • You can try the form at the bottom of this webpage: http://udichi.in/ – Jay Gadekar Jul 19 '20 at 13:48
  • @JayGadekar you look like have some code in contactform.php file, the server cant handle it, you should check your server logs – GNassro Jul 19 '20 at 14:34
  • I did that and I found an extra dot which I have now removed and it worked as expected. The URL changed to "index.html?mailsent" but I still did not receive mail. I rechecked the error log and there was nothing new in it. Anything I can do to make it work? – Jay Gadekar Jul 19 '20 at 15:51
  • I also checked the spam folder in the email account. Nothing there either... – Jay Gadekar Jul 19 '20 at 15:53
  • Hey! I contacted GoDaddy and they told me to change some settings and it is working perfectly now! Thanks again. Problem solved! – Jay Gadekar Jul 20 '20 at 08:11
  • @JayGadekar good, can you please mention what change you do in this settings ? – GNassro Jul 20 '20 at 13:14
  • They asked me to go the DNS settings of my domain, into Records and add two items related to secureserver.net. I don't know what exactly that is but I was told that due to a recent change in my domain, this setting had gone away. – Jay Gadekar Jul 20 '20 at 15:18