PHP 500 Error Contact Form?

Related searches

I updated the code below based on answers

Full disclosure I don't know PHP. I need a contact form and I understood this code below enough to feel comfortable using it. But when I try submitting the contact form I get an error: POST http://(myurlhere)/contact_form.php 500 (Internal Server Error)

I appreciate any insight. Below is the code:

HTML

        <form id="form">
          <p id="returnmessage"></p>
          <input type="text" id="name" placeholder="Name"/>
          <input type="text" id="email" placeholder="Email"/>
          <textarea id="message" placeholder="Your Message Here"></textarea>
       </form>
       <a id="submit">Send</a>

PHP

<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// if (!preg_match("/^[0-9]{10}$/", $contact)) {
// echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" .    $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("myemailaddresshere@gmail.com", $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
}
} else {
echo "<span>* invalid email *</span>";
}
?>

New Code - It sends the message but does not have the name, email or message included in the email like it should.

New HTML

<form id="form" method="post" action="">
 <p id="returnmessage"></p>
 <input type="text" id="name" name="name" placeholder="Name"/>
 <input type="text" id="email" name="email" placeholder="Email"/>
 <textarea id="message" name="message" placeholder="Message"></textarea>
 <button id="submit" id="submit">Send</button>
</form> 

New PHP

<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// if (!preg_match("/^[0-9]{10}$/", $contact)) {
// echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" .    $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("myemailhere@gmail.com", $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
}
?>

It seems there is problem with the code you submitted. Some of the part i noticed there is a problem

form method and submit request to php not set, it has to be like this

<form id="form" method="post" action="php-url">

Submit link is added after ending </form> so this one has to be before form ends

 <button type"submit" id="submit">Send</a>

In your php file double } } added before else statement?

}
} else {
echo "<span>* invalid email *</span>";

The final html would be like

 <form id="form" method="post" action="submit.php">
      <p id="returnmessage"></p>
      <input type="text" id="name" name="name" placeholder="Name"/>
      <input type="email" id="email" name="email" placeholder="Email"/>
      <textarea id="message" name="message" placeholder="Your Message Here"></textarea>
  <button type="submit" id="submit">Send</a>
 </form>

PHP

<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$recipient = "myemailaddresshere@gmail.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" .    $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail($recipient, $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
?>

Check the proof

Contact form 500 Error - PHP, You should check whether $_POST values are set and whether your mail call is correct. According to documentation it should be: mail ( string� Two issues I can think of is that Code 1 is more prone to errors (mistyping, missing closing or opening PHP tags, etc), and even though in this case Code 2 looks cleaner, writing a few hundred lines of HTML inside echo would be pretty messy. Is there any advantage or disadvantage? Thanks. Code 1 (in and out of PHP)

Please try following code.

HTML code

<form id="form" method="post" action="">
   <p id="returnmessage"></p>
   <input type="text" name="name" placeholder="Name"/>
   <input type="text" name="email" placeholder="Email"/>
   <textarea name="message" placeholder="Your Message Here"></textarea>
   <input type="submit" value="Submit"/>
</form>

PHP Code

if(!empty($_POST))
{
  // Fetching Values from URL.
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];

  $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
  // After sanitization Validation is performed
  if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
   //check email validation and show error
  } else {
   //Email sending functionality
  }
}

PHP contact form returning 500 internal server code : PHPhelp, Hi, I have a simple php contact form on my site that its returning a 500 internal server error when submitted, the same form works with the same … (HELP) PHP contact form saying "500 Internal Server Error". Thanks! Close. 0. Posted by u/ Not a fatal, which would generate a 500 if display errors is turned off

I can see a few problems:

1. Change your form to add name attributes with the names that PHP expects.

2. Include a submit button to your form, and make sure it's actually inside the form (your <a>Send</a> is outside your form)

3. Change your form to use the POST instead of GET method because your PHP script is looking for values in $_POST

4. Since all your inputs are required to send a mail, add the required attribute to them

The form below makes all 4 adjustments

<form id="form" method="post">
  <p id="returnmessage"></p>
  <input type="text" id="name" name="name" placeholder="Name" required/>
  <input type="text" id="email" name="email" placeholder="Email" required/>
  <textarea id="message" name="message" placeholder="" required></textarea>
  <button type="submit" name="submit">Send</button>
</form>

You will also have to add an action attribute to <form> if the script that handles the submission is not the same file as the form.

5. On the PHP side, check that input is available before you attempt to use it. Right now your code proceeds with non-existent values if nothing was submitted

<?php
if(isset($_POST['email'],$_POST['name'],$_POST['message'])){
    // proceed with your regular script here

}else{
    // no form values were submitted, so nothing to email
}

6. Find out where your server error log is and review it. There may be more changes required beyond my steps. The details of a 500 error can be found in the error log.

(HELP) PHP contact form saying "500 Internal Server Error". Thanks , (HELP) PHP contact form saying "500 Internal Server Error". Thanks! What am I doing wrong?? PHP BELOW: <?php // Contact subject $subject ="$subject";� The 500 Internal Server Error, which you may be seeing on the web console, may occur because of a random PHP file. 500 Internal Server Error may happen because of faulty.htaccess configuration, PHP memory limit, or a slew of other random issues.

Are you able to send out any webmail from your server? Here is a php script, I call foolproof, it is very minimal and if it fails to send emails, then you may have some sort of web mail restriction. This works on my shared server. I'd also recommend checking your server logs.

<?php
function sanitize_my_email($field) {
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
$to_email = 'your-email@your-domain.com';
$subject = 'PHP Foolproof Send Mail script';
$message = 'This mail is sent using the foolproof PHP mail script';
$headers = 'From: gallienus@roman-empire.com';
//check if the email address is invalid $secure_check
$secure_check = sanitize_my_email($to_email);
if ($secure_check == false) {
echo "Invalid input";
}
else { //send email
mail($to_email, $subject, $message, $headers);
echo "echo: This email is sent using simple foolproof PHP mail";
}
?>

Contact form 7: 500 (Internal Server Error), [This thread is closed.] When I try to send the contact form (contact form 7, latest version) on CHROME (last version, MacOS High Sierra) the form… VERIFY THE IIS HANDLER MAPPING. Make sure that PHP was configured correctly in IIS. Open IIS snap-in and click on the server name.

500 Server Error using PHP contact form, 500 Server Error using PHP contact form. Hi all. We have had a contact form on our site for a few years and recently it stopped working. In the send_contact_mail.php file, I have created PHP code to send an email containing the submitted contact information. Contact Form User Interface. The PHP contact form UI with the Name, Email, Subject and Content fields can be shown to the user by using this HTML script. If you are looking for a responsive contact form UI, check the article

Now the errors will be logged in the debug.log file inside /wp-content/ folder. The wp-config.php file is inside your WordPress root folder. Set PHP to 7.1 and try accessing the WordPress backend. Now, please post the content of wp-content/debug.log file here in the forum or on pastebin.com if it is too big. Related documentation:

Here are some possibilities as to why you are getting 500 errors with php scripts. When a php script requires write access to a file or directory (like Nucleus does for the 'media'-directory if you want to be able to upload pictures etc), you have to chmod that directory (or file) to 777 (or 666 for files) on most servers.

Comments
  • there is no action you mentioned the code. <form id="form" action= "url.php">
  • Hey I tried using this but I've had no luck. I don't get the error anymore now that there's a "submit" button it seems but the message never goes through and I'm not seeing anything in the error logs. I'm completely PHP ignorant.
  • Also yes there is a double }} before the else - is this correct?
  • An update, I no longer have the error but it echoes "invalid email" as set in my code even if the email is valid.
  • Hey this finally sends thanks for that! But the emails received doesn't have the Name Email or Message. The Hello, is empty with no name and the 3 fields: Name: Email: and Message: are also blank in the email so I can't actually see any messages sent. I appreciate your help, this is the most progress I've had these last few days!
  • check the edits, this time it will work for sure. You haven't added name variable in form.
  • Adding the if was crucial thank you and I no longer have the error. However now it echoes the <span> tag of "invalid email" even if a valid email is entered so it does not send the message.