March 27, 201016 yr I'm no php boff (complete the opposite! total noob!) I have a contact form on one of my sites, that just wont work. Can someone pls check the code to see if there are any errors anywhere.... thanks <form method="post" action="contact.php"> <fieldset> <legend>How can we help you?</legend> <div id="formconts"> <p class="formp"><label>Email:</label> <input type="text" name="EmailFrom"/> </p> <p><label>Name:</label> <input type="text" name="Name"/></p> <p><label>Company name:</label> <input type="text" name="Companyname"/></p> <p><label>Telephone:</label> <input type="text" name="Telephonenumber"/></p> <p><label>Message:</label> <textarea name="Yourmessage" cols="23" rows="5"></textarea></p> </div> </fieldset> <div class="buttons"> <input type="submit" name="submit" value="Send form"/> <input type="reset" name="reset" value="Reset" /> </div> <!--buttons end--> </form> <?php // get posted data into local variables $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); $EmailTo = "helenguttridge@live.com"; $Subject = "Electrod UK contact form completion"; $Name = Trim(stripslashes($_POST['Name'])); $Companyname = Trim(stripslashes($_POST['Companyname'])); $Telephonenumber = Trim(stripslashes($_POST['Telephonenumber'])); $Yourmessage = Trim(stripslashes($_POST['Yourmessage'])); // validation $validationOK=true; if (Trim($EmailFrom)=="") $validationOK=false; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Companyname: "; $Body .= $Companyname; $Body .= "\n"; $Body .= "Telephonenumber: "; $Body .= $Telephonenumber; $Body .= "\n"; $Body .= "Yourmessage: "; $Body .= $Yourmessage; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=index.htm\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?>
March 27, 201016 yr First, remove the <> from "From: <$EmailFrom>" ie. "From: $EmailFrom" Next. It is not a good idea to have the email from the person who filled in the form as the email is being sent from your site. So use this. $header = "MIME-Version: 1.0\r\n"; $header .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; $header .= "From: ".$FromAddress."\r\n"; $header .= "Reply-To: ".$email."\r\n"; mail($email, $Subject , $Body, $header,'-f'.$FromAddress
March 27, 201016 yr Author Ok... thanks Turns out the form wasn't the problem.... it just wouldn't send to my email address. It was fine with another address though... strange!
March 27, 201016 yr Ok... thanks Turns out the form wasn't the problem.... it just wouldn't send to my email address. It was fine with another address though... strange! The reason is that you were sending it 'from' an email address that was not related to the site you were on. Some email clients will not accept emails where the from address is not the same as the site from which it was sent. That's why some email clients accept it (the less fussy ones) and some reject it.
March 28, 201016 yr You may be able to get your hosting company to set up a SPF record for your website so that email clients don't associate your website with spam - whether or not they will depends on the hosting type you have really. If you're on some super cheap shared hosting then chances are they wont do it unless you've been with them a while
Create an account or sign in to comment