0

I have used this php coding for a contact form I have recently used for one of our clients contact page, and when I testing the page it leaves this error by clicking the submit button: 500 Internal Server Error was encountered while trying to use an the below page to handle the request.

HTML

<form name="contactform" action="contact-form-handler.php" method="post">
  <div class="cnt_field cnt_field1">
    <label for='name'>First Name:</label>
    <input name="name" type="text" class="cnt_input">
</div>
<div class="cnt_field cnt_field1 cnt_field_ml">
    <label>Last Name:</label>
    <input name="lastname" type="text" class="cnt_input" >
</div>
<div class="cnt_field">
    <label for='email'>Email Address:</label> 
    <input name="email" type="text" class="cnt_input">
</div>
<div class="cnt_field">
    <label for='message'>Message:</label>
    <textarea name="message" cols="" rows="" class="cnt_msg"></textarea>
</div>
<input name="submit" type="submit" class="send_btn" value="Send">
</form>

PHP

<?php 
    $errors = '';
    $myemail = 'shajjan@shajjanlaw.com';//<-----Put Your email address here.
    if(empty($_POST['name'])  || 
     empty($_POST['email']) || 
     empty($_POST['message']))
    {
        $errors .= "\n Error: all fields are required";
    }
    
    $name = $_POST['name']; 
    $lastname =$_POST['lastname'];
    $email_address = $_POST['email']; 
    $message = $_POST['message']; 
    
    if (!preg_match(
        "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
        $email_address))
    {
        $errors .= "\n Error: Invalid email address";
    }
    
    if( empty($errors))
    {
        $to = $myemail; 
        $email_subject = "Contact Form Submission | Shajjan & Associates";
        $email_body = " A new message has been recieved from your website's contact page.\n".
        "\n From: $email_address \n Name: $name \n Last Name: $lastname \n \n Message: \n $message"; 
        
        $headers = "From: $myemail\n"; 
        $headers .= "Reply-To: $email_address\n";
        
        mail($to,$email_subject,$email_body,$headers);
        //redirect to the 'thank you' page
        header('Location: contact-form-thank-you.html');
    } 
    ?>
    <!DOCTYPE HTML> 
    <html>
    <head>
        <title>Contact form handler</title>
    </head>
    
    <body>
        <!-- This page is displayed only if there is some error -->
        <?php
        echo nl2br($errors);
        ?>


    </body>
    </html>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Motaal
  • 27
  • 2
  • remove .htaccess and then try. use this code at the top of your php page ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); and try print_r($_POST); die; – Ravi Sharma Nov 14 '17 at 09:09
  • It doesn't changed any thing and still shows the 500 internal server error. – Motaal Nov 14 '17 at 09:41
  • ok dont worry just put this code on your php page and comment all your code one more thing make your html page form action=" " and then submit and tell me what you get. – Ravi Sharma Nov 14 '17 at 10:04
  • Could you try commenting the "mail($to,$email_subject,$email_body,$headers);" and " echo nl2br($errors);" lines to see whether the page can load properly? – Nax.S Nov 14 '17 at 10:06
  • 1
    Please share more details, like the full and exact error message you are facing. If your browser shows an error 500, your server usually wrote a more verbose error message to its log files – Nico Haase Aug 14 '23 at 08:02

0 Answers0