Hi everyone, could anyone give me an idea of what's wrong with this PHP of a contact form? I get a 500 error when running it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi everyone, could anyone give me an idea of what's wrong with this PHP of a contact form? I get a 500 error when running it.

<?php $name = $_POST['firstname']; $email = $_POST['email']; $jobtype = $_POST['jobtype']; $message = $_POST['message']; $formcontent" From: $name \n Type of Job: $jobtype \n Message: $message"; $recipient = "*********@gmail.com" $subject = "Website Contact Form Message"; $mailheader = "From: $email \r\n"; mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); echo "Thank you!"; ?>

26th Feb 2020, 6:58 AM
Werner Botha
Werner Botha - avatar
5 Answers
+ 4
//Maybe you are trying to send mail from local server , there are other steps to that + you have some error on concat string and variables
26th Feb 2020, 7:10 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 1
It's been a while since I touched PHP but I am excited to see that it's still rather garbage. The mail function does different things on windows and linux; in either case you need to dive into your php.ini file. If you are on windows, you need to find the `SMTP` and `smtp_port` options and set them accordingly. (Mail is sent via the smtp protocol. You can set up an smtp server on your machine, but other mailservers won't trust you and mails will be marked as spam. It's better to use gmail smtp servers or whatever) Also try this, to get better error messages: (Taken from the php.net user comments) <?php $success = mail('example@example.com', 'My Subject', $message); if (!$success) {     $errorMessage = error_get_last()['message']; } ?>
26th Feb 2020, 7:16 AM
Schindlabua
Schindlabua - avatar
0
I'm doing this through a Linux hosting service, Sudarshan Rai 👑 could I ask you to point out the errors?
26th Feb 2020, 7:38 AM
Werner Botha
Werner Botha - avatar
0
On Linux there's a `sendmail_path` in your php.ini that needs to point to a local "mail transfer agent" installation (an MTA, not necessarily sendmail) If you are renting some webspace and not a whole server that should all be already configured for you though as you can't access the php.ini files. Did `error_get_message()` do anything for you? Or do you have a more specific error message somewhere?
26th Feb 2020, 8:22 AM
Schindlabua
Schindlabua - avatar
- 1
Here's the HTML: <div class="container"> <a name="contact"> <form action="action_page.php" method="POST"> <label for="fname">Name</label> <input type="text" id="fname" name="firstname" placeholder="Your name"> <br> <label for="mail">Email</label> <input type="text" id="mail" name="email" placeholder="Your Email"> <br> <label for="jtype">Job Type</label> <input type="text" id="jtype" name="jobtype" placeholder="Electrical, plumbing, roofing, etc."> <br> <label for="message">Message</label> <br> <textarea id="msg" name="message" placeholder="Your message" style="height:200px"> </textarea> <input type="submit" value="Submit"> </form> </a> </div>
26th Feb 2020, 6:59 AM
Werner Botha
Werner Botha - avatar