MikeBrando Posted November 25, 2011 Share Posted November 25, 2011 I have a PHP script for my contact form. It works properly when the form is filled out, but it also sends out blank emails and I am not sure if they are empty forms or forms that have been filled out, but will not show. The blank emails come in as "Results from form:". Any help is great help, thank you! I am sorry if this has been reposted a hundred times, but I looked through this question and each gave a different answer and I do not know PHP. If you know of the correct answer please redirect me to that thread. Thank you! Here is my script: <?php //--------------------------Set these paramaters-------------------------- // Subject of email sent to you. $subject = 'New BrandoArts.com Client'; // Your email address. This is where the form information will be sent. $emailadd = 'mikebrando@comcast.net'; // Where to redirect after form is processed. $url = 'http://www.brandoarts.com/thankyou.html'; // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty. $req = '1'; // --------------------------Do not edit below this line-------------------------- $text = "Results from form:\n\n"; $space = ' '; $line = ' '; foreach ($_POST as $key => $value) { if ($req == '1') { if ($value == '') {echo "$key is empty";die;} } $j = strlen($key); if ($j >= 20) {echo "Name of form element $key cannot be longer than 20 characters";die;} $j = 20 - $j; for ($i = 1; $i <= $j; $i++) {$space .= ' ';} $value = str_replace('\n', "$line", $value); $conc = "{$key}:$space{$value}$line"; $text .= $conc; $space = ' '; } mail($emailadd, $subject, $text, 'From: '.$emailadd.''); echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; ?> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 It's sending the correct email along with a blank email? does it send an email even when a field is not filled out? Where is the form that is populating the relevant $_POST array? If it is the attached php source, please paste the code on this thread. I believe you want die() and not simply die, unless "die" is a custom function. The use of die() is not very user friendly and I would advise against using it in any practical situation, other than debugging. while looking at your code, I do not really understand the purpose of the $key value since you are specifying its value statically directly above your mail() code. Once you have answered these questions, we can further assist you. Quote Link to comment Share on other sites More sharing options...
MikeBrando Posted November 25, 2011 Author Share Posted November 25, 2011 The form and script work fine, but it is sending a blank email at random times. It will not let you submit the form unless all values are filled out. As for the rest of your help, I do not understand the terms die, key, post, etc. I am just a designer and I was given this script. I can browse around to other scripts and see how they match up. Does this script look correct? <?php $ToEmail = 'youremail@site.com'; $EmailSubject = 'Site contact form '; $mailheader = "From: ".$_POST["email"]."\r\n"; $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> Thank you for your help! Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 The form and script work fine, but it is sending a blank email at random times. It will not let you submit the form unless all values are filled out. As for the rest of your help, I do not understand the terms die, key, post, etc. I am just a designer and I was given this script. I can browse around to other scripts and see how they match up. Does this script look correct? <?php $ToEmail = 'youremail@site.com'; $EmailSubject = 'Site contact form '; $mailheader = "From: ".$_POST["email"]."\r\n"; $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> Thank you for your help! yes that should send the correct email, accept wrap your indices in single quotes instead of double quotes, they way you have it now it will break the line prematurely and cause errors. Quote Link to comment Share on other sites More sharing options...
MikeBrando Posted November 26, 2011 Author Share Posted November 26, 2011 Thank you very much for your time and help. Forgive me for my lack of knowledge on the subject, but are the indices everything after the = marks? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 26, 2011 Share Posted November 26, 2011 You are getting blank emails because your form processing code is not testing if a form has been submitted at all and it will send an email every time the page is requested (when there is no submitted $_POST data, for your first posted code, because it at least was testing the submitted data.) This will occur every time a search engine indexes your site or a spam bot script requests your form processing page. At a minimum, you must do the following - <?php if($_SERVER['REQUEST_METHOD'] == "POST"){ // all your form processing code goes in here, including the mail() statement. } ?> You also need to validate ALL the external data before using it. Quote Link to comment Share on other sites More sharing options...
MikeBrando Posted November 26, 2011 Author Share Posted November 26, 2011 I just altered my script to check for the post. Thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.