Jump to content

Temperamental PHP Contact Form


slaterino

Recommended Posts

Hi,

I designed a contact form a few months ago, did loads of testing, and form worked perfectly fine. Recently, I got some complaints from the client who was using it, saying that it wasn't always sending e-mails to him. I have then tested it again and it seems that sometimes it is sending out e-mails and sometimes not. Nothing at all has changed with this page, so I can't work out what would have changed.

 

Also, when the form is completed successfully the script does three things, it sends out an e-mail, adds an entry to a database and also echoes an e-mail sent statement. The form is doing the last two of these, it's just the sending of mail that is a problem. I have included the script below. Does anyone know what might be causing this or anything I can try to get it working correctly again?

 

<?php

    if(isset($_POST['docontact']))
    {

        $to = "[email protected]";

        $def_subject = "Ringback Service";

        $min_name_len = 2;
	$min_phone_len = 5;
	$min_email_len = 5;

        if(
        isset($_POST['name']) and 
        strlen($_POST['name']) >= $min_name_len and 
        isset($_POST['phone']) and 
        strlen($_POST['phone']) >= $min_phone_len and 
        isset($_POST['email']) and 
        preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_POST['email'])
        )
        {
            $subject = $_POST['name'] . " requires Ringback";
            $message = $_POST['name'] ."\n==================================================\n" .$_POST['phone'] ." | " .$_POST['email'] ." | " .$_POST['comments'];
            $headers = "From: " .$_POST['name'] ." <" .$_POST['email'] .">\r\n";

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

		$rb_name = mysql_real_escape_string($_POST['name']);
		$rb_phone = mysql_real_escape_string($_POST['phone']);
		$rb_email = mysql_real_escape_string($_POST['email']);
		$rb_comments = mysql_real_escape_string($_POST['comments']);

		mysql_query("INSERT INTO ringback (rb_name, rb_phone, rb_email, rb_comments) VALUES ('$rb_name','$rb_phone','$rb_email', '$rb_comments')") or die(mysql_error());

            header("location: ?" .$_SERVER['QUERY_STRING'] ."&sent");

        }
        else
        {
            header("location: ?" .$_SERVER['QUERY_STRING'] ."&fillall");
        }
    }

?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/143108-temperamental-php-contact-form/
Share on other sites

It's probably a mail() function related issue. If you want to diagnose it then add a conditional statement around the mail() function so you can log (the error, the time it happened, the data being sent, and other things related to the mailing process) that way you can see what is actually going wrong. You can also setup a cron script that reads that log to try and resending the mail() at a later time. Mail related scripts like any important process should have fail safe fall back options that keep the data safe until the prescribed action can be fully completed.

You should be putting the email address that was entered in the form into a Reply-to: address and the From: address should be an email address hosted at the mail server where the script is running.

 

FYI: a TRUE status returned by the mail() function only means that there is a sending mail server and it accepted the email to be sent. It does not mean that it was sent.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.