PHP Email form not working

Hey,

I just set up an email form for my dads website. For some reasons no message arrives in the given address. I expect the issue being on the PHP side, but it could be JS as well.

The server supports PHP 5. http://www.taxi-thomas-schulze.de/new/contact.html
I will immediately notice you when someone shows up on my fathers address.

I’ve taken the code from this tutorial: http://www.html-form-guide.com/contact-form/php-email-contact-form.html but since its 3 years old, I don’t know whether it still works.

Thanks for helping!

I assume you have changed the email address to your fathers - is the email address on the same domain. I used to have a website where the security was set so that the emails could not be sent to a different domain.

You are not getting any errors?

I would put this at the top of the contact-form-handler.php page and see what you get:

echo '<pre>';
print_r($_POST);
echo "</pre>';

Yeah you are right, I changed it to an external AOL email address.

I honestly didn’t check for errors. And I don’t know where to do, because after sending the site forwards to another site with something like a “Thank you for submitting” text (that I didn’t style yet).

I guess the errors were in the console of the actual form-site. I have to take the redirection out of the code then right?

Can you make a temporary address on your site with the domain name and test with that to see if it is the problem?

You could put the code I posted on the contact-form-handler.php page as suggested and comment out the redirect line; this should then display the results.

I set up kontakt@taxi-thomas-schulze.de , changed the address of destination in the php file and commented the forwarding code out, added the script you posted within the script tags.

The forwarding did not happen, but after hitting the ‘send’ button, the contact-form-handler.php loads up, but does not show any visible content. Checking the console didn’t show any logs.

The inbox of the address is still empty.
But when I try to reload the contact-form-handler.php, this shows up in the console.

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

As with any code problem, you should post an applicable snippet of the code in question when asking for help.

In this case, post the source of /new/contact-form-handler.php with your private details hidden (email address for eg).

<?php
$errors = '';
$myemail = 'kontakt@taxi-thomas-schulze.de';//<-----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'];
$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: $name";
	$email_body = "You have received a new message. ".
	" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";

	$headers = "From: $myemail\n";
	$headers .= "Reply-To: $email_address";

	mail($to,$email_subject,$email_body,$headers);
	//redirect to the 'thank you' page
	//header('Location: contact-form-thank-you.html');//
}
echo '<pre>';
print_r($_POST);
echo "</pre>';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>

This is the complete contact-form-handler.php file. Thanks for helping!

It is a bit strange you are not getting any output from the code I posted - have you uploaded the “Javascript form validation snippet”?

Are you sure your hosting provider allows the use of the mail() function? Many now require that authenticated SMTP be used instead and block mail() when the to address is not on the same domain as the script.

Damn.

I didn’t upload the javascript. I uploaded the snippet and linked it in the contact.html head now, but it doesn’t work anyway. I additionally linked the general javascript link into the head.

This appears as I load the website

Unfortunately I couldn’t find any information on how the provider deals with the mail function.
This is my provider and his offering: https://www.1blu.de/webhosting/homepagepakete/power/

I just took another look into the console: the javascript didn’t load properly, but there is some more new stuff:

As I tried to resemble the console log, it ended up in the internal server errors again although I didn’t changed anything o.O

Looks like it could be a JavaScript problem after all perhaps @James_Hibbard may be able to shed some light on the problem?

I would remove JavaScript from the equation. It is only needed for form validation anyway.
Instead make a very basic script to test if the mail() function is even working as expected and go from there:

<?php
$to      = 'whoever@whatever.de';
$subject = 'test';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Ref:

http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php

1 Like

Try this modified version. It has a lot more debugging steps included:-

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors','On');
$disabled = explode(',', ini_get('disable_functions'));
if(in_array('mail', $disabled)) die("mail function is disabled on this server");
$debug = true; //change value to false once the form is working
$errors = '';
$myemail = 'kontakt@taxi-thomas-schulze.de';//<-----Put Your email address here.
if(isset($_POST['name']))
    {
        
    if(empty($_POST['name'])  ||
       empty($_POST['email']) ||
       empty($_POST['message']))
    {
        $errors .= "\n Error: all fields are required";
    }
    
    if( empty($errors))
    {
    
        $name = $_POST['name'];
        $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: $name";
            $email_body = "You have received a new message. ".
            " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
        
            $headers = "From: $myemail\n";
            $headers .= "Reply-To: $email_address";
            
            $send = (function_exists('mail')) ? @mail($to,$email_subject,$email_body,$headers) : false;
            if($send) {
                //redirect to the 'thank you' page
                header('Location: contact-form-thank-you.html');
                exit;
            } else {
                if($debug) phpinfo();
                else $errors .= 'Server error: Please report to the webmaster';
                exit;    
            }
        }
    }
} //end isset name
?>
1 Like

Thanks for your helpful answers. I honestly didn’t really understand what to do referring to Pullos post.
But I tried the second posts code and - drum roll - it worked.
The ‘thank you’ site appears, the email shows up in the mail account. I checked it with the private address, this didn’t worked. But 1blu.de offers forwarding, so I just forward the email to the private one of my dad.

But the error message loading the website still appears.
Additionally, I would like to have a little different ‘sent’-reaction. Instead of redirecting, I would like to change the text in the ‘send’ button to ‘sent’. Is this even possible? If so, I will try to do this on my own this time :D.

Thanks for all of you fixing my silly problems!

Paul.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.